Deck 5: Control Statements: Logical Operators

Full screen (f)
exit full mode
Question
Which of the following is equivalent to this code segment?
int total = 0;
For (int i = 0; i <= 20; i += 2) {
Total += i;
}

A) int total = 0; for (int i = 20; i < 0; i += 1) {
Total += i;
}
B) int total = 0; for (int i = 0; i <= 20; total += i, i += 2) {}
C) int total = 0; for (int i = 0, i <= 20, total += i; i += 2) {}
D) int total = 0; for (int i = 2; i < 20; total += i, i += 2) {}
Use Space or
up arrow
down arrow
to flip the card.
Question
Which of the following statements about a do…while iteration statement is true?

A) The body of a do…while loop is executed only if the terminating condition is true.
B) The body of a do…while loop is executed only once.
C) The body of a do…while loop is always executed at least once.
D) None of the above.
Question
Which formatting flag indicates that the floating-point values should be output with a thousands separator?

A) plus (+).
B) minus (-).
C) comma (,).
D) period (.).
Question
Consider the following two Java code segments:
Segment 1 Segment 2
Int i = 0;
For (int i = 0; i <= 20; i++) {
While (i < 20) { System.out.println(i);
I++; }
System.out.println(i);
}
Which of the following statements are true?

A) The output from these segments is not the same.
B) The scope of the control variable i is different for the two segments.
C) Both (a) and (b) are true.
D) Neither (a) nor (b) is true.
Question
Which of the following statements about the switch statement is false?

A) You can use Strings in a switch statement's controlling expression.
B) You can use a String in a switch statement's case label.
C) You can use a comma-separated list of Strings in a switch statement's case label.
D) You cannot use a String in a switch statement's default case label.
Question
Which of the following statements about the continue statement is true?

A) The continue statement is used to exit an iteration structure early and continue execution after the loop.
B) The continue statement is used to continue after a switch statement.
C) The continue statement does not alter the flow of control.
D) A continue statement proceeds with the next iteration of the immediately enclosing while, for, do…while statement.
Question
For the two code segments below:
Segment A
Int q = 5;
Switch(q) {
Case 1:
System.out.println(1);
Case 2:
System.out.println(2);
Case 3:
System.out.println(3);
Case 4:
System.out.println(4);
Case 5:
System.out.println(5);
Default:
System.out.println("default");
}
Segment B
Q = 4;
Switch(q) {
Case 1:
System.out.println(1);
Case 2:
System.out.println(2);
Case 3:
System.out.println(3);
Case 4:
System.out.println(4);
Case 5:
System.out.println(5);
Default:
System.out.println("default");
}
Which of the following statements is true?

A) The output for Segment A is: default
B) The output for Segment B is: 4
C) The output for Segment B is: 45default
D) The output for Segment A is: 5
Default
Question
Consider the code segment below.
if (gender == 1) {
If (age >= 65) {
++seniorFemales;
}
}
This segment is equivalent to which of the following?

A) if (gender == 1 || age >= 65) { ++seniorFemales;
}
B) if (gender == 1 && age >= 65) { ++seniorFemales;
}
C) if (gender == 1 AND age >= 65) { ++seniorFemales;
}
D) if (gender == 1 OR age >= 65) { ++seniorFemales;
}
Question
Which of the following can be used in a switch statement in the expression after keyword case? A. a constant integral expression.
B) a character constant.
C) a String
D) an enumeration constant.

A) A and B.
B) A and C.
C) B and C.
D) All.
Question
Which of the following statements about the break statement is false?

A) The break statement is used to exit an iteration structure early and continue execution after the loop.
B) A break statement can only break out of an immediately enclosing while, for, do…while or switch statement.
C) The break statement, when executed in a while, for or do…while, skips the remaining statements in the loop body and proceeds with the next iteration of the loop.
D) Common uses of the break statement are to escape early from a loop or to skip the remainder of a switch.
Question
The control variable of a counter-controlled loop should be declared as ________to prevent errors.

A) int.
B) float.
C) double.
D) Any of the above.
Question
Which of the following will count down from 10 to 1 correctly?

A) for (int j = 10; j <= 1; j++)
B) for (int j = 1; j <= 10; j++)
C) for (int j = 10; j > 1; j--)
D) for (int j = 10; j >= 1; j--)
Question
Which of the following will not help prevent infinite loops?

