Share on Google+Share on Google+

SCJP Module-2 Question-18

Ads
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'.

Advertisements

Ads

Posted on: July 8, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Ads

Advertisement