16. What will be printed when you execute the following code?
Choices:
a. Z
b. YZ
c. XYZ
d. YXYZ
Answer:-
D is correct.
A difficult but a fundamental question, please observe carefully.
Before any object is constructed the object of the parent class is constructed(as there is a default call to the parent's constructor from the constructor of the child class via the super() statement).
Also note that when an object is constructed the variables are initialized first and then the constructor is executed.
So when new Z() is executed , the object of class X will be constructed, which means Y b = new Y() will be executed and "Y" will be printed as a result.
After that constructor of X will be called which implies "X" will be printed.
Now the object of Z will be constructed and thus Y y = new Y() will be executed and Y will be printed and finally the constructor Z() will be called and thus "Z" will be printed.
Thus YXYZ will be printed.
Choices:
a. Z
b. YZ
c. XYZ
d. YXYZ
Answer:-
D is correct.
A difficult but a fundamental question, please observe carefully.
Before any object is constructed the object of the parent class is constructed(as there is a default call to the parent's constructor from the constructor of the child class via the super() statement).
Also note that when an object is constructed the variables are initialized first and then the constructor is executed.
So when new Z() is executed , the object of class X will be constructed, which means Y b = new Y() will be executed and "Y" will be printed as a result.
After that constructor of X will be called which implies "X" will be printed.
Now the object of Z will be constructed and thus Y y = new Y() will be executed and Y will be printed and finally the constructor Z() will be called and thus "Z" will be printed.
Thus YXYZ will be printed.
No comments:
Post a Comment