Unit 2 Test Repetition Statements

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

int counter = 0; while (counter < 100) { Console.WriteLine(counter);counter++; } The first value printed with the program segment above is ____.

0

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

1

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?

3

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?

5

int loopVariable = 0; do { Console.WriteLine("Count = {0:}", ++loopVariable); }while (loopVariable < 5); How many times will be loop body be executed?

5

What is NOT required when a counter-controlled loop is created?

Allowing the user to enter a value indicating the loop should stop.

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

Which of the following is an advantage of a sentinel-controlled loop?

The number of iterations does not have to be known.

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

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

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.

foreach

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

When one of the program statement included within the body of a loop is another loop, a(n) ____________ loop is created.

nested

Inside the for statement, when you place a semicolon after the parentheses without anything to the left of it, it indicates that no statement or a(n) ____________ statement is associated with the initialize portion.

null

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

With the while loop, the body of the loop is ____.

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.

recursive

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.

state

Using the break or continue statements with a loop violates the single entry and single exit guideline for developing a loop.

true

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?

6

To "prime the read" with a loop, you place an input statement ____.

before the loop body

When you know the number of times the statements must be executed, create a(n) ____________ loop.

counter-controlled

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

for (int i = 0; i < 5; i++) Console.Write(i + "\t"); The conditional expression used with the for statement ____.

is written following the first semicolon

The third programming construct repetition is also called ____________.

iteration or looping

You must tell the user what value to type to end the loop for ____.

sentinel-controlled loops

Which of the following is NOT a keyword used to enable unconditional transfer of control to a different program statement?

while

The most appropriate sentinel value that might be selected for month of the year is ____.

0

int loopVariable = 0; do { Console.WriteLine("Count = {0:}", ++loopVariable); }while (loopVariable < 5); What will be printed during the first iteration through the loop?

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?

20

int counter = 0; while (counter < 100) { Console.WriteLine(counter);counter++; } The last value printed with the above program segment is ____.

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

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.

In order to use the MessageBox.Show( ) method in your console application, you must ____.

add a reference to the System.Windows.Forms.dll.

The only posttest loop structure available in C# is _____.

do...while

A sentinel controlled loop is also called a flag-controlled loop.

false

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.

false

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.

false

s = num.Next(7); The statement above produces seven random numbers.

false

The event-driven model used to create Windows applications ____.

handles the repetition for you

A(n) ____________ is a loop that has no provisions for termination.

infinite loop

A flag-controlled loop is also called a ____.

state-controlled loop

In order to write a recursive solution ____.

the simplest case should be determined

One of the major strengths of programming languages can be attributed to loops.

true

The do...while loop can be used to implement a counter-controlled, sentinel-controlled, or state-controlled loop.

true

The sentinel value is used as the operand in the conditional expression for an indefinite loop.

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

int sum = 0; int number = 1; while (number < 100) { ***** sum = sum + number; } The program statements above produce an infinite loop.

true

To write a recursive solution, a base case needs to be identified.

true

Windows applications use an event-driven model to manage the interaction between the user and the GUI by handling the repetition for you.

true


Ensembles d'études connexes

58 - Pelvic Diaphragm Vessels & Nerves

View Set

Psychology Chapter 14 - McGraw Hill

View Set

computer tech chapter five -- storage basics

View Set

Systems Administrator Security Questions

View Set

Aircraft Drawings General Prepware

View Set