An assert statement can be used to check a control-flow invariant to verify which of the following?
a. A particular assumption is true when the flow of control enters a method. b. The flow of control does not reach a particular point in the program. c. The normal flow of control has reached a particular point in the program. d. The normal flow of control has reached the end of a method. e. The default case of a switch statement is not reached. f. The else block of an if/else statement is not reached.
Question from Dan's tests..the answer says b,e,f are true..I feel that a is also true.. So can someone explain me why option a is not considered.
There is a school of thought which says that preconditions should not be checked by assertions. A precondition could be that the state of the object is valid when a method is called, or that the values of the arguments passed to the method are valid . An example is checking that the value passed to a square root method is non-negative. Because assertions can be turned off at runtime the check for a non-negative argument will not occur and the program may crash or silently produce nonsense when supplied with an invalid value. The correct technique, according to Sun, is to throw an exception when an invalid argument value is passed or the state of the object on which the method is called is invalid.
For the SCJP remember this: do not check method arguments with assertions.