Introduction to Programming with C++, Third Edition, Y. Daniel Liang

This quiz is for students to practice. A large number of additional quiz is available for instructors using Quiz Generator from the Instructor's Resource Website. Videos for Java, Python, and C++ can be found at https://yongdanielliang.github.io/revelvideos.html.

Many questions in this edition have been updated in the new edition. Please check with the publisher on the newest edition.

Chapter 5 Loops


Section 5.2 The while Loop
5.1  How many times will the following code print "Welcome to C++"?

int count = 0;
while (count < 10
{
  cout << "Welcome to C++";
  count++;
}
A. 8
B. 9
C. 10
D. 11
E. 0

5.2  What is the output of the following code?

int x = 0;
while (x < 4
{
  x = x + 1;
}
cout << "x is " << x << endl;
A. x is 0
B. x is 1
C. x is 2
D. x is 3
E. x is 4

5.3  Analyze the following code.

int count = 0;
while (count < 100
{
  // Point A
  cout << "Welcome to C++!" << endl;
  count++;
  // Point B
}

  // Point C

int count = 0;
while (count < 100
{
  // Point A
  cout << "Welcome to C++!\n";
  count++;
  // Point B
}
  // Point C
A. count < 100 is always true at Point A
B. count < 100 is always true at Point B
C. count < 100 is always false at Point B
D. count < 100 is always true at Point C
E. count < 100 is always false at Point C

5.4  How many times will the following code print "Welcome to C++"?

int count = 0;
while (count++ < 10
{
  cout << "Welcome to C++";
}
A. 8
B. 9
C. 10
D. 11
E. 0

5.5  What is the output of the following code?

int count = 0;
while (count < 5
{
  cout << count << " ";
  count++;
}
A. 1 2 3 4 5
B. 2 3 4 5
C. 0 1 2 3 4 5
D. 0 1 2 3 4

5.6  What is the output of the following code?

int count = 5;
while (count > 0
{
  cout << count << " ";
  count--;
}
A. 5 4 3 2 1
B. 4 3 2 1
C. 4 3 2 1 0
D. 5 4 3 2 1 0

5.7  What will be displayed when the following code is executed?

    int number = 6;
    while (number > 0)
    {
      number -= 3;
      cout << number << " ";
    }
A. 6 3 0
B. 6 3
C. 3 0
D. 3 0 -3
E. 0 -3

Section 5.3 The do-while Loop
5.8  How many times will the following code print "Welcome to C++"?

int count = 0;
do 
{
  cout << "Welcome to C++";
  count++;
while (count < 10);
A. 8
B. 9
C. 10
D. 11
E. 0

5.9  What is the output of the following code?

int count = 0;
do
{
  cout << count << " ";
  count++;
}
while (count < 5);
A. 1 2 3 4 5
B. 2 3 4 5
C. 0 1 2 3 4 5
D. 0 1 2 3 4

5.10  What is the output of the following code?

int count = 0;
do
{
  cout << count << " ";
}
while (count < 5);
A. 1 2 3 4 5
B. 2 3 4 5
C. 0 1 2 3 4 5
D. 0 1 2 3 4
E. The program has an infinite loop

5.11  How many times will the following code print "Welcome to C++"?

int count = 0;
do 
{
  cout << "Welcome to C++";
while (count++ < 10);
A. 8
B. 9
C. 10
D. 11
E. 0

5.12  How many times will the following code print "Welcome to C++"?

int count = 0;
do 
{
  cout << "Welcome to C++";
while (++count < 10);
A. 8
B. 9
C. 10
D. 11
E. 0

5.13  What is the value in count after the following loop is executed?

int count = 0;
do 
{
  cout << "Welcome to C++";
while (count++ < 9);
cout << count;
A. 8
B. 9
C. 10
D. 11
E. 0

Section 5.4 The for Loop
5.14  Analyze the following statement:

double sum = 0;
for (double d = 0; d < 10;) 
{
  d += 0.1;
  sum += sum + d;
}
A. The program has a compile error because the adjustment is missing in the for loop.
B. The program has a compile error because the control variable in the for loop cannot be of the double type.
C. The program runs in an infinite loop because d < 10 would always be true.
D. The program compiles and runs fine.

5.15  Which of the following loops prints "Welcome to C++" 10 times?

A:
for (int count = 1; count <= 10; count++) 
{
  cout << "Welcome to C++" << endl;
}

B:
for (int count = 0; count < 10; count++) 
{
  cout << "Welcome to C++" << endl;
}

C:
for (int count = 1; count < 10; count++) 
{
  cout << "Welcome to C++" << endl;
}

D:
for (int count = 0; count <= 10; count++) 
{
  cout << "Welcome to C++" << endl;
}
A. BD
B. ABC
C. AC
D. BC
E. AB

5.16  The following loop displays _______________.

for (int i = 1; i <= 10; i++) 
{
  cout << i << " ";
  i++;
}
A. 1 2 3 4 5 6 7 8 9
B. 1 2 3 4 5 6 7 8 9 10
C. 1 2 3 4 5
D. 1 3 5 7 9
E. 2 4 6 8 10

5.17  Which of the following loops correctly computes 1/2 + 2/3 + 3/4 + ... + 99/100?

A:
double sum = 0;
for (int i = 1; i <= 99; i++) 
{
  sum = i / (i + 1);
}
cout << "Sum is " << sum << endl;

B:
double sum = 0;
for (int i = 1; i < 99; i++) 
{
  sum += i / (i + 1);
}
cout << "Sum is " << sum << endl;

C:
double sum = 0;
for (int i = 1; i <= 99; i++) 
{
  sum += 1.0 * i / (i + 1);
}
cout << "Sum is " << sum << endl;

D:
double sum = 0;
for (int i = 1; i <= 99; i++) 
{
  sum += i / (i + 1.0);
}
cout << "Sum is " << sum << endl;

E:
double sum = 0;
for (int i = 1; i < 99; i++) 
{
  sum += i / (i + 1.0);
}
cout << "Sum is " << sum << endl;
A. BCD
B. ABCD
C. B
D. CDE
E. CD

5.18  Do the following two statements result in the same value in sum?

for (int i = 0; i < 10; ++i) 
{
  sum += i;
}

for (int i = 0; i < 10; i++) 
{
  sum += i;
}
A. Yes
B. No

5.19  What is the output for y?

int y = 0;
for (int i = 0; i < 10; ++i) 
{
  y += i;
}
cout << y;
A. 10
B. 11
C. 12
D. 13
E. 45

5.20  What is i after the following for loop?

int y = 0;
for (int i = 0; i < 10; ++i) 
{
  y += i;
}
A. 9
B. 10
C. 11
D. undefined

5.21  How many times is the println statement executed?

for (int i = 0; i < 10; i++) 
  for (int j = 0; j < 10; j++)
    cout << i * j << endl;

A. 100
B. 20
C. 10
D. 45

5.22  How many times is the println statement executed?

for (int i = 0; i < 10; i++) 
  for (int j = 0; j < i; j++)
    cout << i * j << endl;
A. 100
B. 20
C. 10
D. 45

5.23  Is the following loop correct?

for (;  ; );
A. Yes
B. No

5.24  Given the following four patterns,

Pattern A        Pattern B        Pattern C        Pattern D
1                1 2 3 4 5 6                1      1 2 3 4 5 6
1 2              1 2 3 4 5                2 1        1 2 3 4 5
1 2 3            1 2 3 4                3 2 1          1 2 3 4
1 2 3 4          1 2 3                4 3 2 1            1 2 3
1 2 3 4 5        1 2                5 4 3 2 1              1 2
1 2 3 4 5 6      1                6 5 4 3 2 1                1


Which of the pattern is produced by the following code?

    for (int i = 1; i <= 6; i++)
    {
      for (int j = 6; j >= 1; j--)
        if (j <= i)
          cout << j << " ";
        else
          cout << " ";

      cout << endl;
    }
A. Pattern A
B. Pattern B
C. Pattern C
D. Pattern D

Section 5.5 Which Loop to Use?
5.25  Analyze the following fragment:

double sum = 0;
double d = 0;
while (d != 10.0
{
  d += 0.1;
  sum += sum + d;
}
A. The program does not compile because sum and d are declared double, but assigned with integer value 0.
B. The program never stops because d is always 0.1 inside the loop.
C. The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers.
D. After the loop, sum is 0 + 0.1 + 0.2 + 0.3 + ... + 1.9

5.26  Analyze the following code:

int main() 
{
  int i = 0;
  for (i = 0; i < 10; i++);
    cout << i + 4;

  return 0;
}
A. The program has a compile error because of the semicolon (;) on the for loop line.
B. The program compiles despite the semicolon (;) on the for loop line, and displays 4.
C. The program compiles despite the semicolon (;) on the for loop line, and displays 14.
D. The for loop in this program is same as for (i = 0; i < 10; i++) { }; cout << i + 4;

Section 5.9 Keywords break and continue
5.27  Will the following program terminate?

int balance = 10;

while (true
{
  if (balance < 9) break;
  balance = balance - 9;
}
A. Yes
B. No

5.28  What is sum after the following loop terminates?

int sum = 0;
int item = 0;
do 
{
  item++;
  sum += item;
  if (sum > 4) break;
}
while (item < 5);
A. 5
B. 6
C. 7
D. 8

5.29  What is the output after the following loop terminates?

int number = 25;
int i;

bool isPrime = true;
for (i = 2; i < number && isPrime; i++) 
{
  if (number % i == 0)
  {
    isPrime = false;
  }
}

cout << "i is " << i << " isPrime is " << isPrime << endl;
A. i is 5 isPrime is 1
B. i is 5 isPrime is 0
C. i is 6 isPrime is 1
D. i is 6 isPrime is 0

5.30  What is the output after the following loop terminates?

int number = 25;
int i;

bool isPrime = true;
for (i = 2; i < number; i++)
{
  if (number % i == 0)
  {
    isPrime = false; break;
  }
}

cout << "i is " << i << " isPrime is " << isPrime << endl;
A. i is 5 isPrime is 1
B. i is 5 isPrime is 0
C. i is 6 isPrime is 1
D. i is 6 isPrime is 0

5.31  What is sum after the following loop terminates?

int sum = 0;
int item = 0;
do 
{
  item++;
  sum += item;
  if (sum >= 4) continue;
}
while (item < 5);
A. 15
B. 16
C. 17
D. 18

5.32  Will the following program terminate?

int balance = 10;

while (true
{
  if (balance < 9) continue;
  balance = balance - 9;
}
A. Yes
B. No

5.33  What is the number of iterations in the following loop:

  for (int i = 1; i < n; i++)
  {
    // iteration
  }
A. 2*n
B. n
C. n - 1
D. n + 1

5.34  What is the number of iterations in the following loop:

  for (int i = 1; i <= n; i++)
  {
    // iteration
  }
A. 2*n
B. n
C. n - 1
D. n + 1

5.35  Suppose the input for number is 9. What is the output from running the following program?

 #include <iostream>
 using namespace std;

 int main()
 {
   cout << "Enter an integer: ";
   int number;
   cin >> number;

   int i;
   bool isPrime = true;
   for (i = 2; i < number && isPrime; i++)
   {
     if (number % i == 0)
     {
       isPrime = false;
     }
   }

   cout << "i is " << i << endl;

   if (isPrime)
     cout << number << " is prime" << endl;
   else
     cout << number << " is not prime" << endl;

   return 0;
 }
A. i is 3 followed by 9 is prime
B. i is 3 followed by 9 is not prime
C. i is 4 followed by 9 is prime
D. i is 4 followed by 9 is not prime