wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Quiz 5: Loops

Total questions: 10

Worksheet time: 5mins

Name
Class
Date
1.

What is the value of loopCount after control exits the following loop?

loopCount = 1;

while (loopCount <= 140)

{

alpha = alpha + 7;

loopCount++;

}

a)

1

b)

139

c)

140

d)

141

2.

What is the output of the following code?

n = 1;

while (n <= 10)

{

n = n + 2;

cout << n << ' ';

}

a)

1 3 5 7 9

b)

1 3 5 7 9 11

c)

3 5 7 9 11

d)

1 3 5 7 9 11 13 forever

3.

What is the output of the following code fragment?


int value = 10;

while (value > 2)

{

cout << value << " ";

--value;

}

a)

10 9 8 7 6 5 4 3

b)

3 4 5 6 7 8 9 10

c)

10 9 8 7 6 5 4 3 2

d)

10 10 10 forever

4.

Given the input data

Hi# Good day.

what is the output of the following code fragment?


char ch1;

int howMany= 0;


cin >> ch1;

while (ch1 != '#')

{

howMany++;

cin >> ch1;

}

cout << howMany << endl;

a)

1

b)

2

c)

3

d)

12

5.

Given the input

-3 5 -1 8 -2 -4 0

what is stored in the variables positive and negative after the following loop executes (assume all variables have been declared as type int and initialized to 0)?

cin >> num;

while (num != 0)

{

if (num > 0)

++positive;

else if (num < 0)

++negative;

cin >> num;

}

a)

positive = 3 and negative = 2

b)

positive = 3 and negative = 3

c)

positive = 2 and negative = 4

d)

positive = 4 and negative = 2

6.

Which loop design would be most appropriate for solving the problem "Count the number of positive integers in a data file of unknown length"?

a)

a count-controlled loop

b)

a flag-controlled loop

c)

a sentinel-controlled loop

d)

an End-of-file-controlled loop

7.

what is the output of the following code fragment? (All variables are of type int.)

sum = 0;

num = 9;

while (num != 0)

{

sum = sum + num;

num = num - 2;

}

cout << sum << endl;

a)

55

b)

25

c)

16

d)

no output--this is an infinite loop

8.

What does the following C++ program fragment do?

while (count < 10)

count++;

cout << "Hello";

a)

prints nothing

b)

prints "Hello" once

c)

prints "Hello" 9 times

d)

prints "Hello" forever

9.

What does the following program fragment do? (read carefully)


for (count =1; count <9; count++);

cout << "Hello";

a)

prints "Hello" 8 times

b)

prints "Hello" 1 time

c)

prints "Hello" forever

d)

prints "Hello" 0 time

10.

What does the following program fragment do? (read carefully)

for (int m=2; m<17; m--)

{

cout<<"Hello";

}

a)

prints "Hello" 15 times

b)

prints "Hello" 0 time

c)

prints "Hello" 16 times

d)

print "Hello" forever