CS150 Module 5

Ace your homework & exams now with Quizwiz!

What is the output of the following C++ code? num = 10; while (num > 10) num = num - 2; cout << num << endl;

10

What is the output of the following C++ code? int j; for (j = 10; j <= 10; j++) cout << j << " "; cout << j << endl;

10 11

Suppose j, sum, and num are int variables, and the input is 26 34 61 4 -1. What is the output of the code? sum = 0; cin >> num; for (int j = 1; j <= 4; j++) { sum = sum + num; cin >> num; } cout << sum << endl;

125

Suppose sum and num are int variables, and the input is 1 5 6 3 -1. What is the output of the following code? sum = 0; cin >> num; while (num != -1) { sum = sum + num; cin >> num; } cout << sum << endl;

15

What is the output of the following program segment? int count = 0; while (count++ < 5) cout << count + 1 << " "; cout << endl;

2 3 4 5 6

What is the output of the following C++ code? count = 1; num = 25; while (count < 25) { num = num - 1; count++; } cout << count << " " << num << endl;

25 1

What is the value of x after the following statements execute? int x = 5; int y = 30; do x = x * 2; while (x < y);

40

Consider the following code. (Assume that all variables are properly declared.) cin >> ch; while (cin) { cout << ch; cin >> ch; } This code is an example of a(n) ____ while loop.

EOF-controlled

Consider the following for loop. int j, s; s = 0; for (j =1; j<=10; j++) s = s + j * (j - 1); In this for loop, identify the loop control variable, the initialization statement, loop condition, the update statement, and the statement that updates the value of s.

Loop control variable-- j The initialization statement-- j = 1; Loop condition -- j<=10; Update statement -- j++ The statement that updates the value of s-- s + j * (j-1);

Suppose that the input is 0 5 6 4 9 8 -1. What is the output of the following code? int num = 0; int sum; cin >> sum; while (num != -1) { cin >> num; sum = sum + 2 * static_cast<int> (sqrt(num)); } cout << "Sum = " << sum << endl;

Sum = 22

What is the output of the following loop? count = 5; cout << 'St'; do { cout << 'o'; count--; } while (count <= 5);

This is an infinite loop.

The body of this loop executes at least once.

do...while

Which of the following is a repetition structure in C++?

do...while

Which of the following loops does not have an entry condition?

do...while loop

Which of the following loops is guaranteed to execute at least once?

do...while loop

What does a break statement do in a loop?

exits the loop

In a counter-controlled while loop, it is not necessary to initialize the loop control variable.

false

The statement in the body of a while loop acts as a decision maker.

false

When a while loop terminates, the control goes back to the statement just before the while statement, and then the control goes to the statement immediately following the while loop.

false

A(n) ____-controlled while loop uses a bool variable to control the loop.

flag

When does the following while loop terminate? char ch = 'D'; while ('A' <= ch && ch <= 'Z') ch = static_cast< char> (static_cst<int>(ch)+ 1;

if ch > 'Z' or ch < 'A'

A loop that continues to execute endlessly is called a(n) _________ loop.

infinite

In ____ structures, the computer repeats particular statements a certain number of times depending on some condition(s).

looping

In ____________ structures, the computer repeats particular statements a certain number of times depending on some condition(s).

looping

What is the output of the following program segment? int num = 0, count; int y=0; for (count = 1; count <= 5; ++ count) { y=y + count; num = num * count + y; } cout << "num = " << num << ", y = " << y << endl;

num= 485, y =15

A(n) ____ is an arbitrary value that, when read, stops the execution of a loop

sentinal

Suppose that the input is 20 -16 -5 15 6 0. What is the output of the following code? int num; int temp = 0; cin >> num; while ( num!= 0) { if (num % 2 == 0) temp = temp + num; else temp = temp - num; cin >> num; } cout << "temp = " << temp << endl;

temp = 0

Which executes first in a do... while loop?

the statement

A loop is a control structure that causes certain statements to execute over and over

true

A sentinel-controlled while loop is an event-controlled while loop whose termination depends on a special value.

true

Assume all variables are properly declared. The output of the following C++ code is 2 3 4 5. n = 1; while (n < 5) { n++; cout << n << " "; }

true

Both the while loop and the do...while loop are considered a conditional loop.

true

Executing a break statement in the body of a loop immediately terminates the loop.

true

In a counter-controlled while loop, the loop control variable must be initialized before the loop.

true

In an infinite loop, the while expression (the decision maker) is initially false, but after the first iteration it is always true. j = 0; while (j <= 10) j++; The while loop: terminates if j>10.

true

In the case of the sentinel-controlled while loop, the first item is read before the while loop is entered.

true

It is possible that the body of a while loop may not execute at all

true

The control statements in the for loop include the initial statement, loop condition, and update statement.

true

The control variable in a flag-controlled while loop is a bool variable.

true

The following while loop terminates when j>=20. j = 0; while (j < 20) j++;

true

The number of iterations of a counter-controlled loop is known in advance.

true

The output of the following C++ code is 2 3 4 5. n = 1; while (n < 5) { n++; cout << n << " "; }

true

To read data from a file of an unspecified length, an EOF-controlled loop is a good choice.

true

The body of the loop may not execute at all for the _________ loop.

while

Which loop has the logical expression controlling the loop and is evaluated before the loop is entered.

while

What is the output of the following program segment? int count = 0; while ( count++ <= 5) cout << (count -1) * (count -2) << " "; cout << endl;

0 3 8 15 24

What is the output of the following C++ code? num = 10; while (num > 10) { num = num -2; } cout << num << endl;

10

To generate a random number, you can use the function rand of the header file

cstdlib

A do...while loop is called a pretest loop.

false


Related study sets

Chapter 9: Professional Organizations

View Set

Ch.7 Organization Change and Innovation

View Set

Experiment 4 Part 1: Distillation

View Set