File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Valentin's mock exam no1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Valentin Watch "Valentin New topic
Author

Valentin's mock exam no1

Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
Hi Valentin

public class Question38 {
public static void main(String[] args){
while(false);//line 1
if(false);//line 2
do{}while(false);//line 3
for(;false ;//line 4
}
}

You stated about the above question that line1 and line 4 won't compile because of unreachable statements problem. I would agree with you if there was another statement other than the empty one. The code above compiles fine but not the following.


Whatever doesn't kill us ...<br />Is probably circling back for another try.<br />SCJP 1.4
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
It depends on which compiler you use to compile the code, but as per the specification (JLS 14.20 Unreachable Statements), such code SHOULD NOT compile.
An excerpt of that section is given here:
-----JLS 14.20 quote begin-----
[...]
the following statement results in a compile-time error:
while (false) { x=3; }
because the statement x=3; is not reachable; but the superficially similar
case:
if (false) { x=3; }
does not result in a compile-time error. An optimizing compiler may realize
that the statement x=3; will never be executed and may choose to omit the
code for that statement from the generated class file, but the statement
x=3; is not regarded as "unreachable" in the technical sense specified here.
-----JLS 14.20 quote end-----
If you read that section entirely, it will become clear to you that only
lines 2 and 3 compile correctly. Bottom line,
when in doubt, trust the specification and not the compilers which are not
always specification-compliant...
The empty statement is a statement like any other except that certain optimizing compilers may decide to disregard it.
[ October 14, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
That's right Valentin but when using
while(false); there's no statement to be reached anyway.
P.S i'm using jdk 1.4.1
Did you try to compile it. ?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
No, the statement to execute is the empty statement denoted by the semicolon ;
And yes, I have compiled that code and it produces.

PS1: I never release code that has not been compiled
PS2: Both 1.4 and 1.4.1 yield the same compilation output
[ October 14, 2002: Message edited by: Valentin Crettaz ]
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
Weird I compiled it using j.d.k 1.4.1 and 1.3.1 and it doesn't produce any errors.
Anyway thx and sorry about that
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
btw do you have any other mock exams. I can only see one ?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
I am preparing a second mock exam but I have been under lots of pressure lately and had to postpone the release of my new mock. It should be out by the end of this year.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Valentin's mock exam no1
 
Similar Threads
Unreachable stmts
Question 38
while(false) etc.
New SCJP mock exam
Inner classes