CS 258 Quiz 6

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

off-by-one

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.

recursive

A method that calls itself repeatedly until it arrives at the solution is a(n) ____method.

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

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

the simplest case should be determined

In order to write a recursive solution ____.

handles the repetition for you

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

0

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

before the loop body

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

True

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

False

True or 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

True or False: In C# curly braces must be added to surround all loop bodies.

sentinel-controlled loops

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

a) 0

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

c) 99

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

b) 3

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? a) 2 b) 3 c) 5 d) 6

b for (int r = 0, c = 5; r < 3 && c > 2; r++, c--)

An example for statement that has a compound initialization, compound test and compound update could be written as ____. a) for (int r = 0; c = 5; r < 3 && c > 2; r++; c--) b) for (int r = 0, c = 5; r < 3 && c > 2; r++, c--) c) for (int r = 0; c = 5; r < 3 ; c > 2; r++; c--) d) for (int r = 0, c = 5; r < 3, c > 2; r++, c--)

state

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.

do...while

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

performed as long as the conditional expression evaluates to true

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

b) 20

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? a) 19 b) 20 c) 99 d) 100

a) 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)? a) 0 b) 4 c) 5 d) 6

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.

True

True or False: One of the major strengths of programming languages can be attributed to loops.

True

True or False: The conditional expression is sometimes called the loop condition.

True

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

False

True or 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.

True

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

False

True or False: The while statement is the only type of loop construct that can be used to develop a sentinel-controlled loop.

True

True or False: 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.

False

True or False: With nested loops, the outermost loop is always totally completed before the innermost loop is executed.

True

True or False: Without incrementing the counter used in the conditional expression with counter controlled loops, the loop would go on indefinitely.

d) The conditional expression must be set up prior to the loop.

What is a requirement of the sentinel-controlled loop shared by counter-controlled loops? a) A counter must be initialized on the outside of the loop. b) The expression must evaluate to false in order for the loop to be executed. c) A counter must be incremented inside the loop body. d) The conditional expression must be set up prior to the loop.

c) while

Which of the following is NOT a keyword used to enable unconditional transfer of control to a different program statement? a) continue b) break c) while d) goto

a) The number of iterations does not have to be known.

Which of the following is an advantage of a sentinel-controlled loop? a) The number of iterations does not have to be known. b) It could be used with the while form of the loop. c) An extreme or dummy value can be entered. d) The loop is always performed at least one time.

c) 5

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

False

True or 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

True or False: A restriction on using the foreach statement is that you cannot change values in the collection. The access to the elements is read-only.

False

True or False: A sentinel controlled loop is also called a flag-controlled loop.

False

True or False: A state-controlled loop should be designed when you know the number of times the statements must be executed.

True

True or False: An advantage of sentinel-controlled loops is you do not need to know how many times the loop will be performed.

True

True or False: An interpretation of the while statement is "while the condition is true, perform statement(s)".

True

True or False: 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

True or False: C# performs automatic garbage collection once the loop is finished and releases any variables declared as part of the loop.

False

True or False: Class diagrams show the data, method, and property members of a class.

True

True or False: In order to use the MessageBox.Show( ) method, you need to not only add a reference to the namespace which includes the class, but also add a using directive to the program statements

False

True or False: Iteration is a technique used where a method calls itself repeatedly until it arrives at the solution.

True

True or False: To write a recursive solution, a base case needs to be identified.

True

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

True

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

False

True or False: You often need to do a "prime the read" for counter controlled loops which means that you would input a value before going into the body of the loop.

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

What is NOT required when a counter-controlled loop is created? a) Incrementing the loop control variable inside the loop. b) A conditional expression involving the loop control variable. c) Initializing loop control variable on the outside of the loop. d) Allowing the user to enter a value indicating the loop should stop.

d) is out of scope when the loop terminates

for (int i = 0; i < 10; i++) { Console.WriteLine("counter " + i); } The variable i defined in the program segment above ____. a) creates an error because it is changed in the update portion b) would have a value of 10 if it were printed on the outside of the loop c) can be displayed prior to the loop program statements d) is out of scope when the loop terminates

b) 5

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? a) 4 b) 5 c) 6 d) 10

b) Outer: 1 Inner: 2

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? a) Outer: 2 Inner 3 b) Outer: 1 Inner: 2 c) Outer: 1 Inner 0 d) Outer: 3 Inner 0

d) 6

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? a) 2 b) 3 c) 5 d) 6

b) 1

int loopVariable = 0; do { Console.WriteLine("Count = {0:}", ++loopVariable); }while (loopVariable < 5); What will be printed during the first iteration through the loop? a) 0 b) 1 c) 10 d) none of the above


Set pelajaran terkait