Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assertion problem?

 
Dan Coo
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all I've come across this question in a mock exam and I can't understand the answer, could somebody help please?
public class Assertification {
public static void main(String args[]){
assert args.length == 0;
}
}
Which of the following conditions must be true to throw an AssertionError?
a) The args array must have exactly zero elements.
b) The args array must have one or more elements.
The answer is (a), but doesn't that make the condition true. Shouldn't the condition be false to throw an AssertionError?
 
Ben Ritchie
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've seen this before in Heller & Roberts 1.4 book - is the code fragment a shortened version of that question? The H&R answer is a typo.
(edited)the assert will throw an AssertionError only if args.length > 0 so the answer is (b) - it is in the errata on the sybex website
[ November 28, 2002: Message edited by: Ben Ritchie ]
 
Dan Coo
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ben, I've been going over this for days, it is from the Heller and Roberts "Complete Java 2 Certification Study Guide". Thanks again.
 
Ben Ritchie
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Confused me too...
 
Ken Krebs
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's always best with any technical book to check the errata for the book. This is a pain but it's the 1st thing I usually do with a tech book because so many of them are rushed out and even if they are not it can be very difficult fot the authors/publishers to be 100% correct when so many details are covered.
Happy trails
 
anushree ari
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but ben, the typo is args.length>0 will throw assertion error, but the ans is a) contrast, what is exact result,
 
Ben Ritchie
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whoops ... must read before I post...
Edited
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic