Hi all,
Next week I am going to write
SCJP exam.I saw some mock exam in assertions , it shows the following questions. It looks like out of syllabus(objectives).In K&B book assertions topic I didn't see any
word like "invariant" or "precondition".Should I go that much deep in assertions?Please anyboby confirm me.Is it enough to study K&B book?.Advance Thanks.
The questions are,
class D {
private boolean b1, b2;
public void setB1(boolean b) {b1 = b;}
public void setB2(boolean b) {b2 = b;}
public void m1 () {
if (!b2 & !b1) {System.out.print("A");
} else if (!b2 & b1) {System.out.print("B");
} else if (b2 & !b1) {System.out.print("C");
} else {assert false;}
}
public static void main (
String[] args) {
D d = new D();
d.setB1(true); d.setB2(true);
d.m1();
}}
Which statements are true?
a. With assertions enabled it prints an AssertionError message.
b. With assertions enabled it prints nothing.
c. With assertions disabled it prints an AssertionError message.
d. With assertions disabled it prints nothing.
e. An assertion should not be placed at any location that the programmer believes will never be reached under normal operating conditions.
f. The assert statement is being used to check a control-flow invariant to verify that the control flow never reaches a point in the program.
--------------------------------------------------------------------------------
Question 15
class A {
private void m1 (int i) {
assert i < 10 : i; System.out.print(i);
}
public void m2 (int i) {
assert i < 10 : i; System.out.print(i);
}
public static void main (String[] args) {
A a = new A(); a.m1(11); a.m2(12);
}}
Which statements are true?
a. If assertions are enabled at run time it prints an error message.
b. With assertions enabled it prints nothing.
c. With assertions disabled it prints an error message.
d. With assertions disabled it prints 1112.
e. With assertions disabled it prints nothing.
f. The assert statements are being used to check a precondition--something that must be true when the method is invoked.
g. Method m1 is an example of an improper use of an assert statement: an assert statement should not be used for argument checking in a non-public method.
h. Method m2 is an example of an improper use of an assert statement: an assert statement should not be used for argument checking in a public method.