• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Use of Assert in Java

 
Greenhorn
Posts: 7
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

11. public void go(int x) {
12. assert (x > 0);
13. switch(x) {
14. case 2: ;
15. default: assert false;
16. }
17. }
18. private void go2(int x) { assert (x < 0); }

Which statement is true?
A. All of the assert statements are used appropriately.
B. Only the assert statement on line 12 is used appropriately.
C. Only the assert statement on line 15 is used appropriately.
D. Only the assert statement on line 18 is used appropriately.
E. Only the assert statements on lines 12 and 15 are used appropriately.
F. Only the assert statements on lines 12 and 18 are used appropriately.
G. Only the assert statements on lines 15 and 18 are used appropriately.

Can someone please explain use of assert & anwser to this.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please SearchFirst. If you want to know the "use of assert", then simply google for something like java use of assert or java assert tutorial.

Then, once you have studied it and written and executed some code of your own to test your understanding of it, predict what you think will happen.

Then execute the code and observe what happens.

THEN, if you're still puzzled, post a question here, and make sure to TellTheDetails.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will say this, however: There is no definitive right or wrong answer to whether those are appropriate uses of assert. You can't tell in a toy program like that with no context and no explanation for the reasons for the asserts.

Whatever author came up with that didn't do a good job at all.


I take it back. On closer inspection, one could make reasonable inferences about the correctness of the usage, but it's still a bit sketchy. More context would have made it better.
 
Marshal
Posts: 74025
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is another possible answer, which is that none of the assertions is being used appropriately.
 
kumar Devesh
Greenhorn
Posts: 7
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only the assert statements on lines 15 and 18 are used appropriately.

need an explanation why this is correct??
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kumar Devesh wrote:Only the assert statements on lines 15 and 18 are used appropriately.

need an explanation why this is correct??



15: Debatable.

18: Yes.

I'll provide explanation on my reasoning if you do first.
 
Ranch Hand
Posts: 607
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd say only 12 18 ....
Even I'll provide explanation on my reasoning if the OP does first for his ....
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Mercs wrote:I'd say only 12 18 ....
Even I'll provide explanation on my reasoning if the OP does first for his ....



Actually, on a few seconds further thought, I agree. My earlier reasoning on 15 was erroneous.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is G.Appropriate use of assertion is to validate private method argument and not public method.Reason being public method can be called from anywhere (where you have no control)and depending on assertion being on or off the behavior will be different which is not correct.
 
Marshal
Posts: 22450
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You contradict yourself. You say that assertions shouldn't be used for public methods, but the assertion on line 15 will throw an AssertionError for any value of the argument to the public method (the case 2 does not have a break statement). So if assertions are turned on, this method will always fail, and if they are turned off it will never fail.
 
Greenhorn
Posts: 2
Oracle Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think answer D is correct. line 15 will definitely throw an AssertinError. if assertion is enabled in runtime.
 
They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic