What is the result?

Given:

05. class A {
06. void foo() throws Exception { throw new Exception(); }
07. }
08. class SubB2 extends A {
09. void foo() { System.out.println(“B “); }
10. }
11. class Tester {
12. public static void main(String[] args) {
13. A a = new SubB2();
14. a.foo();
15. }
16. }

What is the result?

Given:

05. class A {
06. void foo() throws Exception { throw new Exception(); }
07. }
08. class SubB2 extends A {
09. void foo() { System.out.println(“B “); }
10. }
11. class Tester {
12. public static void main(String[] args) {
13. A a = new SubB2();
14. a.foo();
15. }
16. }

What is the result?

A.
B

B.
B, followed by an Exception.

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

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

E.
An Exception is thrown with no other output.

Explanation:
Main.java:14: unreported exception java.lang.Exception; must be caught or declared to be thrown
a.foo();
^
1 error



Leave a Reply 1

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