Welcome Guest | Sign in | Register

Home > Java Programming > Assertions > Questions and Answers

01.
public class Test {
public static void main(String[] args) {
int x = 0;
assert (x > 0): “assertion failed”;
System.out.println(“finished”);
 }
 }
What is the result?
A. finished B. Compilation fails.
C. An AssertionError is thrown. D. An AssertionError is thrown and finished is output.

Answer and Explanation

Answer: Compilation fails.

Explanation:
This question is a bit tricky because it lacks the following information: It should include a
statement that says whether or not assertions are enabled. If they are indeed enabled, the
correction answer is C. but if they are not, the correct answer is A. Assertions are not enabled by default so if the question is not changed, the most logical answer is A.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
02.
Public class Foo{
public void m1( int value ){
assert 0 <= value;
System.out.println( "OK" );
}
public static void main( String[] args ){
Foo foo = new Foo();
System.out.print( "foo.m1( 1 ): " );
foo.m1( 1 );
System.out.print( "foo.m1( -1 ): " );
foo.m1( -1 );
}
}

Predict the output of code if it is assert enabled?
A. OK B. OK OK
C. OK then triggers an assertion error D. None of these

Answer and Explanation

Answer: OK then triggers an assertion error

Explanation:
assert expression1;
assert expression1 : expression2;
In each form, expression1 is the boolean-typed expression being asserted. The expression
represents a program condition that the developer specifically proclaims must be true during program execution. In the second form, expression2 provides a means of passing a String message to the assertion facility

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum



Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved 2012-2015 SoftLucent.