Java Multiple Choice Questions & Answers on String Comparison for Freshers

1. Which of these method of class String is used to compare two String objects for their equality?

a) equals()
b) Equals()
c) isequal()
d) Isequal()
Answer: a
2. Which of these methods is used to compare a specific region inside a string with another specific region in another string?

a) regionMatch()
b) match()
c) RegionMatches()
d) regionMatches()
Answer: d
3. Which of these method of class String is used to check weather a given object starts with a particular string literal?

a) startsWith()
b) endsWith()
c) Starts()
d) ends()
Answer: a

Explanation: Method startsWith() of string class is used to check whether the String in question starts with a specified string. It is specialized form of method regionMatches()
4. What is the value returned by unction compareTo() if the invoking string is less than the string compared?

a) zero
b) value less than zero
c) value greater than zero
d) None of the mentioned
Answer: b

Explanation: compareTo() function returns zero when both the strings are equal, it returns a value less than zero if the invoking string is less than the other string being compared and value greater than zero when invoking string is greater than the string compared to.
5. Which of these data type value is returned by equals() method of String class?

a) char
b) int
c) boolean
d) All of the mentioned
Answer: c

Explanation: equals() method of string class returns boolean value true if both the string are equal and false if they are unequal.
6. What is the output of this program?
  1.     class output {
  2.         public static void main(String args[])
  3.         { 
  4.            String c = "Hello i love java";
  5.            boolean var;
  6.            var = c.startsWith("hello");
  7.            System.out.println(var);
  8.         }
  9.     }
a) true
b) false
c) 0
d) 1
Answer: b

Explanation: startsWith() method is case sensitive “hello” and “Hello” are treated differently, hence false is stored in var.
7. What is the output of this program?
  1.     class output {
  2.         public static void main(String args[])
  3.         { 
  4.            String s1 = "Hello i love java";
  5.            String s2 = new String(s1);
  6.            System.out.println((s1 == s2) + " " + s1.equals(s2));
  7.         }
  8.     }
a) true true
b) false false
c) true false
d) false true
Answer: d

Explanation: The == operator compares two object references to see whether they refer to the same instance, where as equals() compares the content of the two objects.
8. What is the output of this program?
  1.     class output {
  2.         public static void main(String args[])
  3.         { 
  4.            String s1 = "Hello";
  5.            String s2 = new String(s1);
  6.            String s3 = "HELLO";
  7.            System.out.println(s1.equals(s2) + " " + s2.equals(s3));
  8.         }
  9.     }
a) true true
b) false false
c) true false
d) false true
Answer: c
9. In the below code, which code fragment should be inserted at line 3 so that the output will be: “123abc 123abc”?
 1 StringBuilder sb1 = new StringBuilder("123");
 2 String s1 = "123";
 3  // insert code here
 4 System.out.println(sb1 + " " + s1);
a) sb1.append(“abc”); s1.append(“abc”);
b) sb1.append(“abc”); s1.concat(“abc”);
c) sb1.concat(“abc”); s1.append(“abc”);
d) sb1.append(“abc”); s1 = s1.concat(“abc”);
Answer: d

Explanation: append() is stringbuffer method and concat is String class method.
append() is stringbuffer method and concat is String class method.
10. What is the output of this program?
  1.     class output {
  2.         public static void main(String args[])
  3.         {
  4.              String chars[] = {"a", "b", "c", "a", "c"};
  5.              for (int i = 0; i < chars.length; ++i)
  6.                  for (int j = i + 1; j < chars.length; ++j)
  7.                      if(chars[i].compareTo(chars[j]) == 0)
  8.                          System.out.print(chars[j]); 
  9.         }
  10.    }
a) ab
b) bc
c) ca
d) ac
Answer: d

Explanation: compareTo() function returns zero when both the strings are equal, it returns a value less than zero if the invoking string is less than the other string being compared and value greater than zero when invoking string is greater than the string compared to.

Related

HTML Multiple Choice Questions & Answers on Major HTML5 Themes for Freshers

1. All elements are identified by their __________ and are marked up using either start tags and end tags or self-closing tags a) attribute nameb) tag namec) class named) none of the mentioned Ans...

C Programming Questions and Answers on For Loop for Freshers

1. The following code ‘for(;;)’ represents an infinite loop. It can be terminated by. a) breakb) exit(0)c) abort()d) all of the mentioned Answer: a 2. The correct syntax for running two variable ...

CSS Multiple Choice Questions & Answers on CSS Text for Freshers

1. Which of the following CSS property is used to set the text formatting? a) fontb) font-stylec) text-decorationd) all of the mentioned Answer: c 2. Which of the following sets the color of any ...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item