SCJP Module-2 Question-18


 

SCJP Module-2 Question-18

The given program below checks your understanding of flow of Java program.

The given program below checks your understanding of flow of Java program.

What will be the output of the following code :

class Example18{
static String q = "Hello ";

public static void main(String[] args) {
new Example18().subclass1();
System.out.println(s);
}

void subclass1() {
try {
subclass2();
} catch (Exception e) {
q += "Friends";
}
}

void subclass2() throws Exception {
subclass3();
q += "and";
subclass3();
q += "Brothers";
}

void subclass3() throws Exception {
throw new Exception();
}
}

What will be the output of the code :

1. Hello Friends

2. Hello

3.Friend

4.Compile error

Answer:

(1)

Explanation :

Because 'subclass3' raised exception explicitly due to this, the only execution occurs in the 'catch' block of 'subclass1'.

Ads