• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assertion : Dan's mock question

 
Shubh Bhat
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A {
private void m1 (int i) {
assert i < 10 : i;
System.out.print(i);
}
public void m2 (int i) {
assert i < 10 : i;
System.out.print(i);
}
public static void main (String[] args) {
A a = new A();
a.m1(11);
a.m2(12);
}
}

two of the answers are:
a. If assertions are not enabled at run time it prints an error message.
b.With assertions disabled it prints "1112"
what is the difference between. assertion not enabled and assertion disabled??
please explain. Thanks
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assertions can be disabled at compile time and at run time. The details are available in the document titled "Programming with Assertions".
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My assertions exam has not yet been added to my main exam page. Instead, it is still a separate beta exam.
I see that the assertions exam has had 147 hits. I'm wondering how people feel about it. Was it too easy, too difficult, or just about right?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually option b) is:
b. If assertions are not enabled at run time it prints "1112".
Dan, I know it's 4am here and I should be sleeping, but why do you say the above code example gives an error when assertions are disabled at runtime(case a)? The answer to Q9 says a) is true.
I compiled it with -source 1.4 and did not enable
assertions at run time. It printed 1112. So b) is the true one.
-Barry
[ September 22, 2002: Message edited by: Barry Gaunt ]
 
Shubh Bhat
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly Barry that was my question.
Dan the questions are interesting, not to tough neither too easy, I think. I would only know how much it is closer to the real stuff, on wed. that's when I plan to take the exam 1.4.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O.K. Now I understand that you are asking why I gave two obviously contradictory answers on the assertions exam. I initially read your question too quickly and thought that you were asking how assertions are enabled. I'm sorry.
I just uploaded the correction. The answers are now as follows.
  • a. If assertions are enabled at run time it prints an error message.
  • d. With assertions disabled it prints "1112".
  • f. The assert statements are being used to check a precondition--something that must be true when the method is invoked.
  • h. Method m2 is an example of an improper use of an assert statement: an assert statement should not be used for argument checking in a public method.
  •  
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic