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
πŸ’¬ Discuss
βœ… Correct Answer: (A) True
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.
Explanation by: Mayank
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.

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
243
Total Visits
πŸ“½οΈ
3 y ago
Published
πŸŽ–οΈ
Mayank
Publisher
πŸ“ˆ
89%
Success Rate