aspose file tools*
The moose likes Mock Exam Errata and the fly likes Go4Java Q22 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Go4Java Q22" Watch "Go4Java Q22" New topic
Author

Go4Java Q22

Bala Raj
Ranch Hand

Joined: Sep 12, 2000
Posts: 75
What will be the result of compiling and running the given program? Select one correct answer.

The given ansers are:
1. Compile time error in line no. 8 as main() method is declared void.
2. Program compiles correctly and prints "A" when executed.
3. Program compiles correctly and prints "A" and "C" when executed.
4. Compile time error at line no.14 due to statement not reached.
I thought the answer is 4, but the given answer is 2.
When tried to execute the code I am getting the following error:
Thanx in Advance
Bala

[This message has been edited by Bala Rajamani (edited October 08, 2001).]
Bill Krieger
Ranch Hand

Joined: Sep 27, 2001
Posts: 53
This code runs fine on my machine using version 1.2 running under NT 4. What version are you running?
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Bala
First, like Bill, the code compiled fine on my system.
Second, because your in a try-catch block there is a chance that some sort of Exception could be thrown, the line after the catch could be reached. That is why you dont get an unreachable statement error.
Check out the JLS section 14.20 for a detailed explaination. Basically what it all says is that a statement is considered reachable if and only if the statements that come before it are reachable and can complete normally.
hope that helps

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform


Dave
Bala Raj
Ranch Hand

Joined: Sep 12, 2000
Posts: 75
Hi,
I am running JDK 1.3.1 on Windows 2000 Pro machine.
From JLS:
<quote>
A catch block C is reachable iff both of the following are true:
1. Some expression or throw statement in the try block is reachable and can throw an exception whose type is assignable to the parameter of the catch clause C. (An expression is considered reachable iff the innermost statement containing it is reachable.)
2. There is no earlier catch block A in the try statement such that the type of C's parameter is the same as or a subclass of the type of A's parameter.
</quote>
Instead of "Exception" if I put "java.lang.Throwable" I am getting run time error.
Still I am confused how its running on your machine correctly but not in mine.
Thanks,
Bala.
Nain Hwu
Ranch Hand

Joined: Sep 16, 2001
Posts: 139
Dave,
I can't quite get what JLS 14.20 has said.
What you said looks reasonable. But, according to the same
section of JLS, it says a return statement can NOT completed
normally:

A break, continue, return, or throw statement cannot complete normally.

So, I would think even the catch block is not reachable.
But, the code compiled fine. I am totally confused.
Help!
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Nain
Here is the quote from the JLS

A try statement can complete normally iff both of the following are true:
The try block can complete normally or any catch block can complete normally.
If the try statement has a finally block, then the finally block can complete normally.
The try block is reachable iff the try statement is reachable.
A catch block C is reachable iff both of the following are true:
Some expression or throw statement in the try block is reachable and can throw an exception whose type is assignable to the parameter of the catch clause C. (An expression is considered reachable iff the innermost statement containing it is reachable.)
There is no earlier catch block A in the try statement such that the type of C's parameter is the same as or a subclass of the type of A's parameter.

The try statement is definately reachable so the next question is to determine if it can camplete normally. The try can complete normally if the catch can complete normally. The first question to answer there is if the catch is reachable:
The catch is reachable because both of its rules are met:
There is a statement in the try that is reached and can throw an exception, and
There is no catch before the one in the code that would catch the exception.
Because the catch can complete normally the statement after it can be reached.
If your confused about the fact that the return in the try block means it can't complete normally then keep in mind that an Exception could happen before the return, in which case it would go to the catch statement.
hope that clears it up for you

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
Nain Hwu
Ranch Hand

Joined: Sep 16, 2001
Posts: 139
Dave,
This is exactly where I got confused:

The catch is reachable because both of its rules are met:
There is a statement in the try that is reached and can throw an
exception, and There is no catch before the one in the code that would catch the exception.

In the code, the try block has only a return statement. It
does not throw any exception. That's why I think it doesn't fulfill the above condition. Therefore, I thought the catch
block is not reachable.
So, I am still confused.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Nain
Keep in mind that a RuntimeException can be thrown at any time even without a throw statement. In the code a RuntimeException could be thrown before the return statement. Try this as an example:
Try using any type of RuntimeException in the catch statement. You'll see it compiles fine because the compiler knows that it is possible that the Exception could be thrown and then the catch would execute and finish normally.
Now try it with a checked Exception. You'll get a compiler error saying the Exception isn't thrown in the block. These two examples show that the RuntimeException can be throw anytime and the compiler doesn't know when or if it will so it doesn't consider the last statement unreachable.
I hope that clears it up for you.

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
Nain Hwu
Ranch Hand

Joined: Sep 16, 2001
Posts: 139
Dave,
Thanks. It's clear to me now.
Dominic Mack
Greenhorn

Joined: Sep 26, 2001
Posts: 17
To tag on to this...

I know this compiles... but shouldn't the last line be unreachable?
And why does this work???

 
Don't get me started about those stupid light bulbs.
 
subject: Go4Java Q22
 
Similar Threads
exception handling??
exception
mock exam query
Unreachable code?
on constructor