Question
Option 2 : It prints "false" and then throws a ClassCastException / यह "false" प्रिंट करके ClassCastException फेंकेगा
Detailed Solution
सही उत्तर It prints "false" and then throws a ClassCastException / यह "false" प्रिंट करके ClassCastException फेंकेगा है।
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"); } }
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" कभी प्रिंट नहीं होता।