Deck 6: Flow of Control, Part 2: Looping
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/67
Play
Full screen (f)
Deck 6: Flow of Control, Part 2: Looping
1
Why is the first read called the priming read?
A) The first read is the most important one.
B) Like priming a pump, the first read feeds the condition of the while loop for the first iteration.
C) If the first read returns a true block, it is the only read needed.
D) All of these are correct.
A) The first read is the most important one.
B) Like priming a pump, the first read feeds the condition of the while loop for the first iteration.
C) If the first read returns a true block, it is the only read needed.
D) All of these are correct.
B
2
How many times will a loop with the for loop header for ( i = 5; i < 15; i++ ) execute?
A) 1
B) 5
C) 10
D) 11
E) 15
A) 1
B) 5
C) 10
D) 11
E) 15
C
3
How many times will a loop with the for loop header for ( i = 5; i <= 15; i++ ) execute?
A) 1
B) 5
C) 10
D) 11
E) 15
A) 1
B) 5
C) 10
D) 11
E) 15
D
4
Using the for loop header for( int i = 0; i < 5; i++ ), the variable i will still be in scope after we exit the for loop.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
5
A while loop can be nested inside a for loop and vice versa.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
6
If the while loop condition is false the first time it is evaluated, the number of iterations that will be in the body of the loop is __________.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
7
When you write a program that expects an input value within a certain range only, you should not forget to test the __________ case.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
8
The while loop is used to process each input value, one at a time, until a signal tells us that there is no more input. This is called:
A) event-controlled looping.
B) either/or looping.
C) sentinel looping.
D) loop updating.
A) event-controlled looping.
B) either/or looping.
C) sentinel looping.
D) loop updating.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
9
What is each execution of the loop body called?
A) An integer
B) An iteration
C) An endless loop
D) An infinite loop
A) An integer
B) An iteration
C) An endless loop
D) An infinite loop
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
10
The while loop condition is a boolean expression.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
11
A sentinel value in a while loop provides a function similar to which of the following?
A) A child holding a kitten
B) A speed limit sign on an interstate
C) A divider bar on a checkout lane's conveyor belt
D) A quicksand trap in a desert
A) A child holding a kitten
B) A speed limit sign on an interstate
C) A divider bar on a checkout lane's conveyor belt
D) A quicksand trap in a desert
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
12
Which of the following arguments constructs a File object with the pathname file name so that the file name is platform independent?
A) hasNext( )
B) nextDouble( )
C) next( )
D) None of these is correct.
A) hasNext( )
B) nextDouble( )
C) next( )
D) None of these is correct.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
13
What is the argument list for the nextFloat( ) method in the Scanner class?
A) an int
B) a Float
C) Returns true if there is another token in the input stream; false, otherwise
D) None of these is correct.
A) an int
B) a Float
C) Returns true if there is another token in the input stream; false, otherwise
D) None of these is correct.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
14
Without the priming read, the while loop continually processes the same data item, leading to an endless loop.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
15
The hasNext method in the Scanner class returns true if the input has another token, and false otherwise.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
16
Using an event-controlled while loop and an accumulation operation to create a calculator, a result that is higher than the correct total occurs because the total variable was not initialized to 0.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
17
A logic error is likely to happen if we set the sentinel value -1 in an addition calculator.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
18
Calculating an average is a combination of addition and counting.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
19
To get a floating-point average, type cast one of the variables (either total or count) to a double or a float to force the division to be performed as floating-point. double average = (double) ( total ) / count;
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
20
It is a good idea to initialize the maximum value to 0 when trying to find maximum values.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
21
Some code is already coded with a for loop. It is possible to rewrite that code using a while loop.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
22
An InputMismatchException is generated while using Scanner. What can you conclude?
A) The user made a typo.
B) The user misunderstood and entered the wrong data.
C) The user typed "one" instead of "1."
D) Any of these are possible.
A) The user made a typo.
B) The user misunderstood and entered the wrong data.
C) The user typed "one" instead of "1."
D) Any of these are possible.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
23
Which of the following questions should you ask to verify a clean program?
A) Does the program produce correct results with a set of known inputs?
B) Does the program produce correct results if the sentinel value is the first and only input?
C) Does the program deal appropriately with invalid input?
D) All of these are required tests.
A) Does the program produce correct results with a set of known inputs?
B) Does the program produce correct results if the sentinel value is the first and only input?
C) Does the program deal appropriately with invalid input?
D) All of these are required tests.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
24
In the do/while loop, the loop condition is tested at the end of the loop, instead of at the beginning, as in the while loop.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
25
Using an if statement to validate input will find all errors without reprompting.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
26
Before the loop begins, if you know the number of times the loop body should execute, you can use a:
A) for loop.
B) toggle loop.
C) do loop.
D) get next loop.
A) for loop.
B) toggle loop.
C) do loop.
D) get next loop.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
27
If you want to loop between the values 3 included and 7 excluded, the for loop header would be:
for( int count = 3; count < 7; count++ )
for( int count = 3; count < 7; count++ )
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
28
You have a for loop nested in a while loop in order to find prime numbers. The proper way is to set the flag to false before starting the for loop that checks for factors. Inside the for loop, set the flag to true when a factor is found. After the for loop terminates, check the value of the flag. If it is still false, no factors were found and the number is prime.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
29
while loops can be nested.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
30
for loops can be nested.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
31
Which Java class enables us to read data easily from a file?
A) IO
B) Scanner
C) Read
D) File
A) IO
B) Scanner
C) Read
D) File
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
32
How many times will a loop with the for loop header for ( i = 0; i <= 6; i++ ) execute?
A) 1
B) 5
C) 6
D) 7
A) 1
B) 5
C) 6
D) 7
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
33
How many times will a loop with the for loop header for ( i = 0; i < 6; i++ ) execute?
A) 1
B) 5
C) 6
D) 7
A) 1
B) 5
C) 6
D) 7
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
34
When you use a while loop to count how many values meet a certain condition, you should initialize the variable holding the count to:
A) 0.
B) 1.
C) NULL.
D) There is no need to initialize it.
A) 0.
B) 1.
C) NULL.
D) There is no need to initialize it.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
35
When you use a while loop to compute the average of several values, what variables do you need to keep track of (in addition to holding the value being processed)?
A) The sum of the values
B) How many values have been processed
C) None
D) The sum of the values and how many values have been processed
A) The sum of the values
B) How many values have been processed
C) None
D) The sum of the values and how many values have been processed
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
36
When a loop condition is grade >= 90, what values would you use to test your loop?
A) 88 and 89
B) 0 and 90
C) 89, 90, and 91
D) 90, 91, and 92
A) 88 and 89
B) 0 and 90
C) 89, 90, and 91
D) 90, 91, and 92
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
37
When you use a sentinel value for the condition of a loop that processes user inputs, what case should you not forget to test?
A) The sentinel value is the last input value.
B) The sentinel value is the first input value.
C) The sentinel value is the third input value.
A) The sentinel value is the last input value.
B) The sentinel value is the first input value.
C) The sentinel value is the third input value.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
38
The loop condition is a boolean expression.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
39
When asking the user for an integer value, it is possible that the user enters a character that is not an integer; using a while loop, we can keep asking the user for an integer until the user enters one.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
40
The loop condition can be a complex condition involving several logical, equality, and relational operators.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
41
You should always put a semicolon after the closing parenthesis in a for loop header.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
42
In a for loop, a loop control variable is usually, but not always, for counting.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
43
Putting a semicolon after the loop condition of a while loop will most likely result in a(n) __________ or endless loop.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
44
When reading data from a user, the first time we read is called the __________.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
45
When you use a while loop to compute the sum of several values, you should initialize the variables holding the sum to:
A) 0.
B) 1.
C) NULL.
D) There is no need to initialize them.
A) 0.
B) 1.
C) NULL.
D) There is no need to initialize them.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
46
When you use a while loop to compute the product of several values, you should initialize the variables holding the product to:
A) 0.
B) 1.
C) NULL.
D) There is no need to initialize them.
A) 0.
B) 1.
C) NULL.
D) There is no need to initialize them.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
47
In a for loop header, statements are separated with:
A) colons.
B) commas.
C) semicolons.
D) periods.
A) colons.
B) commas.
C) semicolons.
D) periods.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
48
Assuming the for loop header for ( i = 0; i <= 7; i++ ), what will be value of i after we exit the loop?
A) 0
B) 6
C) 7
D) 8
A) 0
B) 6
C) 7
D) 8
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
49
Assuming the for loop header for ( i = 0; i < 20; i++ ), what will be value of i after we exit the loop?
A) 0
B) 19
C) 20
D) 21
A) 0
B) 19
C) 20
D) 21
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
50
How many times will a loop with the for loop header for ( i = 1; i <= 6; i++ ) execute?
A) 1
B) 5
C) 6
D) 7
A) 1
B) 5
C) 6
D) 7
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
51
A special value that the user enters to signal the end of the input is called a(n) __________ value.
A) input
B) user
C) sentinel
D) looping
A) input
B) user
C) sentinel
D) looping
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
52
When reading data from a file using a while loop, the typical condition to exit the loop is when you:
A) have read the first value in the file.
B) have read 100 values in the file.
C) have reached the end of the file.
A) have read the first value in the file.
B) have read 100 values in the file.
C) have reached the end of the file.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
53
When we use a while loop, we always know in advance how many times the loop body will be executed.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
54
Curly braces around the loop body are optional if the loop body consists of only one statement.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
55
Curly braces around a for loop body are optional if the for loop body consists of only one statement.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
56
A while loop can be used for counting input values that have a certain property-for instance, positive values.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
57
When computing the maximum value among a number of values, we should initialize the maximum to 0.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
58
The body of a while loop is executed until the loop condition becomes __________.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
59
We typically use a for loop as a(n) __________ loop.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
60
Writing a String in reverse can be done using a(n) __________.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
61
The loop continuation condition is the ___________ of the loop termination condition.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
62
A loop condition may be simplified by applying __________ laws.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
63
Describe the conditions under which you would want to use a sentinel value.
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
64
Evaluate this code segment, explaining any errors:
12 int count = 1;
13 int total = 1;
14 final int SENTINEL = -1;
15 int score;
12 int count = 1;
13 int total = 1;
14 final int SENTINEL = -1;
15 int score;
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
65
When you use a while loop to compute the average of several integer values, and assuming there is more than one such value, what do you have to worry about when dividing the sum of these values by the number of these values?
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
66
When you use a while loop to compute the average of several values, what do you have to worry about when dividing the sum of these values by the number of these values?
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck
67
What is the output of this code sequence?
for ( int i = 5; i >= 0; i-- )
System.out.print( i + " " );
System.out.println( );
for ( int i = 5; i >= 0; i-- )
System.out.print( i + " " );
System.out.println( );
Unlock Deck
Unlock for access to all 67 flashcards in this deck.
Unlock Deck
k this deck