• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Javacertificate.com question on assertions

 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q. Which of the following situations are good uses of assertions?
Amongst other one of the correct answer was
Ans. To check the precondition of a lock status.
I don't think that checking a lock as the precondition should qualify as a good use of assertions. Becasue locks can be obtained and set free at any point of time. So in case in the development phase the assert might not give an error but when the application is deployed and asserts are disabled it might happen the object is not able to get the lock and the code doesn't functions corectly.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anupam,
Don't forget that if you disable assertions at runtime the assert statement would never be executed no matter what the assert condition is.
Hope it helps.
Param
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paran
That is what I actualy want to to point out that when assertions are disabled at runtime the code that checks whether a lock has been obtainedor not will not be executed. But during development when asserts are enabled it may run fine because the condition is met. But at run time the code which was assumed that it always acquire a lock and then executed might behave differently because there is no gaurantee that the lock would be obtained.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what were the other choices. May be this was the best answer
 
stable boy
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the complete question
Which of the following situations are good uses of assertions? [Check all correct answers]
# Argument checking in public methods.
# To check the precondition of a lock status.
# To check that a location in your code is never reached.
# Whenever you would have written a comment that asserts an invariant.
# To do any work that your application requires for correct operation.


Answers 2, 3 and 4 are correct as they all confirm your assumptions about the behaviour of your program. Answer 1 is incorrect because argument checking is typically part of the published specifications of a method, and these specifications must be obeyed whether or not assertions are enabled or disabled. Answer 5 is also incorrect, because programs must not assume that the boolean expression contained in an assertion will be evaluated. Therefore, if you use an assertion to do any work that your application requires for correct operation, the program will work fine when asserts are enabled, but would fail when they were disabled.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you elaborate on what #4 means.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sometimes there are comments in the code to remind us of an invariant. By replacing them with assertions the invariant is (also) checked when assertions are on.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jose for the nice answer and thanks Thomas for providing the complete question.
 
reply
    Bookmark Topic Watch Topic
  • New Topic