Output of Java Programs | Set 53 (String Comparison)

Prerequisite : String Comparison in Java

1. What should be the output of this program?

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?

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?

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.

4. What should be the output of this program?

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.

5. What should the output of this program?

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.



My Personal Notes arrow_drop_up

Recommended Posts:


    Article Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.