Font size
WorksheetsC++ - Loops and Random Numbers
Total questions: 17
Worksheet time: 15mins
What output is produced by the following segment of code:
for(int i = 0; i < 10; i++)
cout << i << " ";
0 1 2 3 4 5 6 7 8 9 10
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9
Infinite Loop
What output is produced by the following segment of code:
for(int i = 0; i <= 10; i++)
cout << i << " ";
0 1 2 3 4 5 6 7 8 9 10
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9
Infinite Loop
What output is produced by the following segment of code:
for(int i = 1; i <= 10; i++)
cout << i << " ";
0 1 2 3 4 5 6 7 8 9 10
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9
Infinite Loop
What output is produced by the following segment of code:
for(int fun = 20; fun > 1; fun -=3)
cout << fun << " ";
20 17 14 11 8 5 2 -1
20 17 14 11 8 5 2
17 14 11 8 5 2 -1
20 17 14 11 8 5
Infinite Loop
What output is produced by the following segment of code:
int start = 5;
int end = 20;
for(; start <= end; start +=2)
cout << start << " ";
5 7 9 11 13 15 17 19 21
7 9 11 13 15 17 19 20
7 9 11 13 15 17 19 21
5 7 9 11 13 15 17 19
Infinite Loop
What output is produced by the following segment of code:
for (int dmc = 8; dmc >= 0; dmc-=4)
cout << dmc << " ";
8 4 0
8 4
8
No Output
Infinite Loop
What output is produced by the following segment of code:
for (int dmc = 8; dmc >= 8; dmc-=4)
cout << dmc << " ";
8 4 0
8 4
8
No Output
Infinite Loop
What output is produced by the following segment of code:
for (int dmc = 8; dmc >= 10; dmc-=4)
cout << * << " ";
* * *
* *
*
No Output
Infinite Loop
What output is produced by the following segment of code:
for (int x = 10; x > 0; x++)
cout << x << " ";
10 9 8 7 6 5 4 3 2 1
10 9 8 7 6 5 4 3 2 1 0
9 8 7 6 5 4 3 2 1
No Output
Infinite Loop
What output is produced by the following segment of code:
for (int x = 5; x > 0; x--);
cout << * << " ";
* * * * *
* * * * * *
*
No Output
Infinite Loop
What range of values are generated by the statement:
cout << rand() % 7 + 2;
2 to 7
2 to 8
1 to 9
1 to 8
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?
return(0);
unsigned seed = time(0);
srand(seed);
cout << random << endl;
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?
1, 11, 0, 9
8, 7, 5, 0
5, 3, 8, 7, 6
4, -1, 4 , 2, 8
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?
1, 2, 3, 20, 11 , 8,
0, 5, 18, 6, 11, 12,
11, 21, 14, 6, 8,
5, 8, 14, 7, 3,
What output is produced by the following segment of code:
for(int i = 3; i <= 9; i += 2)
cout << i << " ";
3 5 7 9
3 4 5 6 7 8 9
4 6 8 10
3 5 7
Infinite Loop
What range of values are generated by the statement:
cout << rand() % 5 + 10;
1 to 5
10 to 14
5 to 10
10 to 15
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?
0, 1, 2, 3, 0
4, 5, 6, 7, 8
0, 0, 0, 0, 0
1, 2, 3, 4, 5
