WorksheetsQuiz 5: Loops
Total questions: 10
Worksheet time: 5mins
What is the value of loopCount after control exits the following loop?
loopCount = 1;
while (loopCount <= 140)
{
alpha = alpha + 7;
loopCount++;
}
1
139
140
141
What is the output of the following code?
n = 1;
while (n <= 10)
{
n = n + 2;
cout << n << ' ';
}
1 3 5 7 9
1 3 5 7 9 11
3 5 7 9 11
1 3 5 7 9 11 13 forever
What is the output of the following code fragment?
int value = 10;
while (value > 2)
{
cout << value << " ";
--value;
}
10 9 8 7 6 5 4 3
3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2
10 10 10 forever
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;
1
2
3
12
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;
}
positive = 3 and negative = 2
positive = 3 and negative = 3
positive = 2 and negative = 4
positive = 4 and negative = 2
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 count-controlled loop
a flag-controlled loop
a sentinel-controlled loop
an End-of-file-controlled loop
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;
55
25
16
no output--this is an infinite loop
What does the following C++ program fragment do?
while (count < 10)
count++;
cout << "Hello";
prints nothing
prints "Hello" once
prints "Hello" 9 times
prints "Hello" forever
What does the following program fragment do? (read carefully)
for (count =1; count <9; count++);
cout << "Hello";
prints "Hello" 8 times
prints "Hello" 1 time
prints "Hello" forever
prints "Hello" 0 time
What does the following program fragment do? (read carefully)
for (int m=2; m<17; m--)
{
cout<<"Hello";
}
prints "Hello" 15 times
prints "Hello" 0 time
prints "Hello" 16 times
print "Hello" forever
