• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use of Assert in Java

 
kumar Devesh
Greenhorn
Posts: 7
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • 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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • 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 Java Oracle
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
ali kamran
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Niroshan Jayathilaka
Greenhorn
Posts: 2
Java Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think answer D is correct. line 15 will definitely throw an AssertinError. if assertion is enabled in runtime.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic