Chapter 5 Flashcards

1
Q

Typically, ______ statements are used for counter-controlled iteration and ______ statements for sentinel-controlled iteration.

A

for, while

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

The do…while statement tests the loop-continuation condition ______ executing the loop’s body; therefore, the body always executes at least once.

A

after

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

The _____ statement selects among multiple actions based on the possible values of an integer variable or expression, or a String.

A

switch

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The _____ statement, when executed in an iteration statement, skips the remaining statements in the loop body and proceeds with the next iteration of the loop.

A

continue

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

The _____ operator (with short-circuit evaluation) can be used to ensure that two conditions are both true before choosing a certain path of execution.

A

&& (conditional AND)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

If the loop-continuation condition in a for header is initially _____, the program does not execute the for statement’s body.

A

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Methods that perform common tasks and do not require objects are ______ methods.

A

static

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

(T or F) The default case is required in the switch selection statement.

A

False. The default case is optional. If no default action is needed, then there’s no need for a default case.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

(T or F) The break statement is required in the last case of a switch selection statement.

A

False. The break statement is used to exit the switch statement. The break statement is not required for the last case in a switch statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

(T or F) The expression ((x > y) && (a < b)) is true if either x > y is true or a < b is true.

A

False. Both of the relational expressions must be true for the entire expression to be true when using the && operator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

(T or F) An expression containing the || operator is true if either or both of its operands are true.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

(T or F) The comma (,) formatting flag in a format specifier (e.g., %,20.2f) indicates that a value should be output with a grouping separator.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

(T or F) To test for a range of values in a switch statement, use a hyphen (-) between the starts and end values of the range in a case label.

A

False. The switch statement does not provide a mechanism for testing ranges of values so every value that must be tested should be listed in a separate case label.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

(T or F) Listing cases consecutively with no statements between them enables the cases to perform the same set of statements.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
(Find the error) 
i = 1;
while (i <= 10);
      \++i;
}
A

Error: The semicolon after the while header causes an infinite loop, and there’s a missing left brace.
Correction: Replace the semicolon by a {, or remove both the ; and the }.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

(Find the error)
for (k = 0.1; k != 1.0; k += 0.1) {
System.out.println(k);
}

A

Error: Using a floating-point number to control a for statement may not work, because floating-point numbers are represented only approximately by computers.
Correction: Use an integer, and perform the proper calculation to get the values you desire as in:
for (k = 1; k != 10; k++) {
System.out.println((double) k / 10);
}