Output of Java Programs | Set 53 (String Comparison)
Prerequisite : String Comparison in Java
1. What should be the output of this program?
class GeeksforGeeks { public static void main(String args[]) { String GfG1 = "Welcome to GeeksforGeeks"; boolean GfG2; GfG2 = GfG1.startsWith("hello"); System.out.println(GfG2); } } |
a) true
b) false
c) 0
d) 1
Output:
b) false
Explanation: The startsWith() method is case sensitive “hello” and “Hello” are treated differently, hence false is stored in GfG2.
2. What should be the output of this program?
class GeeksforGeeks { public static void main(String args[]) { String GfG1 = "I am intern at GeeksforGeeks"; String GfG2 = new String(GfG1); System.out.println((GfG1 == GfG2) + " " + GfG1.equals(GfG2)); } } |
a) true true
b) false false
c) true false
d) false true
Output:
d) false true
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.
3. What should be the output of this program?
class GeeksforGeeks { public static void main(String args[]) { String GfG1 = "I am intern at GeeksforGeeks"; String GfG2 = new String(GfG1); System.out.println((GfG1 == "I am intern at GeeksforGeeks") + " " + GfG1.equals(GfG2)); } } |
a) true true
b) false false
c) true false
d) false true
Output:
a) true true
Explanation: The == operator compares two object references to see whether they refer to the same instance but when using == with a string literal(not an instantiated String variable) will only compare the content of the strings.
4. What should be the output of this program?
class GeeksforGeeks { public static void main(String args[]) { String GfG[] = { "a", "b", "c", "a", "c" }; for (int i = 0; i < GfG.length; ++i) for (int j = i + 1; j < GfG.length; ++j) if (GfG[i].compareTo(GfG[j]) == 0) System.out.print(GfG[j]); } } |
a) ab
b) bc
c) ca
d) ac
d) ac
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. What should be the output of this program?
class GeeksforGeeks { public static void main(String args[]) { String GfG1 = "Hello"; String GfG2 = new String(GfG1); String GfG3 = "HELLO"; System.out.println(GfG1.equals(GfG2) + " " + GfG2.equals(GfG3)); } } |
a) true true
b) false false
c) true false
d) false true
Output:
c) true false
Explanation: As we know that equal() method compares the content of the strings. GfG1 and GfG are having the same content. But as we know equal() is case sensitive, therefore GfG2 and GfG3 are different.
6. What should the output of this program?
class GeeksforGeeks { public static void main(String args[]) { StringBuffer GfG1 = new StringBuffer("Hello"); StringBuffer GfG2 = new StringBuffer(" World"); GfG1.append(GfG2); System.out.println(GfG1); } } |
a) Hello
b) World
c) Helloworld
d) Hello World
Output:
d) Hello World
Explanation: append() method of class StringBuffer is used to concatenate the string representation to the end of invoking string.
Recommended Posts:
- Output of Python Programs | Set 23 (String in loops)
- Output of Java Programs | Set 49
- Output of Java programs | Set 29
- Output of Java Programs | Set 31
- Output of Java Programs | Set 12
- Output of Java Programs | Set 50
- Output of Java Programs | Set 30
- Output of Java Programs | Set 32
- Output of Java Programs | Set 37 (If-else)
- Output of Java Programs | Set 38 (Arrays)
- Output of Java Programs | Set 40 (for loop)
- Output of Java Programs | Set 36 (do-while loop)
- Output of Java Programs | Set 34 (Collections)
- Output of Java Programs | Set 41 (try-catch)
- Output of Java Programs | Set 42 (Arrays)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Saurav Bhasin


