Chapter 3 - Selections Flashcards
What does &&, ||, and ! mean in boolean expressions?
&& means AND
|| means OR
! means NOT
What 3 types of selection statements are there?
if-else statements, switch-statements, and conditional expressions
What types can be in a switch expression?
char, byte, short, int, or String
What happens if you omit a break statement in a case?
The program will continue to the next case, instead of exiting the switch construct
What is 1.0 + 1.0 + 1.0 + 1.0 + 1.0 == 5.0?
A. true
B. false
C. other
C. other.
There is no guarantee that 1.0 + 1.0 + 1.0 + 1.0 + 1.0 == 5.0 is true, becuase floating-point numbers are approximated.
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, but displays nothing.
D. The program runs and displays It is even!
The correct answer is D
Explanation: 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 prints ‘It is even!’.
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. Both Code 1 and Code 2 are correct, but Code 2 is better.
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 are incorrect.
Explanation: a: (3 => 4) should be (3 >= 4), d: (x = 0) should be (x == 0), and e: should be (-10 < x) && (x < 0)
Suppose x=10 and y=10 what is x after evaluating the expression (y > 10) && (x++ > 10).
A. 9
B. 10
C. 11
B. 10
Explanation: For the && operator, the right operand is not evaluated, if the left operand is evaluated as false.
Suppose x=10 and y=10 what is x after evaluating the expression (y >= 10) || (x++ > 10).
A. 9
B. 10
C. 11
B. 10
Explanation: For the || operator, the right operand is not evaluated, if the left operand is evaluated as true.
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. 2
Case 6 is invoked first, but since there’s no break statement in case 6, all the other cases are executed as well.
What will be displayed by the following switch statement?
char ch = 'a';
switch (ch) { case 'a': case 'A': System.out.print(ch); break; case 'b': case 'B': System.out.print(ch); break; case 'c': case 'C': System.out.print(ch); break; case 'd': case 'D': System.out.print(ch); }
A. abcd B. a C. aa D. ab E. abc
B. a
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. The switch control variable cannot be double.
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. All three are correct, but Code 3 is preferred.
E. All three are correct, but Code 3 is preferred.
Explanation: Code 3 is the simplest. Code 1 and Code 2 contain redundant code.
What is the syntax of conditional expressions?
boolean-expr ? expr1 : expr2;