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.
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.
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.
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.