GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes For Dan's attention: Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "For Dan Watch "For Dan New topic
Author

For Dan's attention:

Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
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

Joined: Sep 12, 2003
Posts: 19
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

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Aug 05, 2002
Posts: 787
Thanks guys. I just over looked that. Barkat
Jim Crawford
Ranch Hand

Joined: Sep 08, 2002
Posts: 127
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.


<img src="cool.gif" border="0"> <img src="graemlins/beerchug.gif" border="0" alt="[beerchug]" /> <br />SCJP 1.4
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Sep 08, 2002
Posts: 127
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: For Dan's attention: