Chapter 3
Which of the following is equivalent to x != y?
a. !(x==y) c. x >y || x < y
Which of the Boolean expressions below is incorrect?
a. (true) && (3 => 4) d. (x!=0)||(x=0) e. (-10<x<0)
What is y after the following statement is executed? x = 0; y = (x > 0) ? 10 : -10;
a. -10
Analyze the following code: if (x < 100) && (x > 10) System.out.println("x is between 10 and 100");
a. the statement has compile errors because (x<100) & (x > 10) must be enclosed inside parentheses.
What is 1 + 1 + 1 + 1 + 1 == 5?
a. true
Which of the following is the correct expression that evaluates to true if the number x is between 1 and 100 or the number is negative?
b. ((x<100) && (x>1) || (x<0)
Suppose x = 10 and y = 10. What is x after evaluating the expression (y >= 10) || (x++ > 10)
b. 10
What is y after the following switch statement is executed? int x = 3; int y = 4; switch (x + 3) { case6: y=0; case7: y=1; default: y += 1; }
b. 2
The "less than or equal to" comparison operator in Java is __________.
b. <=
In Java, the word true is ________.
b. a Boolean literal
What is the output of the following code? boolean even = false; System.out.println((even ? "true" : "false"));
b. false
Suppose income is 4001, what is the output of the following code: if (income > 3000) { System.out.println("Income is greater than 3000"); } else if (income > 4000) { System.out.println("Income is greater than 4000");
b. income is greater than 3000
Analyze the following code: boolean even = false; if (even) { System.out.println("It is even!"); }
b. the code displays nothing
Suppose x = 1, y = -1, and z = 1. What is the printout 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");
b. x < 0 and z > 0;
Assume x = 4 and y = 5, Which of the following is true?
b. x < 5 || y < 5
The equal comparison operator in Java is __________.
c. ==
The __________ method immediately terminates the program.
c. System.exit(0);
What is 1 - 1.0 - 1.0 - 1.0 - 1.0 - 1.0 == 5.0?
c. There is no guarantee that 1 - 1.0 - 1.0 - 1.0 - 1.0 - 1.0 == 5.0 is true.
Suppose isPrime is a boolean variable, which of the following is the correct and best statement for testing if isPrime is true.
c. if (isPrime)
The following code displays ___________. double temperature = 50; if (temperature >= 100) System.out.println("too hot"); else if (temperature <= 40) System.out.println("too cold"); else System.out.println("just right");
c. just right
Analyze the following program fragment: int x; double d = 1.5; switch (d) { case 1.0: x = 1; case 1.5: x = 2; case 2.0: x = 3; }
c. the switch control variable cannot be double
Analyze the following code: Code 1: int number = 45; boolean even; if (number % 2 == 0) even = true; else even = false; Code 2: boolean even = (number % 2 == 0);
d. Both code 1 and code 2 are correct, but code 2 is better.
Analyze the following code: Boolean even = false; if (even = true) { System.out.println("It is even!"); }
d. the program runs fine and displays It is even!