Question

1.
2.
3.
4.
Answer (Detailed Solution Below)

Option 4 : false / false

Advertisement

Detailed Solution

सही उत्तर false / false है।

Key Points
Helpful?
PrepSnapprepsnap.in • Part -A

Question:

What is the output of the following Java code? `String s1 = "abc"; String s3 = new String("abc"); System.out.println(s1 == s3);` / निम्नलिखित Java कोड का आउटपुट क्या होगा? `String s1 = "abc"; String s3 = new String("abc"); System.out.println(s1 == s3);`

1.
true / true
2.
Compilation error / कंपाइलेशन त्रुटि
3.
NullPointerException / NullPointerException
4.
false / false

Correct Answer: false / false

`==` on objects compares references, not content: `s1` refers to the pooled literal while `s3` is a distinct heap object, so the result is false. The trap is confusing `==` with `equals()`, which would return true. / ऑब्जेक्टों पर `==` content की नहीं, reference की तुलना करता है: `s1` pooled literal को और `s3` एक अलग heap ऑब्जेक्ट को संदर्भित करता है, अतः परिणाम false है। जाल `==` को `equals()` समझना है, जो true लौटाता।

Downloaded from PrepSnap (prepsnap.in)