Hi Everyone,
This is one of the Questions from the Dan's mock Exam.
Please go through the code...
class C {
int a, b, c;
public void setA(int i) {a = i; assert validateC() : c;}
public void setB(int i) {b = i; assert validateC() : c;}
private boolean validateC() {
return c > a + 2 * b;
}
public int m1(int i) {
c = a + b + i;
assert validateC() : c;
return c;
}
public C(int i) {
c = i; assert validateC() : c;
}
public static void main(
String[] args) {
C c = new C(251); c.setA(50); c.setB(100);
}}
Which statements are true?
a. If assertions are not enabled at run time it prints an error message.
b. If assertions are not enabled at run time it prints nothing.
c. With assertions enabled it prints an error message.
d. With assertions enabled it prints nothing.
e. The assert statement is being used to check a class invariant--something that must be true about each instance of the class.
f. The assert statements are being used to check a precondition--something that must be true when the method is invoked.
The answers given are b,d,e.
I did not understand what answer
e means. I don't know what invarinat means.
Also i am of the opinion that Assert statement should not be used with the parameters of a method.
Can any one explain me about the answers to this Question.
Thanks in Advance.