Section 3.6 Common Errors and Pitfalls
10. Suppose x = 1, y = –1, and z = 1. What is the output of the following statement? (Please indent the statement
correctly first.)
if (x > 0)
if (y > 0)
System.out.println(“x > 0 and y > 0″);
else if (z > 0)
System.out.println(“x < 0 and z > 0″);
a. x > 0 and y > 0;
b. x < 0 and z > 0;
c. x < 0 and z < 0;
d. no output.
#
11. Analyze the following code:
boolean even = false;
if (even = true) {
System.out.println(“It is even”);
}
a. The program has a compile error.
b. The program has a runtime error.
c. The program runs fine, but displays nothing.
d. The program runs fine and displays It is even.
#
12. Suppose isPrime is a boolean variable, which of the following is the correct and best statement for testing if
isPrime is true?
a. if (isPrime = true)
b. if (isPrime == true)
c. if (isPrime)
d. if (!isPrime = false)
e. if (!isPrime == false)
#
13. Analyze the following code.
boolean even = false;
if (even) {
System.out.println(“It is even!”);
}
a. The code displays It is even!
b. The code displays nothing.
c. The code is wrong. You should replace if (even) with if (even == true).
d. The code is wrong. You should replace if (even) with if (even = true).