• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

assert detail from DanC exam

 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are certainly welcome Jasper.
 
I suggest huckleberry pie. But the only thing on the gluten free menu is this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic