Win a copy of Testing JavaScript Applications 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

assert detail from DanC exam

 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi...here is the following Q
class D {
static {
boolean assertsEnabled = false;
assert assertsEnabled = true;
if (!assertsEnabled) {throw new RuntimeException();}
}
public static void main (String[] args) {}
}
possible answers are as follows:
Which statements are true?
a. If assertions are not enabled at run time it prints an error message.
b. If assertions are not enabled at run time it prints nothing.
c. With assertions enabled it prints an error message.
d. With assertions enabled it prints nothing.
e. The assert statement is being used to check a class invariant--something that must be true about each instance of the class.
f. The assert statement is being used to prevent the class from being initialized if assertions are disabled.
g. The assert statement is being used to check a postcondition--something that must be true when the method completes successfully.
h. A compiler error is generated.
i. None of the above.
the answer is a,d,f. I don't have a problem with that.
i just wonder what the compiler thinks of assert being there, if assertions are disabled - does it not stress out about the syntax?
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If assertions are disabled the assert statements are just treated like comments. No side effects.
If you compile a java file that has aseert statements with jdk1.3 (or without -source 1.4 option ) you get error message that assert is a reserved word.
If you don't use java -ea or any of its flavors then the assert statements compiled above are just ignored. You can turn them on any time...
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jasper,
Thank you for using my exam.
The question assumes that the code is compiled using "source mode 1.4", but I suppose that some readers might assume otherwise. If I remember correctly, real exam questions involving programs that use assertions make a similar assumption. In other words, the question does say something like the following.
"Assume the following code is compiled using source mode 1.4. What is the result of running the program?"
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ah yes, i see, thankyou!
btw - thanks again for your exams, they make me think, very helpful for learning.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are certainly welcome Jasper.
 
I'm not dead! I feel happy! I'd like to go for a walk! I'll even read a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic