• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

For Dan's attention:

 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys. I just over looked that. Barkat
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
First, you drop a couch from the plane, THEN you surf it. Here, take this tiny ad with you:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic