• 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
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Qustion in MockTest about assertions

 
Ranch Hand
Posts: 59
  • 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

Plese Explain(Given answer as D).How this will come?
 
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sukhavasi

Since, there is a while loop before line 20 and compiler knows it is an infinity loop, line 20 will be unreachable and gives compile error.

But, if you replace

with the following, there will be no compiler error
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi MooN

Assertions are disabled at run time by default right? So why compiler is giving 'Unreachable' error at line 20.

Why it does not give error at line 14, as assertion at line 13 is false and compiler knows that it is going to throw AssertionError at line 13.

Can you please explain this?

Murali...
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi your answers are as follow

Q:Assertions are disabled at run time by default right? So why compiler is giving 'Unreachable' error at line 20.
A: Unreachable code is not dependent on assertion, it is compiler task.
so you get it in any code example:
public void m(){
while(true){
System.out.println("Inside");
}
System.out.println("Outside");
}

Q;Why it does not give error at line 14, as assertion at line 13 is false and compiler knows that it is going to throw AssertionError at line 13.
A: A method throw AssertionError at runtime not compile time.
 
If you two don't stop this rough-housing somebody is going to end up crying. Sit down and read this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic