COSC - Quiz 4
int loopVariable = 0; do { Console.WriteLine("Count = {0:}", ++loopVariable); }while (loopVariable < 5); How many times will be loop body be executed if the conditional expression is changed to (loopVariable == 5)? 0 --- Correct 4 5 6
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 --- Correct 10 none of the above
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 -- Correct 99 100
20
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 --- Correct 6 10
5
int counter = 0; while (counter < 100) { Console.WriteLine(counter); counter++; } The last value printed with the above program segment is ____. 0 1 99 --- Correct 100
99
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 -- Correct Outer: 2 Inner 3 Outer: 3 Inner 0 Outer: 1 Inner 0
Outer: 1 Inner: 2
What is a requirement of the sentinel-controlled loop shared by counter-controlled loops? The conditional expression must be set up prior to the loop. ---- Correct A counter must be initialized on the outside of the loop. A counter must be incremented inside the loop body. 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.
Which of the following is an advantage of a sentinel-controlled loop? The number of iterations does not have to be known. ---- Correct 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.
The only post test loop structure available in C# is _____. while for do...while --- Correct foreach
do...while
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 --- Correct
foreach
for (int i = 0; i < 10; i++) { Console.WriteLine("counter " + i); } The variable i defined in the program segment above ____. is out of scope when the loop terminates -- Correct creates an error because it is changed in the update portion would have a value of 10 if it were printed on the outside of the loop can be displayed prior to the loop program statements
is out of scope when the loop terminates
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 ---- Correct last-value counter controlled boundaries
off-by-one
With the while loop, the body of the loop is ____. always performed at least one time performed as long as the conditional expression evaluates to true --- Correct must produce a boolean result must be enclosed in curly braces
performed as long as the conditional expression evaluates to true
A method that calls itself repeatedly until it arrives at the solution is a(n) ____method. static recursive -- Correct iterative instance
recursive
In order to write a recursive solution ____. the simplest case should be determined --- Correct you must create an infinite loop write the iterative solution first the most complicated approach must be identified
the simplest case should be determined