# Chapter 3 Quiz

## Unlock all answers in this set

question
The "less than or equal to" comparison operator in Java is ________
<=
question
The equal comparison operator in Java is _______
==
question
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
question
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 Since the expression involves floating-point numbers and floating-point numbers are approximated
question
In Java, the word true is _______
a Boolean literal
question
C
question
Which of the following is a possible output from invoking Math.random()? A. 3.43 B. 0.5 C. 0.0 D. 1.0
B and C Math.random( ) returns a real value between 0.0 and 1.0, excluding 1.0
question
What is the output from System.out.println((int)Math.random( ) * 4)? A. 0 B. 1 C. 2 D. 3 E. 4
A
question
What is the possible output from System.out.println((int)(Math.random( ) * 4))? A. 0 B. 1 C. 2 D. 3 E. 4
A, B, C, and D
question
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
question
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
question
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
question
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
question
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
question
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
D
question
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"); } A. no output B. Income is greater than 3000 C. Income is greater than 3000 followed by Income is greater than 4000 D. Income is greater than 4000 E. Income is greater than 4000 followed by Income is greater than 3000
B
question
The __________ method immediately terminates the program. A. System.terminate(0); B. System.halt(0); C. System.exit(0); D. System.quit(0); E. System.stop(0);
C
question
A, B, C, and D
question
B
question
Which of the Boolean expressions below is incorrect? A. (true) && (3 => 4) B. !(x > 0) && (x > 0) C. (x > 0) || (x < 0) D. (x != 0) || (x = 0) E. (-10 < x < 0)
A, D, and E
question
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? A. 1 < x < 100 && x < 0 B. ((x < 100) && (x > 1)) || (x < 0) C. ((x < 100) && (x > 1)) && (x < 0) D. (1 > x > 100) || (x < 0)
B
question
Assume x = 4 and y = 5, which of the following is true? A. x < 5 && y < 5 B. x < 5 || y < 5 C. x > 5 && y > 5 D. x > 5 || y > 5
B
question
Assume x = 4, which of the following is true? A. !(x == 4) B. x != 4 C. x == 5 D. x != 5
D
question
Assume x = 4 and y = 5, which of the following is true? A. !(x == 4) ^ y != 5 B. x != 4 ^ y == 5 C. x == 5 ^ y == 4 D. x != 5 ^ y != 4
B
question
Given |x - 2| <= 4, which of the following is true? A. x - 2 <= 4 && x - 2 >= 4 B. x - 2 <= 4 && x - 2 > -4 C. x - 2 <= 4 && x - 2 >= -4 D. x - 2 <= 4 || x - 2 >= -4
C
question
Given |x - 2| >= 4, which of the following is true? A. x - 2 >= 4 && x - 2 <= -4 B. x - 2 >= 4 || x - 2 <= -4 C. x - 2 >= 4 && x - 2 < -4 D. x - 2 >= 4 || x - 2 < -4
B
question
Which of the following is equivalent to x != y? A. ! (x == y) B. x > y && x < y C. x > y || x < y D. x >= y || x <= y
A and C
question
Suppose x=10 and y=10. What is x after evaluating the expression (y > 10) && (x-- > 10)? A. 9 B. 10 C. 11
B
question
Suppose x=10 and y=10. What is x after evaluating the expression (y > 10) && (x++ > 10). A. 9 B. 10 C. 11
B
question
Suppose x=10 and y=10. What is x after evaluating the expression (y >= 10) || (x-- > 10). A. 9 B. 10 C. 11
B
question
Suppose x=10 and y=10. What is x after evaluating the expression (y >= 10) || (x++ > 10). A. 9 B. 10 C. 11
B
question
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. B. The statement has compile errors because (x<100) & (x > 10) must be enclosed inside parentheses and the println(?) statement must be put inside a block. C. The statement compiles fine. D. The statement compiles fine, but has a runtime error
A
question
Which of the following are so called short-circuit operators? A. && B. & C. || D. |
A and C
question
What is y after the following switch statement is executed? int x = 3; int y = 4; switch (x + 3) { case 6: y = 0; case 7: y = 1; default: y += 1; } A. 1 B. 2 C. 3 D. 4 E. 0
B
question
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; } A. The program has a compile error because the required break statement is missing in the switch statement. B. The program has a compile error because the required default case is missing in the switch statement. C. The switch control variable cannot be double. D. No errors.
C
question
What is y after the following statement is executed? x = 0; y = (x > 0) ? 10 : -10; A. -10 B. 0 C. 10 D. 20 E. Illegal expression
A
question
Analyze the following code fragments that assign a boolean value to the variable even. Code 1: if (number % 2 == 0) even = true; else even = false; Code 2: even = (number % 2 == 0) ? true: false; Code 3: even = number % 2 == 0; A. Code 2 has a compile error, because you cannot have true and false literals in the conditional expression. B. Code 3 has a compile error, because you attempt to assign number to even. C. All three are correct, but Code 1 is preferred. D. All three are correct, but Code 2 is preferred. E. All three are correct, but Code 3 is preferred.
E
question
What is the output of the following code? boolean even = false; System.out.println((even ? "true" : "false")); A. true B. false C. nothing D. true false
B
question
The order of the precedence (from high to low) of the operators binary +, *, &&, ||, ^ is: A. &&, ||, ^, *, + B. *, +, &&, ||, ^ C. *, +, ^, &&, || D. *, +, ^, ||, && E. ^, ||, &&, *, +
C
question
What is y displayed in the following code? public class Test1 { public static void main(String[] args) { int x = 1; int y = x = x + 1; System.out.println("y is " + y); } } A. y is 0. B. y is 1 because x is assigned to y first. C. y is 2 because x + 1 is assigned to x and then x is assigned to y. D. The program has a compile error since x is redeclared in the statement int y = x = x + 1
C
question
Which of the following operators are right-associative. A. * B. + (binary +) C. % D. && E. =