A) Include braces around the statements in a do…while statement.
B) Ensure that the header of a for or while statement is not followed by a semicolon.
C) If the loop is counter-controlled, the body of the loop should increment or decrement the counter as needed.
D) If the loop is sentinel-controlled, ensure that the sentinel value is input eventually.
Question
Which of the following for-loop headers results in equivalent numbers of iterations: A. for (int q = 1; q <= 100; q++)
B) for (int q = 100; q >= 0; q--)
C) for (int q = 99; q > 0; q -= 9)
D) for (int q = 990; q > 0; q -= 90)

A) A and B.
B) C and D.
C) A and B have equivalent iterations and C and D have equivalent iterations.
D) None of the loops have equivalent iterations.
Question
For the code segment below:
switch(q) {
Case 1:
System.out.println("apple");
Break;
Case 2:
System.out.println("orange");
Break;
Case 3:
System.out.println("banana");
Break;
Case 4:
System.out.println("pear");
Case 5:
System.out.println("grapes");
Default:
System.out.println("kiwi");
}
Which of the following values for q will result in kiwi being included in the output?

A) 2.
B) Any integer less than 1 and greater than or equal to 4.
C) 1.
D) 3.
Question
Consider the classes below:
public class TestA {
Public static void main(String[] args) {
Int x = 2;
Int y = 20
Int counter = 0;
For (int j = y % x; j < 100; j += (y / x)) {
Counter++;
}
}
}
Public class TestB {
Public static void main(String[] args) {
Int counter = 0;
For (int j = 10; j > 0; --j) {
++counter;
}
}
}
Which of the following statements is true?

A) The value of counter will be different at the end of each for loop for each class.
B) The value of j will be the same for each loop for all iterations
C) Both (a) and (b) are true.
D) Neither (a) nor (b) is true.
Question
To exit out of a loop completely, and resume the flow of control at the next statement after the loop, use a _______.

A) continue statement.
B) break statement.
C) return statement.
D) Any of the above.
Question
Which of the following statements is true?

A) Strings can be used in a switch statement's controlling expression and in its case labels.
B) Strings can be used in a switch statement's controlling expression but not in its case labels.
C) Strings cannot be used in a switch statement's controlling expression but can be used in its case labels.
D) Strings cannot be used in a switch statement's controlling expression and cannot be used in its case labels.
Question
Which statement prints the floating-point value 123.456 right justified with a field width of 10?

A) System.out.printf("%d10.3", 123.456);
B) System.out.printf("%10.3d", 123.456);
C) System.out.printf("%f10.3", 123.456);
D) System.out.printf("%10.3f", 123.456);
Question
Counter-controlled iteration requires

A) A control variable and initial value.
B) A control variable increment (or decrement).
C) A condition that tests for the final value of the control variable.
D) All of the above.
Question
Suppose variable gender contains MALE and age equals 60, how is the expression (gender == FEMALE) && (age >= 65)
Evaluated?

A) The condition (gender == FEMALE) is evaluated first and the evaluation stops immediately.
B) The condition (age >= 65) is evaluated first and the evaluation stops immediately.
C) Both conditions are evaluated, from left to right.
D) Both conditions are evaluated, from right to left.
Question
Boolean values can be displayed as the words true and false with the ________ format specifier.

A) %bool.
B) %b.
C) %true.
D) %boolean.
Question
Which expression is equivalent to if (!(grade == sentinelValue))?

A) if (grade !== sentinelValue)
B) if (grade != sentinelValue)
C) ! if (grade == sentinelValue)
D) ! if (grade !== sentinelValue)
Question
Which statement below is false?

A) Structured programming produces programs that are easier to test.
B) Structured programming requires four forms of control.
C) Structured programming produces programs that are easier to modify
D) Structured programming promotes simplicity.
Question
Which of the following is not a type of iteration statement in Java?

A) while statement.
B) do…while statement.
C) for statement.
D) loop statement.
Question
Which case of the following would warrant using the boolean logical inclusive OR (|) rather than the conditional OR (||)?

A) Testing if two conditions are both true.
B) Testing if at least one of two conditions is true.
C) Testing if at least one of two conditions is true when the right operand has a required side effect.
D) Testing if at least one of two conditions is true when the left operand has a required side effect.
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/26
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 5: Control Statements: Logical Operators
1
Which of the following is equivalent to this code segment?
int total = 0;
For (int i = 0; i <= 20; i += 2) {
Total += i;
}

