What is the result?

Given:

5. class Atom {
6. Atom() { System.out.print(“atom “); }
7. }
8. class Rock extends Atom {
9. Rock(String type) { System.out.print(type); }
10. }
11. public class Mountain extends Rock {
12. Mountain() {
13. super(“granite “);
14. new Rock(“granite “);
15. }
16. public static void main(String[] a) { new Mountain(); }
17. }

What is the result?

Given:

5. class Atom {
6. Atom() { System.out.print(“atom “); }
7. }
8. class Rock extends Atom {
9. Rock(String type) { System.out.print(type); }
10. }
11. public class Mountain extends Rock {
12. Mountain() {
13. super(“granite “);
14. new Rock(“granite “);
15. }
16. public static void main(String[] a) { new Mountain(); }
17. }

What is the result?

A.
Compilation fails.

B.
atom granite

C.
granite granite

D.
atom granite granite

E.
An exception is thrown at runtime.

F.
atom granite atom granite



Leave a Reply 1

Your email address will not be published. Required fields are marked *


Matti

Matti

F is correct!
super object is always first constructed!
new object is allowed is constructor (behind super(…) of course)