Home / Programming MCQs / JAVA MCQs / Question

Q. What is the output of the following code snippet?

Code:
String s1 = “Hello”;
String s2 = “Hello”;
System.out.println(s1 == s2);
(A) True
(B) False
(C) Error
(D) None of these

Explanation:

In Java, when two string literals are created with the same value, they refer to the same object in memory. Therefore, s1 and s2 both refer to the same “Hello” string object. The == operator checks if two variables refer to the same object in memory, so s1 == s2 returns true.

You must be Logged in to update hint/solution

Discusssion

Login to discuss.