Welcome Guest | Sign in | Register
Assertions - Discussion Page For Q.2525 | Java Programming Questions and Answers | Java Programming Free Online-Tests | LucentBlackBoard | LucentBlackBoard.com

Home > Java Programming > Assertions > Questions and Answers

Q:  public class Test2{
public static int x;
public static int foo(int y) {
return y * 2;
}
public static void main(String [] args) {
int z = 5;
assert z > 0; /* Line 9 */
assert z > 2: foo(z); /* Line 10 */
if ( z < 7 )
assert z > 4; /* Line 12 */
switch (z) {
case 4: System.out.println("4");
case 5: System.out.println("5");
default: assert z < 10;
}
if ( z < 10 )
assert z > 4: z++; /* Line 20 */
System.out.println(z);
}
}

Which line is an example of inappropriate use of assertions? 
A. Line 9 B. Line 10
C. Line 12 D. Line 20

Answer and Explanation

Answer:Line 20

Explanation:
Assert statements should not cause side effects. Line 20 changes the value of z if the assert statement is false.
Option A is fine; a second expression in an assert statement is not required.
Option B is fine because it is perfectly acceptable to call a method with the second expression of an assert statement.
Option C is fine because it is proper to call an assert statement conditionally.

Comment on this Question

Name:

Loading...




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