What is the result?

Given:

11. class X { public void foo() { System.out.print(“X “); } }
12.
13. public class SubB extends X {
14. public void foo() throws RuntimeException {
15. super.foo();
16. if (true) throw new RuntimeException();
17. System.out.print(“B “);
18. }
19. public static void main(String[] args) {
20. new SubB().foo();
21. }
22. }

What is the result?

Given:

11. class X { public void foo() { System.out.print(“X “); } }
12.
13. public class SubB extends X {
14. public void foo() throws RuntimeException {
15. super.foo();
16. if (true) throw new RuntimeException();
17. System.out.print(“B “);
18. }
19. public static void main(String[] args) {
20. new SubB().foo();
21. }
22. }

What is the result?

A.
X, followed by an Exception.

B.
No output, and an Exception is thrown.

C.
Compilation fails due to an error on line 14.

D.
Compilation fails due to an error on line 16.

E.
Compilation fails due to an error on line 17.

F.
X, followed by an Exception, followed by B.

Explanation:
X Exception in thread “main” java.lang.RuntimeException
at SubB.foo(Main.java:16)
at SubB.main(Main.java:20)



Leave a Reply 2

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


Alvaro

Alvaro

Correct answer is option A. Output is: X followed by an exception trace.