• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Possible unreachable code

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI guys, in thie question from
Glenn, Mitchell. OCAJP Oracle Certified Associate Java SE 8 Programmer Practice Exams (Kindle Locations 11644-11649). Enthuware. Kindle Edition.

Consider the following question...

Which of the following statements are correct ? Select 3 options
A. If run with an argument of 'false', it will print 'False False'
B. If run with an argument of 'false', it will print 'True True'
C. If run with an argument of 'true', it will print 'True False'
D. It will never print 'True True' E. It will not compile.


I answered A,C,D which are correct, but upon reflection, there is some unreachable code there and I wonder why this compile at all. Whatever value flag gets, this statement

will never be reached. Correct?
 
Henry Wong
author
Marshal
Pie
Posts: 22095
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The unreachable code rules do not apply for if/else blocks. This is to allow for conditional code.

Henry
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah I see, thanks for clarifying it
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:I answered A,C,D which are correct, but upon reflection, there is some unreachable code there and I wonder why this compile at all.

The "unreachable code" topic is one of the more popular ones in this forum. So using the search function you'll find plenty of topics about "unreachable code". Here is a list with a few of these topics with excellent explanations and illustrative code snippets:
  • return value for method.
  • Confused point about infinite loop
  • continue and break in a loop would generate compilation error?
  • Which of these is unreachable code?
  • Unreachable Code
  • Which is the first line to cause error?
  • Maybe "unreachable code" should be "dead code" in page 70, (Java OCA 8 Programmer I Study Guide)
  • Throwing a second exception question
  • System.exit() and unreachable code
  • good examples of unreachable code?

  • I think you know what to do this evening

    Hope it helps!
    Kind regards,
    Roel
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic