Page 183 - Introduction to Programming with Java: A Problem Solving Approach
P. 183
Review Question Solutions
1. Java’s Boolean values are true and false.
2. Java’s comparison operators are:
==, !=, <, >, <=, >=
3. Use an “if, else if ” statement, like this: if (temp < 120)
{
}
{
}
heater = "off";
heater = "on";
else if (temp > 140)
Do not include a final else.
4. There is no limit on the number of “else if ” blocks that are allowed.
5. False. The arithmetic operators have higher precedence than the comparison operators.
6. The corrections are underlined:
Apago PDF Enhancer
System.out.print("OK\n");
(a == 2 || a == 3)
Review Question Solutions 149
{
}
7. The ! operator reverses the truth or falsity of a condition.
8. If you omit the break, control flows into the next case block, and that case block’s statements execute
also.
9. False. An “if, else” condition evaluates to either true or false. The controlling expression in a switch
statement must evaluate to either int or char (or byte or short).
10. When more than one identifier produces the same result, concatenate on the same line, if possible, using
separatecase <identifier>:foreachidentifier: case 'Q': case 'q':
System.out.println("quitting");
11. A while condition evaluates to either true or false.
12. The user query should occur just prior to where the termination condition is tested. A while loop tests the termination condition at the beginning of the loop. Therefore, the user query should occur just above the top of the loop and also just above the bottom of the loop. If you want the loop to always execute at least once, then omit the user query above the loop and replace it with an assignment that forces the termination condition to be true.
13. There is no semicolon after the while condition.
14. If you know ahead of time the exact number of iterations through a loop, use a for loop.