• 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Assertions doubt?

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

Consider the following code,


The answer options,

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.

Answer given as a,b,d. I can reason out a but how come b and d are also option??

Anyone please help me.

(Corrected punctuation in topic, placement of [/C0DE] tag)
[ December 05, 2006: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A and B are correct but I'm not sure why D is?

Anyway, B is correct because the code is throwing an AssertionError and not using the assert statement (which requires the -ea flag). It's as if it was throwing any other throwable class.

At least that is the way I see it. I'm wondering if D is just wrong.

BTW the forum would like for you to state the references for all mock questions we post.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where did the question come from?
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greg,

I got the reason as to why d is correct. If you replace the default block with an assert statement, then when you run the code with assertions disabled, it will give you a compiler error saying that the default block has got no return statement.

Thanks for the effort. It is after reading your post, I got this in my mind.
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Barry,

Sorry that I didn't mention the souce. It is from Dan Chisolm's website.

Here is the link,
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jothi Shankar Kumar Sankararaj:
I got the reason as to why d is correct. If you replace the default block with an assert statement, then when you run the code with assertions disabled, it will give you a compiler error saying that the default block has got no return statement.



Jothi, actually this reasoning is not quite right. If you simply replace the throw in the default block with an assert statement, the code won't compile...ever. You would need a return statement following the assert for it to compile. So, the reasoning for 'd' has nothing to do with running with assertions enabled or disabled because you won't ever get to the point where you can run the code.

-Mark
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark,

When you say throw new AssertionError, you are actually returning something which will be caught by the JVM this cae. So you are not violating the rules for a method that requires a return. Now, when you replace the throws clause with a assert statement, you are not returning anything when the control reaches the default block. So considering this, I feel that option d will also be correct.
Any thoughts to it?
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Jothi,

Could you please clarify this? I am unable to get your point.
As far as I believe, returning is far different from throwing (since getting return value is equally different from catching).

When you say throw new AssertionError, you are actually returning something which will be caught by the JVM this cae. So you are not violating the rules for a method that requires a return.

 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vinayagar,

Consider the following code, also from Dan Chisolm.



In the above code, the method m1 returns with a throws clause. So when you run this with assertions enabled, it compiles but when you run with asertions disabled, it fails to compile. Also, if you replace the throws clause with an assert statement, it will fail to compile even with assertions enabled as you don't have a return statement.
 
Vinayagar Karpagam
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for your explanation.
 
Mark Allenb
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jothi,

Sorry, after reading your previous post, I see that you must have just mistyped and meant to say "compile" instead of "run", but, even then, what you said doesn't make sense. You said:

Originally posted by Jothi Shankar Kumar Sankararaj:
I got the reason as to why d is correct. If you replace the default block with an assert statement, then when you run the code with assertions disabled, it will give you a compiler error saying that the default block has got no return statement.



When you said "when you run the code with assertions disabled, it will give you a compiler error...", that statement is what I was referring to. That statement is not correct. Please re-read what you said and my previous post and I think you will understand my point. In summary, 'd' is correct because if you substitute the throw with an assert statement, the code will never compile, and thus it will never run. So, combining "run the code with assertions disabled" with "you get a compiler error" just doesn't make sense.

I hope it is clearer now what I was saying.

-Mark
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark,

Thanks for pointing out. That was a typo mistake when I said "run the code with assertions disabled" won't compile. Actually the run over there meant not the actual run command, but rather I meant the step you do to compile.

Anyways, at last we understood the concept which is important.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic