This week's giveaway is in the Beginning Java forum.
We're giving away four copies of Bad Programming Practices 101 (e-book) and have Karl Beecher on-line!
See this thread for details.
Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Assertions  RSS feed

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Swati Udas
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok! That's clear now..
Thank You.
 
You can't have everything. Where would you put it?
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!