• 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Question on assert OCP Oracle Certified Professional Java SE8 Programmer II Study Guide  RSS feed

 
Ranch Hand
Posts: 77
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

This question is regarding an answer on a question in one of the chapter review questions in the book: OCP Oracle Certified Professional Java SE8 Programmer II Study Guide by Jeanne Boyarsky and Scott Selikoff.

The question goes as follows (page 319):
10. Which of the following are true of the code? (Choose all that apply.)


A. Line 5 does not compile.
B. Lines 6 and 7 do not compile because they are missing the String message.
C. Lines 6 and 7 do not compile because they are missing parentheses.
D. Line 6 is an appropriate use of an assertion.
E. Line 7 is an appropriate use of an assertion.

I answered A, but the explanation of this question (page 560) goes as follows:
A, E. Line 5 does not compile because assert is a reserved word making Choice A correct. B and C are incorrect because the parenthesis and message are both optional. D is incorrect because assertions should not have side effects. E is correct because checking an argument passed from elsewhere in the program is an appropriate use of an assertion.


I do not understand why E is correct. To me it seems as if is the same as Can anyone explain me the difference and how the authors came to this answer?

Thanks!

Bart
 
author & internet detective
Posts: 39232
727
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bart,
Those two lines are different because one has "side effects." The one with variable "a" changes the value of "a" when it runs. The one with variable "b" does not. As the book discusses, it is not acceptable practice to have assert statements change the value of data.
 
Bart Boersma
Ranch Hand
Posts: 77
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay I understand but that would make answer D correct, not E.
 
Jeanne Boyarsky
author & internet detective
Posts: 39232
727
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bart Boersma wrote:Okay I understand but that would make answer D correct, not E.


Read it again. The question is asking which is appropriate (E), not which is in appropriate (D.)

 
Marshal
Posts: 64086
215
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Asserting that b > 0 is a normal thing to do. It sounds like a normal class invariant, and you have to preserve the fact that b > 0 at all times
 
Bart Boersma
Ranch Hand
Posts: 77
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops my bad! You are completely right. Thanks for the explanation.
 
What are your superhero powers? Go ahead and try them on this tiny ad:
ScroogeXHTML - the small and fast RTF to HTML converter library
https://coderanch.com/t/707504/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!