A) int total = 0; for (int i = 20; i < 0; i += 1) {
Total += i;
}
B) int total = 0; for (int i = 0; i <= 20; total += i, i += 2) {}
C) int total = 0; for (int i = 0, i <= 20, total += i; i += 2) {}
D) int total = 0; for (int i = 2; i < 20; total += i, i += 2) {}
int total = 0; for (int i = 0; i <= 20; total += i, i += 2) {}
2
Which of the following statements about a do…while iteration statement is true?

A) The body of a do…while loop is executed only if the terminating condition is true.
B) The body of a do…while loop is executed only once.
C) The body of a do…while loop is always executed at least once.
D) None of the above.
C
3
Which formatting flag indicates that the floating-point values should be output with a thousands separator?

A) plus (+).
B) minus (-).
C) comma (,).
D) period (.).
C
4
Consider the following two Java code segments:
Segment 1 Segment 2
Int i = 0;
For (int i = 0; i <= 20; i++) {
While (i < 20) { System.out.println(i);
I++; }
System.out.println(i);
}
Which of the following statements are true?

A) The output from these segments is not the same.
B) The scope of the control variable i is different for the two segments.
C) Both (a) and (b) are true.
D) Neither (a) nor (b) is true.
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
5
Which of the following statements about the switch statement is false?

A) You can use Strings in a switch statement's controlling expression.
B) You can use a String in a switch statement's case label.
C) You can use a comma-separated list of Strings in a switch statement's case label.
D) You cannot use a String in a switch statement's default case label.
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
6
Which of the following statements about the continue statement is true?

A) The continue statement is used to exit an iteration structure early and continue execution after the loop.
B) The continue statement is used to continue after a switch statement.
C) The continue statement does not alter the flow of control.
D) A continue statement proceeds with the next iteration of the immediately enclosing while, for, do…while statement.
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
7
For the two code segments below:
Segment A
Int q = 5;
Switch(q) {
Case 1:
System.out.println(1);
Case 2:
System.out.println(2);
Case 3:
System.out.println(3);
Case 4:
System.out.println(4);
Case 5:
System.out.println(5);
Default:
System.out.println("default");
}
Segment B
Q = 4;
Switch(q) {
Case 1:
System.out.println(1);
Case 2:
System.out.println(2);
Case 3:
System.out.println(3);
Case 4:
System.out.println(4);
Case 5:
System.out.println(5);
Default:
System.out.println("default");
}
Which of the following statements is true?

A) The output for Segment A is: default
B) The output for Segment B is: 4
C) The output for Segment B is: 45default
D) The output for Segment A is: 5
Default
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
8
Consider the code segment below.
if (gender == 1) {
If (age >= 65) {
++seniorFemales;
}
}
This segment is equivalent to which of the following?

A) if (gender == 1 || age >= 65) { ++seniorFemales;
}
B) if (gender == 1 && age >= 65) { ++seniorFemales;
}
C) if (gender == 1 AND age >= 65) { ++seniorFemales;
}
D) if (gender == 1 OR age >= 65) { ++seniorFemales;
}
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
9
Which of the following can be used in a switch statement in the expression after keyword case? A. a constant integral expression.
B) a character constant.
C) a String
D) an enumeration constant.

A) A and B.
B) A and C.
C) B and C.
D) All.
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
10
Which of the following statements about the break statement is false?

A) The break statement is used to exit an iteration structure early and continue execution after the loop.
B) A break statement can only break out of an immediately enclosing while, for, do…while or switch statement.
C) The break statement, when executed in a while, for or do…while, skips the remaining statements in the loop body and proceeds with the next iteration of the loop.
D) Common uses of the break statement are to escape early from a loop or to skip the remainder of a switch.
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
11
The control variable of a counter-controlled loop should be declared as ________to prevent errors.

A) int.
B) float.
C) double.
D) Any of the above.
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
12
Which of the following will count down from 10 to 1 correctly?

A) for (int j = 10; j <= 1; j++)
B) for (int j = 1; j <= 10; j++)
C) for (int j = 10; j > 1; j--)
D) for (int j = 10; j >= 1; j--)
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
13
Which of the following will not help prevent infinite loops?

