wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assert Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assert Doubt" Watch "Assert Doubt" New topic
Author

Assert Doubt

vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
While attending mock exams i came thru this question

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

Answer is D

How.....anyone explain me???
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
I guess the last statement "assert false;" is unreachable,
thats why u r getting a compiler error.
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
Hi Animesh

If lineno 20 is unreahable then lineno 14 should also be unreachable...
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
Vidya,
Until the JVM runs with assert flag enabled, u may not be able to determine whether the statement will ever be reachable. What if u dont run with the Assertions Enabled. U may just get blank output, right?

So compiler checks for only those which u can determine during compile time itself whether its reachable or not.
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
The code is unreachable because of while ( true ) statement.


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
Because of full mind thinking about assertion in mind i forgot to see while loop

ya i got it
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
and if what assertion are enabled

what will happen ?

if bar() method is called first. ?

if foo() method is called first ??


Thanks and Regards, Amit Taneja
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Assert Doubt
 
Similar Threads
Good Qn on Unreachable Code
Assert
Assertion discussion
Assert Question
Assertion Question...explain this please