Chapter 3 Selections
16. What is the output from System.out.println((int)Math.random() * 4)? a. 0 b. 1 c. 2 d. 3 e. 4
a Casting is performed before the * operator in (int)Math.random() * 4. So, it returns 0.
(3.2 Boolean Data Type. ) What is 1 + 1 + 1 + 1 + 1 == 5? a. true b. false c. There is no guarantee that 1 + 1 + 1 + 1 + 1 == 5 is true.
a These are all integers. Integer arithmetic is accurate.
Section 3.8 Case Study: Computing Body Mass Index 18. Suppose you write the code to display "Cannot get a driver's license" if age is less than 16 and "Can get a driver's license" if age is greater than or equal to 16. Which of the following code is correct? I: if (age < 16) System.out.println("Cannot get a driver's license"); if (age >= 16) System.out.println("Can get a driver's license"); II: if (age < 16) System.out.println("Cannot get a driver's license"); else System.out.println("Can get a driver's license"); III: if (age < 16) System.out.println("Cannot get a driver's license"); else if (age >= 16) System.out.println("Can get a driver's license"); IV: if (age < 16) System.out.println("Cannot get a driver's license"); else if (age > 16) System.out.println("Can get a driver's license"); else if (age == 16) System.out.println("Can get a driver's license"); a. I b. II c. III d. IV
abcd All the statements are correct. II is the best.
17. What is the possible output from System.out.println((int)(Math.random() * 4))? a. 0 b. 1 c. 2 d. 3 e. 4
abcd Math.random() returns a real value between 0.0 and 1.0, excluding 1.0. Math.random() * 4 yields a real value between 0.0 and 4.0, excluding 4.0. After casting, the resulting integer may be 0, 1, 2, or 3.
(3.2 Boolean Data Type. ) The "less than or equal to" comparison operator in Java is __________. a. < b. <= c. =< d. << e. !=
b It reads less than or equal to. So write the less than symbol before the equal sign. Note that there is no space separating the two symbols.
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).
b Since even is false, the if statement body is not executed. So, the correct answer is B.
Analyze the following code: Code 1: int number = 45; boolean even; if (number % 2 == 0) even = true; else even = false; Code 2: int number = 45; boolean even = (number % 2 == 0); a. Code 1 has compile errors b. Code 2 has compile errors. c. Both Code 1 and Code 2 have compile errors. d. Both Code 1 and Code 2 are correct, but Code 2 is better. Key:d Both Code 1 and Code 2 are correct. Clearly Code 2 is shorter and better.
Key:d Both Code 1 and Code 2 are correct. Clearly Code 2 is shorter and better.
(Section 3.4 Two-Way if-else Statements) 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 Since income is 4001, the condition (income > 3000) is true. So statement for the true case is executed.
(Section 3.3 if Statements) What is the output of the following code? int x = 0; if (x < 4) { x = x + 1; } System.out.println("x is " + x); a. x is 0 b. x is 1 c. x is 2 d. x is 3 e. x is 4
b Since x is 0 before the if statement, x < 4 is true, x becomes 1 after the statement x = x + 1. The correct answer is B
(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.
b You may copy the code to an IDE such as NetBeans or Eclipse and reformat it to see how it is correctly indented. The else clause matches the most recent if clause. So, it actually displays x < 0 and z > 0.
(3.2 Boolean Data Type. ) In Java, the word true is ________. a. a Java keyword b. a Boolean literal c. same as value 1 d. same as value 0
b true is a Boolean literal just like integer literal 10.
(Section 3.7 Generating Random Numbers 15. Which of the following is a possible output from invoking Math.random()? a. 3.43 b. 0.5 c. 0.0 d. 1.0
bc Math.random() returns a real value between 0.0 and 1.0, excluding 1.0.
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)
c A and D are incorrect. B, C, and E are correct. But C is the simplest and thus the best.
(3.2 Boolean Data Type. ) 2. The equal comparison operator in Java is __________. a. <> b. != c. == d. ^=
c Note that there is no space separating the double equal signs.
(Section 3.3 if Statements) Which of the following code displays the area of a circle if the radius is positive. a. if (radius != 0) System.out.println(radius * radius * 3.14159); b. if (radius >= 0) System.out.println(radius * radius * 3.14159); c. if (radius > 0) System.out.println(radius * radius * 3.14159); d. if (radius <= 0) System.out.println(radius * radius * 3.14159);
c Positive means > 0
(Section 3.5 Nested if and Multi-Way if-else Statements ) 9. 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"); a. too hot b. too cold c. just right d. too hot too cold just right
c The statement first test if (temperature >= 100). It is false. Then it tests if (temperature <= 4). It is false. So, it falls to the last else clause. The correct answer is C.
(3.2 Boolean Data Type. ) What is 1 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1 == 0.5? a. true b. false c. There is no guarantee that 1 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1 == 0.5 is true.
c This expression involves floating-point number. Floating-point numbers are approximated. The correct answer is C.
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.
d It is a common mistake to use the = operator in the condition test. What happens is that true is assigned to even when you write even = true. So even is true. The program compiles and runs fine and displays "It is even".