Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assertions

 
Marcelo Ortega
Ranch Hand
Posts: 528
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First off, if a question were to simply ask "What would be the result?" on a question involving assterions on the exam, do we assume that assertions are enabled or disabled at runtime? Compiled with or not compiled with?

Secondly...

What is the result of the following question?



A. An AssertionError is thrown and "Continue..." is output.
B. An AssertionError is thrown with the message �No good�.
C. An AssertionError is thrown with the message �Ok�.
D. Continue...
E. Compilation fails.

Answer givenis E.
I also think it won't compile due to incorrect syntax on line 1???
But i don't trust this mock's results because they have alot of errors in their answers.
 
Richard Martin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
assert statements can take 2 forms

Ex 1: assert (boolean expression);

or

Ex 2: assert (boolean expression) : "houston we have a problem"; -- (in this example the second part can be anything that returns a value, you can send it of to a method as long as the method returns a value, the result will be converted to a string and displayed with the stack trace)

The reason this code will fail to compile is because (val > 0) ? �Ok� : �No good� will not result in a boolean value, it will result in a String. The first part of an assert statement must always result in a boolean value, if you have a second expression as in example 2 that must always result in a value.


The first question is a good one though, from a similar point if they use assert as a variable name in a question are we to assume assertions are enabled?
 
Marcelo Ortega
Ranch Hand
Posts: 528
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So yor saying the following will compile fine:

assert (val > 0) ? true : false;

??
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic