L8 Practice Quiz: Ch 6 Repetition
The most appropriate sentinel value that might be selected for month of the year is ____. 0, 1, 6, 12
0
int loopVariable = 0; do { Console.WriteLine("Count = {0:}", ++loopVariable); }while (loopVariable < 5); What will be printed during the first iteration through the loop? 0, 1, 10, none of the above How many times will be loop body be executed? 0, 4, 5, 6 How many times will be loop body be executed if the conditional expression is changed to (loopVariable == 5)? 1, 4, 5, 6
1, 5, 1
int counter = 0; while (counter < 100) { Console.WriteLine(counter); counter++; } Looking above, if the loop body was changed from counter++; to counter += 5;, how many times would the loop body be executed? 19, 20, 99, 100 The last value printed with the above program segment is ____. 0, 1, 99, 100 The first value printed with the program segment above is ____. 0, 1, 99, 100
20, 99, 0
int inner; for (int outer = 0; outer < 3; outer++) { for (inner = 1; inner < 2; inner++) Console.WriteLine("Outer: {0}\tInner: {1}", outer, inner); } The nested for statement above displays how many lines of output? 2,3,5,6
3
for (int outer = 0; outer < 2; outer++) { for (int inner = 0; inner < 3; inner++) { Console.WriteLine("Outer: {0}\tInner: {1}", outer, inner); } } How many lines will be printed for the above nested loop? 2, 3, 5, 6
6
for (int outer = 0; outer < 2; outer++) { for (int inner = 0; inner < 3; inner++) { Console.WriteLine("Outer: {0}\tInner: {1}", outer, inner); } } During the last iteration through the nested loop, what numbers are printed? Outer: 1 Inner: 2 Outer: 2 Inner 3 Outer: 3 Inner 0 Outer: 1 Inner 0
Outer: 1 Inner: 2
____________ loops are often used for inputting data when you do not know the exact number of values to be entered.
Sentinel-controlled
The _____________ method of the MessageBox class displays a predefined dialog box that can contain text, captions for the title bar, special buttons, and icons.
Show( ) or Show
In order to use the MessageBox.Show( ) method in your console application, you must ____. a) enclose the method call in a body of a loop b) add a reference to the System.Windows.Forms.dll. c) add a using System.MessageBox directive d) call on the method from within the WriteLine( ) or Write( ) methods
add a reference to the System.Windows.Forms.dll.
With counter controlled loops, it is important to think through and ____________ to ensure they have been taken into consideration. You should always check the initial and final values of the loop control variable and verify that you get the expected results at the boundaries.
check endpoints
When you know the number of times the statements must be executed, create a(n) ____________ loop.
counter-controlled
The only posttest loop structure available in C# is _____. while for do...while foreach
do...while
The only posttest loop structure available with C# is the ____________ loop structure.
do...while or do
A state-controlled loop should be designed when you know the number of times the statements must be executed. true or false
false
Iteration is a technique used where a method calls itself repeatedly until it arrives at the solution. true or false
false
int sum = 0; int number = 0; while (number < 10) { sum = sum + number; Console.WriteLine(sum); } The statement above prints the values of 0 through 9 on separate lines. true or false
false
An example for statement that has a compound initialization, compound test and compound update could be written as ____. for (int r = 0; c = 5; r 2; r++; c--) for (int r = 0; c = 5; r 2; r++; c--) for (int r = 0; c = 5; r 2; r++, c--) for (int r = 0, c = 5; r 2; r++, c--)
for (int r = 0, c = 5; r 2; r++, c--)
The ___________ loop structure, which restricts access to read-only, is used to iterate or move through a collection, such as an array.
foreach
The loop control structure used to move through a collection (group of data items) that does not require you to increment a loop control variable or test the expression to determine when all data has been processed is the ____ statement. while for do...while foreach
foreach
The event-driven model used to create Windows applications ____. uses a counter-controlled form of loop uses a state-controlled form of loop uses a sentinel-controlled form of loop handles the repetition for you
handles the repetition for you
A(n) ____________ is a loop that has no provisions for termination.
infinite loop
A common problem associated with counter-controlled loops is not executing the loop for the last value. This type of problem is labeled a(n) ____ error. off-by-one last-value counter controlled boundaries
off-by-one
With the while loop, the body of the loop is ____. a) always performed at least one time b) performed as long as the conditional expression evaluates to true c) must produce a boolean result d) must be enclosed in curly braces
performed as long as the conditional expression evaluates to true
An option other than looping that can be used to repeat program statement is _____________. With this technique a method calls itself repeatedly until it arrives at the solution.
recursion
A method that calls itself repeatedly until it arrives at the solution is a(n) ____method. static recursive iterative instance
recursive
You must tell the user what value to type to end the loop for ____. counter-controlled loops state-controlled loops sentinel-controlled loops any type of loop
sentinel-controlled loops
Instead of requiring that a dummy value be entered after all values are processed, a ____-controlled loop initializes a variable on the outside of the loop and then evaluates the variable to see when it changes values. Its value is changed inside the loop body when the loop should exit. counter sentinel state change
state
A flag-controlled loop is also called a ____. sentinel-controlled loop counter-controlled loop state-controlled loop posttest form loop
state=controlled loop
In order to write a recursive solution ____. the simplest case should be determined you must create an infinite loop write the iterative solution first the most complicated approach must be identified
the simplest case should be determined
A restriction on using the foreach statement is that you cannot change values in the collection. The access to the elements is read-only. true or false
true
An interpretation of the while statement is "while the condition is true, perform statement(s)". true or false
true
An off-by-one error is a common problem associated with counter-controlled loops where the loop body is performed one too many or one too few times. true or false
true
The do...while loop can be used to implement a counter-controlled, sentinel-controlled, or state-controlled loop. true or false
true
The sentinel value is used as the operand in the conditional expression for an indefinite loop. true or false
true
To write a recursive solution, a base case needs to be identified. true or false
true
Unlike the sentinel-controlled loop, the variable used with a state-controlled loop differs from the variable used to store the data that is being processed. true or false
true
Using the break or continue statements with a loop violates the single entry and single exit guideline for developing a loop. true or false
true
Without incrementing the counter used in the conditional expression with counter controlled loops, the loop would go on indefinitely. true or false
true
Which of the following is NOT a keyword used to enable unconditional transfer of control to a different program statement? continue break goto while
while
What is NOT required when a counter-controlled loop is created? a) Initializing loop control variable on the outside of the loop. b) Incrementing the loop control variable inside the loop. c) A conditional expression involving the loop control variable. d) Allowing the user to enter a value indicating the loop should stop.
d) Allowing the user to enter a value indicating the loop should stop.
If a numeric variable is being changed by a consistent amount with each iteration through the loop, the while statement is the best choice of loop constructs. true or false
false
s = num.Next(7); The statement above produces seven random numbers. true or false
false
The ____________ statement is a pretest form of loop which is considered a specialized form of the while statement and is usually associated with counter-controlled types of loops;
for
The for statement is a compact method of writing the loops that can be written using while; it packages together the ____________, test, and update all on one line.
initialization
for (int i = 0; i < 10; i++) { Console.WriteLine("counter " + i); } The variable i defined in the program segment above ____. a) is out of scope when the loop terminates b) creates an error because it is changed in the update portion c) would have a value of 10 if it were printed on the outside of the loop d) can be displayed prior to the loop program statements
is out of scope when the loop terminates
The third programming construct repetition is also called ____________.
iteration or looping
When one of the program statement included within the body of a loop is another loop, a(n) ____________ loop is created.
nested
A sentinel controlled loop is also called a flag-controlled loop. true or false
true
for (int i = 0; i < 5; i++) Console.Write(i + "\t"); Given the code snippet above, how many times will the loop body be executed? 4, 5, 6, 10 The conditional expression used with the for statement ____. a) must involve a counter b) must evaluate to false in order for the loop body to be executed c) is evaluates after the loop body is performed once d) is written following the first semicolon
5 is written following the first semicolon
What is a requirement of the sentinel-controlled loop shared by counter-controlled loops? a) The conditional expression must be set up prior to the loop. b) A counter must be initialized on the outside of the loop. c) A counter must be incremented inside the loop body. d) The expression must evaluate to false in order for the loop to be executed.
The conditional expression must be set up prior to the loop.
The do...while statement is a posttest loop, which means that the conditional expression is tested before any of the statements in the body of the loop are performed. true or false
false
The while statement is the only type of loop construct that can be used to develop a sentinel-controlled loop. true or false
false
With nested loops, the outermost loop is always totally completed before the innermost loop is executed. true or false
false
With while loops, if the conditional expression evaluates to true, the body of the loop is not performed; control transfers to the statements following the loop. true or false
false
if (int.TryParse(inStringValue, out integerValue) == false) Console.WriteLine("Invalid input - 0 recorded for end value"); If a character such as "b" is entered by the user and stored in inStringValue, TryParse( ) returns ____________.
false
Which of the following is an advantage of a sentinel-controlled loop? The number of iterations does not have to be known. An extreme or dummy value can be entered. The loop is always performed at least one time. It could be used with the while form of the loop.
The number of iterations does not have to be known.
To "prime the read" with a loop, you place an input statement ____. inside the loop body before the loop body after the loop body as part of the loop conditional expression
before the loop body