Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Assertion discussion

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, all:
Given:
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 Line 20.
1)Every assert statement above will throw an assert error.
2)It is also ok for repeating the assertion like line 13-14.
3)Line 20 is unreachable, so Line 20 is really appropriate for such situation.
right?
Thanks,
Roger
 
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My 2�
1- As AssertionError is unchecked, and no reasonable program would try to catch one, only one assert will ever execute in any program.
2- Note 1 makes line 14 irrelevant, as coded it's unreachable, but the compiler doesn't care. If the code were actually checking something, it could make sense.
3- This falls under the Control-Flow Invariant part of assertions (at sun) However, as coded, it is unreachable, period. It can't compile. I think the thought is more along the lines of: 'The program flow could possibly enter this area, but never should. If it does, something is wrong, kill it now'. So, no, it's not appropriate.
 
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't believe it:
This quistion was in the exam that I took today. I answerd line 20, my assumption was becuase program flow....
where did you find this question?
 
Roger Zhao
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hanna, r u kidding? The exact same one? My friend give me this for mock question.
What about your SCJP Exam?? What's your explanation about this case?
Thanks,
Roger
[ August 30, 2003: Message edited by: Roger Zhao ]
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A while statement can complete normally iff at least one of the following is true:
The while statement is reachable and the condition expression is not a constant expression with value true.
There is a reachable break statement that exits the while statement.

JLS 14.20 Unreachable Statements
Hope, this helps.
 
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
compiler error - unreachable statement

now if i comment the statement after while loop.. it compiles fine but goes in a infinite loop
 
Can't .... do .... plaid .... So I did this tiny ad instead:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic