Java Questions & Answers – String Comparison

This section of our 1000+ Java MCQs focuses on String comparision in Java Programming Language.

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()
View Answer

Answer: a
Explanation: None.

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()
View Answer

Answer: d
Explanation: None.

3. Which of these methods of class String is used to check whether a given object starts with a particular string literal?
a) startsWith()
b) endsWith()
c) Starts()
d) ends()
View Answer

Answer: a
Explanation: Method startsWith() of string class is used to check whether the String in question starts with a specified string. It is a specialized form of method regionMatches().
advertisement
advertisement

4. What is the value returned by function 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
View Answer

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
View Answer

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 will be the output of the following Java code?

  1.     class output 
  2.     {
  3.         public static void main(String args[])
  4.         { 
  5.            String c = "Hello i love java";
  6.            boolean var;
  7.            var = c.startsWith("hello");
  8.            System.out.println(var);
  9.         }
  10.     }

a) true
b) false
c) 0
d) 1
View Answer

Answer: b
Explanation: startsWith() method is case sensitive “hello” and “Hello” are treated differently, hence false is stored in var.
Output:

advertisement
$ javac output.java
$ java output
false

7. What will be the output of the following Java code?

advertisement
  1.     class output 
  2.     {
  3.         public static void main(String args[])
  4.         { 
  5.            String s1 = "Hello i love java";
  6.            String s2 = new String(s1);
  7.            System.out.println((s1 == s2) + " " + s1.equals(s2));
  8.         }
  9.     }

a) true true
b) false false
c) true false
d) false true
View Answer

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.
Output:

$ javac output.java
$ java output
false true

8. What will be the output of the following Java code?

  1.     class output 
  2.     {
  3.         public static void main(String args[])
  4.         { 
  5.            String s1 = "Hello";
  6.            String s2 = new String(s1);
  7.            String s3 = "HELLO";
  8.            System.out.println(s1.equals(s2) + " " + s2.equals(s3));
  9.         }
  10.     }

a) true true
b) false false
c) true false
d) false true
View Answer

Answer: c
Explanation: None.
Output:

$ javac output.java
$ java output
true false

9. In the following Java 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”);
View Answer

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 will be the output of the following Java code?

  1.     class output 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.              String chars[] = {"a", "b", "c", "a", "c"};
  6.              for (int i = 0; i < chars.length; ++i)
  7.                  for (int j = i + 1; j < chars.length; ++j)
  8.                      if(chars[i].compareTo(chars[j]) == 0)
  9.                          System.out.print(chars[j]); 
  10.         }
  11.    }

a) ab
b) bc
c) ca
d) ac
View Answer

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.
output:

$ javac output.java
$ java output  
ac

Sanfoundry Global Education & Learning Series – Java Programming Language.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.