Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Use of Assert in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Use of Assert in Java" Watch "Use of Assert in Java" New topic
Author

Use of Assert in Java

kumar Devesh
Greenhorn

Joined: Nov 18, 2011
Posts: 7


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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 13, 2005
Posts: 36514
    
  16
There is another possible answer, which is that none of the assertions is being used appropriately.
kumar Devesh
Greenhorn

Joined: Nov 18, 2011
Posts: 7

Only the assert statements on lines 15 and 18 are used appropriately.

need an explanation why this is correct??
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Feb 08, 2009
Posts: 597

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


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Apr 25, 2010
Posts: 29
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

Joined: Oct 27, 2005
Posts: 19544
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Niroshan Jayathilaka
Greenhorn

Joined: Jul 04, 2012
Posts: 2

i think answer D is correct. line 15 will definitely throw an AssertinError. if assertion is enabled in runtime.


Niroshan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Use of Assert in Java
 
Similar Threads
assertion
Assertion question doubt??
assertions