Deck 5: Control Statements: Part 2

Full screen (f)
exit full mode
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-- )
Use Space or
up arrow
down arrow
to flip the card.
Question
Which of the following statements about a do…while repetition 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 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 (but only in Java SE 7).
D)an enumeration constant.

A)A and B.
B)A and C.
C)B and C.
D)All.
Question
Consider the classes below: public class TestA
{
Public static void main( String args[] )
{
Int x = 2,y = 20,counter = 0;
For ( int j = y % x;j < 100;j += ( y / x ))
Counter++;
} // end main
} // end class TestA
Public class TestB
{
Public static void main(String args[])
{
Int counter = 0;
For ( int j = 10;j > 0;--j )
++counter;
} // end main
} // end class TestB
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
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
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
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
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" );
} // end switch
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" );
} // end switch
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
Suppose variable gender is 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
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" );
} // end switch
Which of the following values for q will result in kiwi being included in the output?

A)2.
B)Anything greater than or equal to 4.
C)1.
D)3.
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 );
Question
Which of the following for-loop control 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
Which of the following statements about the switch statement (as used in Java SE 7)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.
Question
Counter-controlled repetition 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
Consider the code segment below. if ( gender == 1 )
{
If ( age >= 65 )
++seniorFemales;
} // end if
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 statements about the continue statement is true?

A)The continue statement is used to exit a repetition 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
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
To exit out of a loop completely,and resume the flow of control at the next line in the method,use _______.

A)A continue statement.
B)A break statement.
C)A return statement.
D)Any of the above.
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 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.
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
The boolean values can be displayed with the ________ format specifier.

A)%bool.
B)%b.
C)%true.
D)%boolean.
Question
Which of the following is not a type of repetition statement in Java?

A)while statement.
B)do…while statement.
C)for statement.
D)loop statement.
Question
The first statement in every paintComponent method should be a call to ________.

A)super
B)super.paintComponent
C)clear
D)update
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/25
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 5: Control Statements: Part 2
1
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-- )
D
2
Which of the following statements about a do…while repetition 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 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 (but only in Java SE 7).
D)an enumeration constant.

A)A and B.
B)A and C.
C)B and C.
D)All.
D
4
Consider the classes below: public class TestA
{
Public static void main( String args[] )
{
Int x = 2,y = 20,counter = 0;
For ( int j = y % x;j < 100;j += ( y / x ))
Counter++;
} // end main
} // end class TestA
Public class TestB
{
Public static void main(String args[])
{
Int counter = 0;
For ( int j = 10;j > 0;--j )
++counter;
} // end main
} // end class TestB
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 25 flashcards in this deck.
Unlock Deck
k this deck
5
Which formatting flag indicates that the floating-point values should be output with a thousands separator?

A)plus (+).
B)minus (-).
C)comma (,).
D)period (. ).
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
6
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 25 flashcards in this deck.
Unlock Deck
k this deck
7
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 25 flashcards in this deck.
Unlock Deck
k this deck
8
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" );
} // end switch
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" );
} // end switch
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 25 flashcards in this deck.
Unlock Deck
k this deck
9
Suppose variable gender is 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 25 flashcards in this deck.
Unlock Deck
k this deck
10
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" );
} // end switch
Which of the following values for q will result in kiwi being included in the output?

A)2.
B)Anything greater than or equal to 4.
C)1.
D)3.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
11
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 );
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
12
Which of the following for-loop control 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 25 flashcards in this deck.
Unlock Deck
k this deck
13
Which of the following statements about the switch statement (as used in Java SE 7)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.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
14
Counter-controlled repetition 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 25 flashcards in this deck.
Unlock Deck
k this deck
15
Consider the code segment below. if ( gender == 1 )
{
If ( age >= 65 )
++seniorFemales;
} // end if
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 25 flashcards in this deck.
Unlock Deck
k this deck
16
Which of the following statements about the continue statement is true?

A)The continue statement is used to exit a repetition 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 25 flashcards in this deck.
Unlock Deck
k this deck
17
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 25 flashcards in this deck.
Unlock Deck
k this deck
18
To exit out of a loop completely,and resume the flow of control at the next line in the method,use _______.

A)A continue statement.
B)A break statement.
C)A return statement.
D)Any of the above.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
19
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 25 flashcards in this deck.
Unlock Deck
k this deck
20
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 25 flashcards in this deck.
Unlock Deck
k this deck
21
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 25 flashcards in this deck.
Unlock Deck
k this deck
22
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 25 flashcards in this deck.
Unlock Deck
k this deck
23
The boolean values can be displayed with the ________ format specifier.

A)%bool.
B)%b.
C)%true.
D)%boolean.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
24
Which of the following is not a type of repetition statement in Java?

A)while statement.
B)do…while statement.
C)for statement.
D)loop statement.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
25
The first statement in every paintComponent method should be a call to ________.

A)super
B)super.paintComponent
C)clear
D)update
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 25 flashcards in this deck.