Question

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

Option 3 : false / false

Advertisement

Detailed Solution

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

Key Points
Helpful?
PrepSnapprepsnap.in • Part -A

Question:

What is the output of the following Java code? `Integer a = 127; Integer b = 127; Integer c = 128; Integer d = 128; System.out.println((a == b) + " " + (c == d));` — note: this line does not compile as `+` on booleans is illegal; instead consider `System.out.println(c == d);` / निम्नलिखित Java कोड पर विचार करें: `Integer c = 128; Integer d = 128; System.out.println(c == d);` आउटपुट क्या होगा?

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

Correct Answer: false / false

`==` compares Integer references; autoboxed values in the -128..127 range are cached (so 127==127 is true) but 128 falls outside the cache, giving two distinct objects and hence false. The trap is assuming autoboxing always reuses objects. / `==` Integer reference की तुलना करता है; -128..127 range के autoboxed मान cache होते हैं (अतः 127==127 true है) पर 128 cache से बाहर है, जिससे दो अलग ऑब्जेक्ट बनते हैं और परिणाम false। जाल यह मानना है कि autoboxing हमेशा ऑब्जेक्ट पुनः उपयोग करता है।

Downloaded from PrepSnap (prepsnap.in)