This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Good Qn on Unreachable Code

 
Chandrakanth
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic