Question

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

Option 1 : A2 A1 B

Advertisement

Detailed Solution

सही उत्तर A2 A1 B है।

Key Points
Helpful?
PrepSnapprepsnap.in • Part -A

Question:

What is the output of the following Java program involving constructor chaining with this() and super()? / this() और super() के साथ कंस्ट्रक्टर चेनिंग वाले निम्नलिखित जावा प्रोग्राम का आउटपुट क्या है? class A { A() { this(5); System.out.print("A1 "); } A(int n) { System.out.print("A2 "); } } class B extends A { B() { super(); System.out.print("B "); } } class Test { public static void main(String[] args) { new B(); } }

1.
A2 A1 B
2.
A1 A2 B
3.
A2 B A1
4.
Compilation fails / संकलन विफल होगा

Correct Answer: A2 A1 B

B() calls super() → A() calls this(5) → A(int) prints "A2 ", control returns to A() which prints "A1 ", then B() prints "B ". / B() super() बुलाता है → A() this(5) बुलाता है → A(int) "A2 " प्रिंट करता है, नियंत्रण A() में लौटकर "A1 " प्रिंट करता है, फिर B() "B " प्रिंट करता है।

Downloaded from PrepSnap (prepsnap.in)