• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Good Qn on Unreachable Code

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)?
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic