Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!