• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assert Doubt

 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess the last statement "assert false;" is unreachable,
thats why u r getting a compiler error.
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Animesh

If lineno 20 is unreahable then lineno 14 should also be unreachable...
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is unreachable because of while ( true ) statement.
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because of full mind thinking about assertion in mind i forgot to see while loop

ya i got it
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and if what assertion are enabled

what will happen ?

if bar() method is called first. ?

if foo() method is called first ??
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic