0% found this document useful (0 votes)
84 views

Control Flow Exception Handling & Assertion 2

The document contains questions and answers about Java exception handling and control flow. It discusses the behavior of try/catch blocks and the order in which exceptions from a class hierarchy would be caught. In one question, a program throws a Level1Exception, which would be caught by the third catch block for Level1Exception, incrementing variable d, while the finally block increments f.

Uploaded by

rahul rastogi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

Control Flow Exception Handling & Assertion 2

The document contains questions and answers about Java exception handling and control flow. It discusses the behavior of try/catch blocks and the order in which exceptions from a class hierarchy would be caught. In one question, a program throws a Level1Exception, which would be caught by the third catch block for Level1Exception, incrementing variable d, while the finally block increments f.

Uploaded by

rahul rastogi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 15

Rahul Rastogi: www.faqworldofrastogi.wetpaint.

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");}
}}

What is the result of attempting to compile and run the program?

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");}
}}

What is the result of attempting to compile and run the program?

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);
}}

What is the result of attempting to compile and run the program?

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");}
}}

What is the result of attempting to compile and run the program?

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);
}}

What is the result of attempting to compile and run the program?

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);
}}

What is the result of attempting to compile and run the program?

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);
}}

What is the result of attempting to compile and run the program?


Rahul Rastogi: www.faqworldofrastogi.wetpaint.com
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:
The first catch block is able to catch a Level3Exception or any subclass of
Level3Exception. The second catch block is able to catch a
Prints: Level2Exception or any subclass of Level2Exception. The switch
e
0,0,1,0,1 statement throws a Level2Exception. The try block completes abruptly as
control passes to the second catch block where c is incremented. The finally
block is also executed, so f is incremented.

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);
}}

What is the result of attempting to compile and run the program?

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);
}}

What is the result of attempting to compile and run the program?

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();
}}

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.
Answer:
With assertions enabled it prints an
The assert statement indicates that the
AssertionError message. With assertions
a programmer believes that b1 and b2 will
disabled it prints nothing. The assert statement is
d never be true simultaneously, and the
being used to check a control-flow invariant to
f assert statement should not be reached
verify that the control flow never reaches a point in
under normal operating conditions.
the program.

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);
}}

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.
Answer:
Rahul Rastogi: www.faqworldofrastogi.wetpaint.com
If assertions are enabled at run time
Assertions may be enabled or disabled at run time. Since
it prints an error message. With
assertions are not always enabled, they should not be used
assertions disabled it prints 1112.
to validate the parameters of public methods. Parameter
The assert statements are being checking is typically published in the API specification of
a used to check a precondition-- a method and must be enforced even when assertions are
d something that must be true when not enabled. Rather than use an assertion, an appropriate
f the method is invoked. Method m2 runtime exception should be thrown such as
h is an example of an improper use of IllegalArgumentException,
an assert statement: an assert
IndexOutOfBoundsException, or
statement should not be used for
NullPointerException. However, an assertion may
argument checking in a public
be used to validate the parameters of a nonpublic method.
method.

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);
}}

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 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);
}}

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.
Answer:
If assertions are not enabled at run time This question is an example of using assertions to
it prints nothing. With assertions check a class invariant--something that must be true
b
enabled it prints nothing. The assert about each instance of the class. Although a class
d
statement is being used to check a class invariant must be true before and after the execution
e
invariant--something that must be true of each public method, the invariant is typically only
about each instance of the class. checked at the end of each method and constructor.

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);
}}

Which statements are true?

a. With assertions enabled it prints an error message.


b. With assertions enabled it prints: true,true,false
c. With assertions disabled it prints an error message.
d. With assertions disabled it prints: true,true,false
e. With assertions disabled it prints nothing.
f. The combination of the if/else statements and the assert statement indicate that the
programmer expects no more than one boolean, b1, b2 or b3, to be true.
Rahul Rastogi: www.faqworldofrastogi.wetpaint.com
g. The assert statement is being used to check a precondition--something that must be true when
the method begins.
h. The assert statement is being used to check an internal invariant--something that the
programmer assumes to be true at a particular point in the program.
Answer:
Method m1 has a series of if/else statements.
The first if statement is processed if none of
the booleans are true. The second is processed
With assertions enabled it prints an error if only b1 is true. The third is processed if only
message. With assertions disabled it prints: b2 is true. A set of three booleans can exist is
true,true,false The combination of the if/else eight states. The three if statements account
a statements and the assert statement indicate for three of those states; so five more states
d that the programmer expects no more than one remain. The assert statement indicates that the
f boolean, b1, b2 or b3, to be true. The assert programmer assumes that only one of those
h statement is being used to check an internal five remaining states is valid--that is the state
invariant--something that the programmer where only b3 is true. The combination of the
assumes to be true at a particular point in the three if statements and the assert statement
program. indicate that the programmer believes that no
more than one of the booleans will be true at
that point in the program. That assumption is
called an internal invariant.

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));
}}

Which statements are true?

a. Prints "Only b1 is true" followed by an error message.


b. An assertion should not be placed at any location that the programmer believes will never be
reached under normal operating conditions.
c. The combination of the if/else statements and the assert statement indicate that the
programmer expects no more than one boolean, b1, b2, or b3, to be true.
d. The assert statement is being used to check a precondition--something that must be true when
the method begins.
e. 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.
Rahul Rastogi: www.faqworldofrastogi.wetpaint.com
f. The throw statement could be replaced by an assert statement.
Answer:
Method m1 has a series of if/else statements. The
first if statement is processed if none of the
booleans are true. The second is processed if only
b1 is true. The third is processed if only b2 is true.
The fourth is processed if only b3 is true. A set of
three booleans can exist in one of eight states. The
first four if statements account for four of those
states; so four more states remain. The combination
of the three if statements and the fact that an
AssertionError is thrown from the last else
block indicates that the programmer believes that
no more than one of the booleans will be true when
Prints "Only b1 is true" followed by an
method m1 is being processed. An assumption
error message. The combination of the
concerning the state of a set of variables is called
if/else statements and the assert statement
an internal invariant. In this case, however, the
a indicate that the programmer expects no
assertion was tested by verifying that control never
c more than one boolean, b1, b2, or b3, to be
reached a particular point in the program. Based on
e true. The assert statement is being used to
the testing technique, we would say that the
check a control-flow invariant to verify
assertion tests a control-flow invariant. A throw
that the control flow never reaches a point
statement is used in place of an assert statement,
in the program.
because the throw statement can not be disabled.
As a result, the method is certain to generate an
error once control passes beyond all of the return
statements. The declared return type of method m1
is String. No return statement appears after the
sequence of if statements; therefore, every if
statement must either return a String or throw an
exception. Assertions can be disabled at run time,
so an assert statement in the final if block is no
guarantee that an exception will be thrown. For that
reason, an assert can not replace the throw
statement.

Question 17
An assert statement can be used to check a control-flow invariant to verify which of the
following?

a. A particular assumption is true when the flow of control enters a method.


b. The flow of control does not reach a particular point in the program.
c. The normal flow of control has reached a particular point in the program.
d. The normal flow of control has reached the end of a method.
e. The default case of a switch statement is not reached.
f. The else block of an if/else statement is not reached.
Answer:
b The flow of control does not reach A control-flow invariant is placed at a point in the program
e a particular point in the program. that the programmer assumes will never be reached. Two
f The default case of a switch examples are the default case of a switch statement or the
statement is not reached. The else block of an if/else statement. It makes no sense to use
Rahul Rastogi: www.faqworldofrastogi.wetpaint.com
an assert statement to verify that the flow of control does
reach a particular point in the program, because it is
unlikely that an assertion error is helpful when the program
is found to be functioning correctly. An assert statement
placed at the beginning of a method is generally used to
else block of an if/else statement
check a precondition. An assert statement that is placed at
is not reached.
the end of a method to check the state of some variables is
generally said to be checking a post condition. However, it
is also possible that an assert statement placed at the end of
a method might also be checking a control-flow invariant.
The correct term depends on the usage.

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);
}}

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 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);
}}

What is the result of attempting to compile and run the program?

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);
}}

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 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).

You might also like