wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

C++ - Loops and Random Numbers

Total questions: 17

Worksheet time: 15mins

Name
Class
Date
1.

What output is produced by the following segment of code:


for(int i = 0; i < 10; i++)

cout << i << " ";

a)

0 1 2 3 4 5 6 7 8 9 10

b)

0 1 2 3 4 5 6 7 8 9

c)

1 2 3 4 5 6 7 8 9 10

d)

1 2 3 4 5 6 7 8 9

e)

Infinite Loop

2.

What output is produced by the following segment of code:


for(int i = 0; i <= 10; i++)

cout << i << " ";

a)

0 1 2 3 4 5 6 7 8 9 10

b)

0 1 2 3 4 5 6 7 8 9

c)

1 2 3 4 5 6 7 8 9 10

d)

1 2 3 4 5 6 7 8 9

e)

Infinite Loop

3.

What output is produced by the following segment of code:


for(int i = 1; i <= 10; i++)

cout << i << " ";

a)

0 1 2 3 4 5 6 7 8 9 10

b)

0 1 2 3 4 5 6 7 8 9

c)

1 2 3 4 5 6 7 8 9 10

d)

1 2 3 4 5 6 7 8 9

e)

Infinite Loop

4.

What output is produced by the following segment of code:


for(int fun = 20; fun > 1; fun -=3)

cout << fun << " ";

a)

20 17 14 11 8 5 2 -1

b)

20 17 14 11 8 5 2

c)

17 14 11 8 5 2 -1

d)

20 17 14 11 8 5

e)

Infinite Loop

5.

What output is produced by the following segment of code:


int start = 5;

int end = 20;

for(; start <= end; start +=2)

cout << start << " ";

a)

5 7 9 11 13 15 17 19 21

b)

7 9 11 13 15 17 19 20

c)

7 9 11 13 15 17 19 21

d)

5 7 9 11 13 15 17 19

e)

Infinite Loop

6.

What output is produced by the following segment of code:


for (int dmc = 8; dmc >= 0; dmc-=4)

cout << dmc << " ";

a)

8 4 0

b)

8 4

c)

8

d)

No Output

e)

Infinite Loop

7.

What output is produced by the following segment of code:


for (int dmc = 8; dmc >= 8; dmc-=4)

cout << dmc << " ";

a)

8 4 0

b)

8 4

c)

8

d)

No Output

e)

Infinite Loop

8.

What output is produced by the following segment of code:


for (int dmc = 8; dmc >= 10; dmc-=4)

cout << * << " ";

a)

* * *

b)

* *

c)

*

d)

No Output

e)

Infinite Loop

9.

What output is produced by the following segment of code:


for (int x = 10; x > 0; x++)

cout << x << " ";

a)

10 9 8 7 6 5 4 3 2 1

b)

10 9 8 7 6 5 4 3 2 1 0

c)

9 8 7 6 5 4 3 2 1

d)

No Output

e)

Infinite Loop

10.

What output is produced by the following segment of code:


for (int x = 5; x > 0; x--);

cout << * << " ";

a)

* * * * *

b)

* * * * * *

c)

*

d)

No Output

e)

Infinite Loop

11.

What range of values are generated by the statement:

cout << rand() % 7 + 2;

a)

2 to 7

b)

2 to 8

c)

1 to 9

d)

1 to 8

12.

What TWO statements are needed at the beginning of the program to ensure that you get different random numbers each time you execute the program?

a)

return(0);

b)

unsigned seed = time(0);

c)

srand(seed);

d)

cout << random << endl;

13.

Given the following segment of code:

for(int i = 0; i <= 4; i++)

    cout << rand() % 10 << ", ";

Which of the following could be one of the possible outputs?

a)

1, 11, 0, 9

b)

8, 7, 5, 0

c)

5, 3, 8, 7, 6

d)

4, -1, 4 , 2, 8

14.

Given the following segment of code:

for(int i = 0; i < 6; i++)

    cout << rand() % 20 + 1 << ", ";

Which of the following could be one of the possible outputs?

a)

1, 2, 3, 20, 11 , 8,

b)

0, 5, 18, 6, 11, 12,

c)

11, 21, 14, 6, 8,

d)

5, 8, 14, 7, 3,

15.

What output is produced by the following segment of code:


for(int i = 3; i <= 9; i += 2)

cout << i << " ";

a)

3 5 7 9

b)

3 4 5 6 7 8 9

c)

4 6 8 10

d)

3 5 7

e)

Infinite Loop

16.

What range of values are generated by the statement:

cout << rand() % 5 + 10;

a)

1 to 5

b)

10 to 14

c)

5 to 10

d)

10 to 15

17.

Given the following segment of code:

for(int i = 2; i < 7; i++)

cout << rand() % 4 << ", ";

Which of the following could be one of the possible outputs?

a)

0, 1, 2, 3, 0

b)

4, 5, 6, 7, 8

c)

0, 0, 0, 0, 0

d)

1, 2, 3, 4, 5