Question

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

Option 2 : It prints "false" and then throws a ClassCastException / यह "false" प्रिंट करके ClassCastException फेंकेगा

Advertisement

Detailed Solution

सही उत्तर It prints "false" and then throws a ClassCastException / यह "false" प्रिंट करके ClassCastException फेंकेगा है।

Key Points
Helpful?
PrepSnapprepsnap.in • Part -A

Question:

What is the behaviour of the following Java program? / निम्नलिखित जावा प्रोग्राम का व्यवहार क्या होगा? class A {} class B extends A {} class C extends A {} class Test { public static void main(String[] args) { A a = new B(); System.out.print(a instanceof C); C c = (C) a; System.out.print("done"); } }

1.
It prints "true done" / यह "true done" प्रिंट करेगा
2.
It prints "false" and then throws a ClassCastException / यह "false" प्रिंट करके ClassCastException फेंकेगा
3.
It prints "false done" / यह "false done" प्रिंट करेगा
4.
Compilation fails / संकलन विफल होगा

Correct Answer: It prints "false" and then throws a ClassCastException / यह "false" प्रिंट करके ClassCastException फेंकेगा

a actually refers to a B, so a instanceof C is false; the downcast (C) a then fails at run time with ClassCastException, and "done" is never printed. / a वास्तव में B को संदर्भित करता है, इसलिए a instanceof C false है; फिर डाउनकास्ट (C) a रन टाइम पर ClassCastException के साथ विफल होता है और "done" कभी प्रिंट नहीं होता।

Downloaded from PrepSnap (prepsnap.in)