A) Include braces around the statements in a do…while statement.
B) Ensure that the header of a for or while statement is not followed by a semicolon.
C) If the loop is counter-controlled, the body of the loop should increment or decrement the counter as needed.
D) If the loop is sentinel-controlled, ensure that the sentinel value is input eventually.
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
14
Which of the following for-loop headers results in equivalent numbers of iterations: A. for (int q = 1; q <= 100; q++)
B) for (int q = 100; q >= 0; q--)
C) for (int q = 99; q > 0; q -= 9)
D) for (int q = 990; q > 0; q -= 90)

A) A and B.
B) C and D.
C) A and B have equivalent iterations and C and D have equivalent iterations.
D) None of the loops have equivalent iterations.
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
15
For the code segment below:
switch(q) {
Case 1:
System.out.println("apple");
Break;
Case 2:
System.out.println("orange");
Break;
Case 3:
System.out.println("banana");
Break;
Case 4:
System.out.println("pear");
Case 5:
System.out.println("grapes");
Default:
System.out.println("kiwi");
}
Which of the following values for q will result in kiwi being included in the output?

A) 2.
B) Any integer less than 1 and greater than or equal to 4.
C) 1.
D) 3.
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
16
Consider the classes below:
public class TestA {
Public static void main(String[] args) {
Int x = 2;
Int y = 20
Int counter = 0;
For (int j = y % x; j < 100; j += (y / x)) {
Counter++;
}
}
}
Public class TestB {
Public static void main(String[] args) {
Int counter = 0;
For (int j = 10; j > 0; --j) {
++counter;
}
}
}
Which of the following statements is true?

A) The value of counter will be different at the end of each for loop for each class.
B) The value of j will be the same for each loop for all iterations
C) Both (a) and (b) are true.
D) Neither (a) nor (b) is true.
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
17
To exit out of a loop completely, and resume the flow of control at the next statement after the loop, use a _______.

A) continue statement.
B) break statement.
C) return statement.
D) Any of the above.
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
18
Which of the following statements is true?

A) Strings can be used in a switch statement's controlling expression and in its case labels.
B) Strings can be used in a switch statement's controlling expression but not in its case labels.
C) Strings cannot be used in a switch statement's controlling expression but can be used in its case labels.
D) Strings cannot be used in a switch statement's controlling expression and cannot be used in its case labels.
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
19
Which statement prints the floating-point value 123.456 right justified with a field width of 10?

A) System.out.printf("%d10.3", 123.456);
B) System.out.printf("%10.3d", 123.456);
C) System.out.printf("%f10.3", 123.456);
D) System.out.printf("%10.3f", 123.456);
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
20
Counter-controlled iteration requires

A) A control variable and initial value.
B) A control variable increment (or decrement).
C) A condition that tests for the final value of the control variable.
D) All of the above.
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
21
Suppose variable gender contains MALE and age equals 60, how is the expression (gender == FEMALE) && (age >= 65)
Evaluated?

A) The condition (gender == FEMALE) is evaluated first and the evaluation stops immediately.
B) The condition (age >= 65) is evaluated first and the evaluation stops immediately.
C) Both conditions are evaluated, from left to right.
D) Both conditions are evaluated, from right to left.
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
22
Boolean values can be displayed as the words true and false with the ________ format specifier.

A) %bool.
B) %b.
C) %true.
D) %boolean.
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
23
Which expression is equivalent to if (!(grade == sentinelValue))?

A) if (grade !== sentinelValue)
B) if (grade != sentinelValue)
C) ! if (grade == sentinelValue)
D) ! if (grade !== sentinelValue)
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
24
Which statement below is false?

A) Structured programming produces programs that are easier to test.
B) Structured programming requires four forms of control.
C) Structured programming produces programs that are easier to modify
D) Structured programming promotes simplicity.
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
25
Which of the following is not a type of iteration statement in Java?

A) while statement.
B) do…while statement.
C) for statement.
D) loop statement.
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
26
Which case of the following would warrant using the boolean logical inclusive OR (|) rather than the conditional OR (||)?

A) Testing if two conditions are both true.
B) Testing if at least one of two conditions is true.
C) Testing if at least one of two conditions is true when the right operand has a required side effect.
D) Testing if at least one of two conditions is true when the left operand has a required side effect.
Unlock Deck
Unlock for access to all 26 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 26 flashcards in this deck.