Chapter 5 Control Statements: Part 2 Q3
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?
Anything >= 4.
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.
Both (a) and (b) are true.
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 )
C and D.
Which of the following will NOT HELP PREVENT infinite loops?
INCLUDE BRACES around the statements in a do...while statement.
Which of the following is NOT a type of REPETITION statement in Java?
LOOP statement.
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
Neither (a) nor (b) is true
Which statement below is FALSE? Structured programminG
Structured programming requires 4 forms of CONTROL
Which statement prints the floating-point value 123.456 right justified with a field width of 10?
System.out.printf( "%10.3f", 123.456 );
Which case of the following would warrant using the boolean logical inclusive OR (|) rather than the conditional OR (||)?
Testing if at least one of two conditions is TRUE when the RIGHT operand has a required side effect
Which of the following statements about a do...while repetition statement is TRUE? while loop
The body of a DO ... WHILE loop is always executed AT LEAST ONCE
Which of the following statements about the break statement is FALSE?
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.
Suppose variable gender is MALE and age equals 60, how is the expression ( gender == FEMALE ) && ( age >= 65 ) evaluated?
The condition ( gender == FEMALE ) is evaluated first and the evaluation STOPS IMMEDIATELY
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?
The output for Segment A is: 5 default
Which FORMATTING FLAG indicates that the floating-point values should be OUTput with a thousands separator?
comma (,).
Which of the following will COUNT DOWN from 10 to 1 correctly?
for ( int j = 10; j >= 1; j-- )
Consider the code segment below. if ( gender == 1 ) { if ( age >= 65 ) ++seniorFemales; } // end if
if ( gender == 1 && age >= 65 ) ++seniorFemales;
Which expression is equivalent to if ( ! ( grade == sentinelValue ) )?
if ( grade != sentinelValue ).
while ( counter <= 10 ) { System.out.printf( "%d ", counter); ++counter; }
while ( ++counter <= 10) // loop continuation condition System.out.printf( "%d ", counter );
The boolean values can be displayed with the ________ format specifier.
%b.
To EXIT out of a loop completely, and resume the flow of control at the next line in the method, use _______.
A BREAK statement
Which of the following statements about the CONTINUE statement is TRUE?
A continue statement PROCEEDS with the NEXT iteration of the immediately enclosing while, for, do...while statement
Counter-Controlled repetition requires
All of the above.
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.
All.
Which of the following statements about the switch statement (as used in Java SE 7) is FALSE?
You can use a COMMA-separated list of Strings in a switch statement's case label.
Which of the following is equivalent to this code segment? int total = 0; for ( int i = 0; i <= 20; i += 2 ) total += i;
int total = 0; for ( int i = 0; i <= 20; total += i, i += 2 )
The CONTROL variable of a counter-controlled loop should be declared as ________to PREVENT ERRORS.
int.
The 1ST statement in every PAINTComponent method should be a call to ________.
super.paintComponent
Method drawOval's arguments specify ________.
the POSITION and SIZE of the bounding rectangle for the oval.
