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 Good Qn on Unreachable Code 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 » Programmer Certification (SCJP/OCPJP)
Bookmark "Good Qn on Unreachable Code" Watch "Good Qn on Unreachable Code" New topic
Author

Good Qn on Unreachable Code

Chandrakanth
Ranch Hand

Joined: Aug 16, 2005
Posts: 60
11. public class Test {
12. public void foo() {
13. assert false;
14. assert false;
15. }
16. public void bar(){
17. while(true){
18. assert false;
19. }
20. assert false;
21. }
22. }

What causes compilation to fail?
A. Line 13
B. Line 14
C. Line 18
D. Line 20

Ans is D,

why not 14.(ie B)?
Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
Hi,
I hv analyzed the given program in this way,
assert false;
statement will be executed if assertions are enabled during the runtime. If assertions are disabled during the run time , assert statements are just like commented statements. During the compilation compiler doesn't know whether the user executes the program with assertions enabled/disabled, so it is checking only syntax of assert statement.
But any statement after while(true){ } is not reachable , because there is no 'break' statement with in the infinite loop. [ I hv added break statment in the while loop, then no compilation error ] thats why no compilation error at line14.
Please correct me if i am wrong.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Good Qn on Unreachable Code
 
Similar Threads
Assertion Question...explain this please
Assert
Qustion in MockTest about assertions
Assert Question
Assertion discussion