Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

For Dan's attention:

 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following is from topic exam for assertion:
class C {
String m1(int i) {
switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default:
throw new AssertionError();
}
}
public static void main(String[] args) {
C c = new C();
for (int i = 0; i < 4; i++) {
System.out.print(c.m1(i));
}
}
}

Which statements are true?
a. With assertions enabled it prints "ABC" followed by an AssertionError message.
b. With assertions disabled it prints "ABC" followed by an AssertionError message.
c. Assertions should not be used within the default case of a switch statement.
d. In this code example an "assert" statement could not be used in place of the "throw" statement.
e. A compiler error is generated.
f. None of the above.
The given answer is a, b and d. I am not sure about d. The explaination is:
The throw statement is used rather than an assert because the compiler knows that the assert statement is not functional when assertions are disabled.
There is no mentioned assumption that assertions will be enabled or disabled. Therefore, an assert (false) ; could have done the job as well.
Thanks
Barkat
 
Nidhi Bangur
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Barkat,
I guess that there is one additional explanation to this question well explained at this link: http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html.
Just to give an overview: You can't use an assert statement here because the compiler will throw an error. Reason: In this particular code, your method has got to return a String object. Thus, either you should write a return statement outside 'Switch' block in which case, you are safe or you should use a 'return' or 'throw' statement in every 'case' statement within 'switch' block. Now, you can't just use an 'assert' statement to replace 'throw' here since the compiler knows that assertions are not always enabled.
I hope that explains the scenario.
-Nidhi.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The loosey-goosey way to explain d is to say, well, when assertions are disabled, it is rather like there is no assert statement at all. If so, then there needs to be a return or a throw statement because the method returns a String.
Now, you didn�t think I would leave you with a loosey-goosey explanation, did you?
According the Assertion Spec (not the Assertion Guide), the assert statement is merely syntactic sugar for this:

where $assertionsDisabled is a synthetic variable added by the compiler.
Now imagine this after the default: label. As you can see the code needs a return or throw statement, because the method returns a String.
When I see an assert statement on mock exams, I imagine that if statement. It helps me through some tricky questions.
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys. I just over looked that. Barkat
 
Jim Crawford
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Why is the if in a do - while block?
Originally posted by Marlene Miller:
According the Assertion Spec (not the Assertion Guide), the assert statement is merely syntactic sugar for this:

where $assertionsDisabled is a synthetic variable added by the compiler.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is the if in a do - while block?

The Assertion Specs says
The surrounding do-while loop is merely an artifice to transform the if statement to the correct syntactic category (StatementWithoutTrailingSubStatement), and has no sematics associated with it.

Here is the way I understand this explanation:
When an assert statement is coded between if and else, the (implicit) do statement surrounding the (implicit) if prevents the else from becoming attached to the (implicit) if.

[ January 14, 2004: Message edited by: Marlene Miller ]
 
Jim Crawford
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marlene Miller:

When an assert statement is coded between if and else, the (implicit) do statement surrounding the (implicit) if prevents the else from becoming attached to the (implicit) if.

I see. Interesting.
Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic