Chapter 5: Control Statements Part 2

¡Supera tus tareas y exámenes ahora con Quizwiz!

constant

A variable which contains a value which does not change for the entire program

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.

ANS: a. Include braces around the statements in a do...while statement.

Which of the following statements about the break statement is false? a. The break statement is used to exit a repetition 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.

ANS: a. The break statement is used to exit a repetition structure early and continue execution after the loop.

Suppose variable gender is MALE and age equals 60, how is the expression ( gender == FEMALE ) && ( age >= 65 ) evaluated?

ANS: a. The condition ( gender == FEMALE ) is evaluated first and the evaluation stops immediately.

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.

ANS: a. The continue statement is used to exit a repetition structure early and continue execution after the loop. b

The control variable of a counter-controlled loop should be declared as: a.int. b.float. c.double. d.Any of the above.

ANS: a. int.

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 output? a.2. b.4 and anything greater than 4. c.1. d.3.

ANS: b. 4 and anything greater than 4.

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.

ANS: b. A break statement.

Labeled break statements cannot be used to break out of which of the following? a.A while statement. b.A method. c.A for loop. d.A switch statement.

ANS: b. A method.

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

ANS: b. C and D

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;

ANS: b. if ( gender == 1 && age >= 65 ) ++seniorFemales;

Which expression is equivalent to if ( ! ( grade==sentinelValue ) )?

ANS: b. if ( grade != sentinelValue )

Which of the following is equivalent to the following code segment? Segment: int total = 0; for ( int i = 0; i <= 20; i += 2 ) total += i;

ANS: b. int total = 0; for ( int i = 0; i <= 20; total += i, i += 2 )

Consider the following two Java code segments: Segment 1 int i = 0; while ( i < 20 ) { i++; System.out.println( i ); } Segment 2 for ( int i = 0; i <= 20; 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.

ANS: c. Both (a) and (b) are true

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.

ANS: c. 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? 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

ANS: c. The body of a do...while loop is always executed at least once

Which formatting flag indicates that the floating-point values should be output with a thousands separator?

ANS: c. comma (,)

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

ANS: d. All

Which of the following statements is not true? A.A break statement can only break out of an immediately enclosing while, for, do...while or switch statement. B.Labeled break statements break out of any number of enclosing repetition structures. C.A continue statement proceeds with the next iteration of the immediately enclosing while, for, do...while statement. D.Labeled continue statements break out of any number of enclosing repetition structures, and continue execution with next iteration of the labeled repetition structure. a.A and C. b.B and D. c.None of the above are true. d.All of the above are true.

ANS: d. All of the above are true.

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

ANS: d. All of the above.

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++; } } } 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 the 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

ANS: d. Neither (a) nor (b) is true

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);

ANS: d. System.out.printf("%10.3f", 123.456);

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

ANS: d. The output for Segment A is: 5 default

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-- )

ANS: d. for ( int j = 10; j >= 1; j-- )

&

Boolean logical AND

|

Boolean logical inclusive OR

off-by-one error

Can be caused by the use of an incorrect relational operator or using an incorrect final value of a loop counter in the condition of a repetition statement

break

Causes immediate exit from a repetition statement

&&

Conditional AND

| |

Conditional OR

switch

Handles a series of decisions, in which a particular variable or expression is tested for values it can assume and different actions are taken

for repetition statement

Handles all of the details of counter-controlled repetition

!

Logical negation

do...while repetition statement

Repetition statement that tests the loop-continuation condition at the end of the loop, so that the body of the loop will execute at least once

continue

Skips any remaining statements in the body of a repetition statement and proceeds with the next iteration of the loop


Conjuntos de estudio relacionados

Lewis-Chapter 15 - Infection and Human Immunodeficiency Virus Infection

View Set

Chapter 27: Cerebral Dysfunction

View Set

11.1 - 11.5 Wide Area Networks (WANs)

View Set

Lippincott the child with health problems of the urinary system

View Set

Chapter 5- Derived from the text

View Set

Sociology - Chapter 4 Review Quiz - IWCC

View Set

Module 7a:Structuring and Storing Data

View Set

Chapter 21 Inkjet Printers, Impact Printers, and Thermal Printers

View Set

Module 2: Solubility, Absorption.

View Set