Control Flow Exception Handling & Assertion 2
Control Flow Exception Handling & Assertion 2
com
Control Flow Exception Handling & Assertion
SET►2
Question 1
class JMM107 {
public static void main(String[] args) {
boolean b = true;
if (b = false) {System.out.print("A");
} else if (b) {System.out.print("B");
} else {System.out.print("C");}
}}
a. Prints: A
b. Prints: B
c. Prints: C
d. Run-time error
e. Compile-time error
f. None of the above
Answer:
The boolean type variable, b, is initialized to true. The boolean expression of the first
if statement, b = false, is a simple assignment expression that sets b to false.
Prints:
c Always look carefully at the boolean expression of an if statement to verify that the
C
expected equality operator (==) has not been replaced by the simple assignment
operator (=).
Question 2
class ColorException extends Exception {}
class WhiteException extends ColorException {}
class White {
void m1() throws ColorException {throw new WhiteException();}
void m2() throws WhiteException {}
public static void main (String[] args) {
White white = new White();
int a,b,d,f; a = b = d = f = 0;
try {white.m1(); a++;} catch (ColorException e) {b++;}
try {white.m2(); d++;} catch (WhiteException e) {f++;}
System.out.print(a+","+b+","+d+","+f);
}}
What is the result of attempting to compile and run the program?
a. Prints: 0,1,0,0
b. Prints: 1,1,0,0
c. Prints: 0,1,1,0
d. Prints: 1,1,1,0
e. Prints: 1,1,1,1
f. Compile-time error
g. Run-time error
h. None of the above
Rahul Rastogi: www.faqworldofrastogi.wetpaint.com
Answer:
The first try block contains two statements. The first invokes method m1, and the
subsequent statement contains a post increment expression with the variable, a, as the
operand. Method m1 throws a WhiteException exception, so variable a is not
incremented as control passes to the catch block where b is incremented. The
throws clause of m1 declares a ColorException, so the body may throw a
Prints:
c ColorException or any subclass of ColorException. The second try block
0,1,1,0
also contains two statements. The first invokes method m2, and the subsequent
statement contains a post increment expression with the variable, d, as the operand.
Method m2 does not throw an exception, so d is incremented, and the try block
completes normally. Although the throws clause of m2 declares a
WhiteException, there is no requirement to throw any exception.
Question 3
class JMM108 {
static boolean b;
public static void main(String[] args) {
if (b) {System.out.print("A");
} else if (b = false) {System.out.print("B");
} else if (b) {System.out.print("C");
} else if (!b) {System.out.print("D");
} else {System.out.print("E");}
}}
a. Prints: A
b. Prints: B
c. Prints: C
d. Prints: D
e. Prints: E
f. Run-time error
g. Compile-time error
h. None of the above
Answer:
The expression, b = false, appears to be testing the value of b, but it is really
Prints: setting the value of b. Always look carefully at the boolean expression of an if
d
D statement to verify that the expected equality operator (==) has not been replaced by
the simple assignment operator (=).
Question 4
class ColorException extends Exception {}
class WhiteException extends ColorException {}
class White {
void m1() throws ColorException {throw new ColorException();}
void m2() throws WhiteException {throw new ColorException();}
public static void main (String[] args) {
White white = new White();
int a,b,d,f; a = b = d = f = 0;
try {white.m1(); a++;} catch (ColorException e) {b++;}
try {white.m2(); d++;} catch (WhiteException e) {f++;}
Rahul Rastogi: www.faqworldofrastogi.wetpaint.com
System.out.print(a+","+b+","+d+","+f);
}}
a. Prints: 0,1,0,0
b. Prints: 1,1,0,1
c. Prints: 0,1,0,1
d. Prints: 0,1,1,1
e. Prints: 1,1,1,1
f. Compile-time error
g. Run-time error
h. None of the above
Answer:
The throws clause of White.m2 declares a WhiteException, so the body of
Compile- m2 may throw a WhiteException or any subclass of WhiteException.
f
time error Instead, the body of m2 throws a superclass of WhiteException. The result is a
compile-time error.
Question 5
class JMM109 {
public static void main(String[] args) {
boolean b;
if (b = false) {System.out.print("A");
} else if (b) {System.out.print("B");
} else if (!b) {System.out.print("C");
} else {System.out.print("D");}
}}
a. Prints: A
b. Prints: B
c. Prints: C
d. Prints: D
e. Run-time error
f. Compile-time error
g. None of the above
Answer:
The first if statement initializes the value of b. The expression, b = false, appears
Prints: to be testing the value of b, but it is really setting the value of b. Always look carefully
c
C at the boolean expression of an if statement to verify that the expected equality
operator (==) has not been replaced by the simple assignment operator (=).
Question 6
class ColorException extends Exception {}
class WhiteException extends ColorException {}
class White {
void m1() throws ColorException {throw new ColorException();}
void m2() throws WhiteException {throw new WhiteException();}
Rahul Rastogi: www.faqworldofrastogi.wetpaint.com
public static void main (String[] args) {
White white = new White();
int a,b,d,f; a = b = d = f = 0;
try {white.m1(); a++;} catch (WhiteException e) {b++;}
try {white.m2(); d++;} catch (WhiteException e) {f++;}
System.out.print(a+","+b+","+d+","+f);
}}
a. Prints: 0,1,0,0
b. Prints: 1,1,0,1
c. Prints: 0,1,0,1
d. Prints: 0,1,1,1
e. Prints: 1,1,1,1
f. Compile-time error
g. Run-time error
h. None of the above
Answer:
The throws clause of White.m1 declares a ColorException, but the
Compile-
f catch clause in the main method catches only a subclass of
time error
ColorException. The result is a compile-time error.
Question 7
class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Brown {
public static void main(String args[]) {
int a, b, c, d, f; a = b = c = d = f = 0;
int x = 1;
try {
switch (x) {
case 1: throw new Level1Exception();
case 2: throw new Level2Exception();
case 3: throw new Level3Exception();
} a++; }
catch (Level3Exception e) {b++;}
catch (Level2Exception e) {c++;}
catch (Level1Exception e) {d++;}
finally {f++;}
System.out.print(a+","+b+","+c+","+d+","+f);
}}
a. Prints: 0,0,0,1,1
b. Prints: 0,0,1,1,1
c. Prints: 0,1,1,1,1
d. Prints: 1,1,1,1,1
e. Prints: 0,0,1,0,1
f. Prints: 0,1,0,0,1
g. Prints: 1,0,0,0,1
h. Compile-time error
Rahul Rastogi: www.faqworldofrastogi.wetpaint.com
i. Run-time error
j. None of the above
Answer:
The first catch clause has a parameter e of type Level3Exception, so the
first catch clause is able to catch any exception type that is assignable to type
Level3Exception. Since Level2Exception is the superclass of
Level3Exception, an instance of Level2Exception is not assignable to a
catch clause parameter of type Level3Exception. Similarly,
Level1Exception is also a superclass of Level3Exception, so an instance
of Level1Exception is not assignable to a catch clause parameter of type
Level3Exception. The only exception type that can be caught by the first
catch clause is a Level3Exception. The second catch clause has a
parameter e of type Level2Exception, so the second catch clause is able to
catch a Level2Exception. The Level1Exception is the superclass of
Level2Exception. An instance of Level1Exception is not assignable to a
Prints: catch clause parameter of type Level2Exception, so the second catch
a
0,0,0,1,1 clause can not catch a Level1Exception. Since a Level3Exception is a
subclass of Level2Exception an exception of type Level3Exception is
assignable to a catch clause parameter type Level2Exception. All exceptions
of type Level3Exception will be caught by the first catch clause, so the
second catch clause in this program will not have an opportunity to catch a
Level3Exception. The third catch clause has a parameter e of type
Level1Exception, so the third catch clause is able to catch a
Level1Exception. The exceptions of type Level2Exception and
Level3Exception are assignable to the catch clause parameter of the third
catch clause, but the exceptions of those subclass types will be caught by the first
two catch clauses. The switch statement throws a Level1Exception. The
try block completes abruptly as control passes to the third catch block where d
is incremented. The finally block is also executed, so f is incremented.
Question 8
class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Brown {
public static void main(String args[]) {
int a, b, c, d, f; a = b = c = d = f = 0;
int x = 2;
try {
switch (x) {
case 1: throw new Level1Exception();
case 2: throw new Level2Exception();
case 3: throw new Level3Exception();
} a++; }
catch (Level3Exception e) {b++;}
catch (Level2Exception e) {c++;}
catch (Level1Exception e) {d++;}
finally {f++;}
System.out.print(a+","+b+","+c+","+d+","+f);
}}
Question 9
class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Brown {
public static void main(String args[]) {
int a, b, c, d, f; a = b = c = d = f = 0;
int x = 4;
try {
switch (x) {
case 1: throw new Level1Exception();
case 2: throw new Level2Exception();
case 3: throw new Level3Exception();
} a++; }
catch (Level3Exception e) {b++;}
catch (Level2Exception e) {c++;}
catch (Level1Exception e) {d++;}
finally {f++;}
System.out.print(a+","+b+","+c+","+d+","+f);
}}
a. Prints: 0,0,0,1,1
b. Prints: 0,0,1,1,1
c. Prints: 0,1,1,1,1
d. Prints: 1,1,1,1,1
e. Prints: 0,0,1,0,1
f. Prints: 0,1,0,0,1
g. Prints: 1,0,0,0,1
h. Compile-time error
i. Run-time error
j. None of the above
Answer:
Rahul Rastogi: www.faqworldofrastogi.wetpaint.com
The switch statement does not throw an exception; so the switch completes
Prints:
g normally. The subsequent statement increments the variable, a; and the try block
1,0,0,0,1
completes normally. The finally block is also executed, so f is incremented.
Question 10
class ColorException extends Exception {}
class WhiteException extends ColorException {}
abstract class Color {
abstract void m1() throws ColorException;
}
class White extends Color {
void m1() throws WhiteException {throw new WhiteException();}
public static void main (String[] args) {
White white = new White();
int a,b,c; a = b = c = 0;
try {white.m1(); a++;}
catch (WhiteException e) {b++;}
finally {c++;}
System.out.print(a+","+b+","+c);
}}
a. Prints: 0,0,0
b. Prints: 0,0,1
c. Prints: 0,1,0
d. Prints: 0,1,1
e. Prints: 1,0,0
f. Prints: 1,0,1
g. Prints: 1,1,0
h. Prints: 1,1,1
i. Compile-time error
j. Run-time error
k. None of the above
Answer:
The try block contains two statements. The first invokes method m1, and the
subsequent statement contains a post increment expression with the variable, a, as the
operand. Method m1 throws a WhiteException exception, so variable a is not
Prints:
d incremented as control passes to the catch block where b is incremented. Although
0,1,1
Color.m1 declares a ColorException in the throws clause, a subclass of
Color is free to declare only a subclass of ColorException in the throws
clause of the overriding method.
Question 11
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");
Rahul Rastogi: www.faqworldofrastogi.wetpaint.com
} 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();
}}
Question 12
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);
}}
Question 13
class B {
int a, b, c;
private void setA(int i) {a = i;}
private void setB(int i) {b = i;}
private int m1 (int i) {
c = a + b + i; assert c < 200 : c; return c;
}
public static void main (String[] args) {
B b = new B(); b.setA(50); b.setB(100); b.m1(50);
}}
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 postcondition--something that must be true when
the method completes successfully.
Answer:
If assertions are not enabled at run time it prints nothing. With
b Variable c equals 200
assertions enabled it prints an error message. The assert statement is
c when the assertion is
being used to check a postcondition--something that must be true when
e checked.
the method completes successfully.
Question 14
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) {
Rahul Rastogi: www.faqworldofrastogi.wetpaint.com
c = i; assert validateC() : c;
}
public static void main(String[] args) {
C c = new C(251); c.setA(50); c.setB(100);
}}
Question 15
class E {
private boolean b1, b2, b3;
public void setB1(boolean b) {b1 = b;}
public void setB2(boolean b) {b2 = b;}
public void setB3(boolean b) {b3 = b;}
public void m1 (int i) {
b2 = i % 2 == 0;
if (!b3 & !b2 & !b1) {System.out.print("A");
} else if (!b3 & !b2 & b1) {System.out.print("B");
} else if (!b3 & b2 & !b1) {System.out.print("C");
} else { // Only b3 is true.
assert b3 & !b2 & !b1;
}
System.out.print(b1 + "," + b2 + "," + b3);
b1 = b2 = b3 = false;
}
public static void main (String[] args) {
E e = new E(); e.setB1(true); e.m1(2);
}}
Question 16
class F {
private boolean b1, b2, b3;
public void setB1(boolean b) {b1 = b;}
public void setB2(boolean b) {b2 = b;}
public void setB3(boolean b) {b3 = b;}
public String m1 (int i) {
b2 = i % 2 == 0;
if (!b3 & !b2 & !b1) {return "None are true.";
} else if (!b3 & !b2 & b1) {return "Only b1 is true.";
} else if (!b3 & b2 & !b1) {return "Only b2 is true.";
} else if (b3 & !b2 & !b1) {return "Only b3 is true.";
} else {throw new AssertionError();}
}
public static void main (String[] args) {
F f = new F();
f.setB1(true);
System.out.println(f.m1(5));
System.out.println(f.m1(6));
}}
Question 17
An assert statement can be used to check a control-flow invariant to verify which of the
following?
Question 18
class JMM110 {
public static void main (String[] args) {
int j = 0;
do for (int i = 0; i++ < 2;)
System.out.print(i);
while (j++ < 2);
}}
a. Prints: 0001
b. Prints: 012
c. Prints: 012012
d. Prints: 012345
e. Prints: 001122
f. Prints: 1112
g. Prints: 111222
h. Prints: 121212
i. Run-time error
j. Compile-time error
k. None of the above
Answer:
Prints: This trick question has a for loop nested inside of a do loop. For each iteration, the
h
121212 values of i and j are as follows: (1,0)(2,0)(1,1)(2,1)(1,2)(2,2).
Question 19
class JMM111 {
public static void main (String[] args) {
int j = 0;
for (int i = 0; i < 2; i++) do
System.out.print(i);
while (j++ < 2);
}}
a. Prints: 0001
Rahul Rastogi: www.faqworldofrastogi.wetpaint.com
b. Prints: 012
c. Prints: 012012
d. Prints: 012345
e. Prints: 001122
f. Prints: 1112
g. Prints: 111222
h. Prints: 121212
i. Run-time error
j. Compile-time error
k. None of the above
Answer:
Prints: This trick question has a do-loop nested inside of a for-loop. For each iteration, the
a
0001 values of i and j are as follows: (0,0)(0,1)(0,2)(1,3).
Question 20
class JMM112 {
public static void main (String[] args) {
int j = 0;
for (int i = 0; i++ < 2;) do
System.out.print(i);
while (j++ < 2);
}}
a. Prints: 0001
b. Prints: 012
c. Prints: 012012
d. Prints: 012345
e. Prints: 001122
f. Prints: 1112
g. Prints: 111222
h. Prints: 121212
i. Run-time error
j. Compile-time error
k. None of the above
Answer:
Prints: This trick question has a do-loop nested inside of a for-loop. For each iteration, the
f
1112 values of i and j are as follows: (1,0)(1,1)(1,2)(2,3).
Question 21
class JMM113 {
public static void main (String[] args) {
int i = 0, j = 0, k = 0;
do while (i++ < 3)
System.out.print(k++);
while (j++ < 3);
}}
Rahul Rastogi: www.faqworldofrastogi.wetpaint.com
What is the result of attempting to compile and run the program?
a. Prints: 0001
b. Prints: 012
c. Prints: 012012
d. Prints: 012345
e. Prints: 001122
f. Prints: 1112
g. Prints: 111222
h. Prints: 121212
i. Run-time error
j. Compile-time error
k. None of the above
Answer:
Prints: This trick question has a while-loop nested inside of a do-loop. For each iteration, the
b
012 values of i, j and k are as follows: (1,0,0)(2,0,1)(3,0,2).