Self Test 2: Chapter 2, Flow of Control

  1. What is the output of this code?
    
        1	#include <iostream>
        2	using namespace std;
        3	
        4	int main()
        5	{
        6	  int i;
        7	
        8	  i = 0;
        9	  while( i < 5 ) {
       10	    cout << "A";
       11	    i++;
       12	  }
       13	  return 0;
       14	}
    

    1. AAAA


    2. AAAAA


    3. A
      A
      A
      A


    4. A
      A
      A
      A
      A

    next prev start
  2. What is the output of this code? (Note the two slight changes from the previous problem.)
    
        1	#include <iostream>
        2	using namespace std;
        3	
        4	int main()
        5	{
        6	  int i;
        7	
        8	  i = 1;
        9	  while( i < 5 ) {
       10	    cout << "A" << endl;;
       11	    i++;
       12	  }
       13	  return 0;
       14	}
    

    1. AAAA


    2. AAAAA


    3. A
      A
      A
      A


    4. A
      A
      A
      A
      A

    next prev start
  3. There is no syntax error below (it compiles), but it does not do what it claims. What is the error?

    
        1	#include <iostream>
        2	
        3	using namespace std;
        4	
        5	int main()
        6	{
        7	  int sum;
        8	  int i;
        9	  int n;
       10	  
       11	  cout << "This program will compute the sum 1 + 2 + ... + n" << endl;
       12	  cout << "\nEnter a value for n: ";
       13	  cin >> n;
       14	  
       15	  sum = 0;
       16	  while( i <= n ) {
       17	    sum += i;
       18	    i++;
       19	  }
       20	
       21	  cout << "1 + 2 + ... + " << n << " = " << sum << endl;
       22	
       23	  return 0;
       24	}
    
    1. sum should be initialized to 1.
    2. i should be initialized to 1.
    3. sum += i; should be sum = sum + i;
    4. i++ should be ++i
    next prev start
  4. The following code also compiles without errors, but contains a serious error. What is it?

    
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int capacity;
      cout << "This program will determine the largest value of n"
           << "\nsuch that 1 + 2 + 3 + ... + n < capacity" << endl;
      cout << "\nEnter a value for capacity > 1: ";
      cin >> capacity;
      if (capacity <= 1) {
        cout << "The capacity  must be > 1" << endl;
        exit(0);
      }
    
      int sum = 0;
      int n = 1
      while( sum + n < capacity ) {
        sum += n;
      }
      cout << "The largest value of n such that 1 + 2 + 3 + ... + n < " 
           << capacity << " is " << n - 1<< endl;
    
      return 0;
    }
    
    1. The loop condition sum + n < capacity should be sum < capacity
    2. The initial value of n should be 0.
    3. The statement in the loop body, sum += n; should be sum += n++;
    4. The statement in the loop body, sum += n; should be sum += ++n;
    next prev start
  5. What is the output of the code below as written and what would the output be if lines 10 and 11 were interchanged?

    
        1	#include <iostream>
        2	
        3	using namespace std;
        4	
        5	int main()
        6	{
        7	  int sum = 0;
        8	  int k = 1;
        9	  while (k < 4) {
       10	    sum += k;
       11	    k++;
       12	  }
       13	  cout << sum << endl;
       14	
       15	  return 0;
       16	}
    
    1. 4 and 4
    2. 6 and 6
    3. 4 and 7
    4. 6 and 9
    next prev start
  6. Which for loop below is equivalent to this code using a while loop:

    
      int x;
      x = 1;
      while( x < 10) {
        if (x % 3 == 0) {
          cout << x << " is divisible by 3." << endl;
        }
        x++;
      }
    
    1. 
          1	for(int x = 1; x < 10; x++) {
          2	    if (x % 3 == 0) {
          3	      cout << x << " is divisible by 3." << endl;
          4	    }
          5	    x++;
          6	}
      
    2. 
          1	for(int x = 1; x < 10; x++) {
          2	    x++;
          3	    if (x % 3 == 0) {
          4	      cout << x << " is divisible by 3." << endl;
          5	    }
          6	}
      
    3. 
          1	for(int x = 1; x < 10; x++) {
          2	    if (x % 3 == 0) {
          3	      cout << x << " is divisible by 3." << endl;
          4	    }
          5	}
      
    4. 
          1	for( int x;  x < 10; x++) {
          2	    if (x % 3 == 0) {
          3	      cout << x << " is divisible by 3." << endl;
          4	    }
          5	}
      
    next prev start
  7. Which while loop below is equivalent to this for loop:
    
    int n;
    for(n = 10; n > 0; n--) {
      p *= n;
    }
    
    1. 
      int n;
      n = 10;
      while( n > 0 ) {
        n--;
        p *= n;
      }
      
    2. 
      int n = 10;
      while( n > 0 ) {
        n--;
        p *= n;
      }
      
    3. 
      int n;
      n = 10;
      while( n > 0 ) {
        p *= n;
        n--;
      }
      
    4. 
      int n = 10;
      while( n > 0 ) {
        p *= --n;
      }
      
    next prev start
  8. What is the output of the following program?

    
    #include <iostream>
    
    int main()
    {
      for(int i = 0; i < 5; i++) {
        cout << "A";
      }
      return 0;
    }
    

    1. AAAA


    2. AAAAA


    3. A
      A
      A
      A


    4. A
      A
      A
      A
      A

    next prev start
  9. What is the output of this program? (Note the 2 slight changes from the program in the previous problem.)

    
    #include <iostream>
    
    int main()
    {
      for(int i = 1; i < 5; i++) {
        cout << "A" << endl;;
      }
      return 0;
    }
    

    1. AAAA


    2. AAAAA


    3. A
      A
      A
      A


    4. A
      A
      A
      A
      A

    next prev start
  10. Which of the following will print exactly 10 copies of X?

    1. 
      #include <iostream>
      
      int main()
      {
        for(int i = 0; i <= 10; i++) {
          cout << "X";
        }
        cout << endl;
        return 0;
      }
      
    2. 
      #include <iostream>
      
      int main()
      {
        for(int i = 1; i < 10; i++) {
          cout << "X";
        }
        cout << endl;
        return 0;
      }
      
    3. 
      #include <iostream>
      
      int main()
      {
        for(int i = 0; i < 10; i++) {
          cout << "X";
        }
        cout << endl;
        return 0;
      }
      
    4. 
      #include <iostream>
      
      int main()
      {
        for(int i = 0; i < 11; i++) {
          cout << "X";
        }
        cout << endl;
        return 0;
      }
      
    next prev start
  11. What is output if input is 'Tristram Shandy' (without the quotes)?
    
        1	#include <iostream>
        2	#include <string>
        3	using namespace std;
        4	
        5	int main()
        6	{
        7	  string name;
        8	  cout << "Enter a name (no spaces): ";
        9	  cin >> name;
       10	  int n = name.length();
       11	
       12	  for(int i = 0; i < n + 2; i++) {
       13	    cout << "*";
       14	  }
       15	
       16	  cout << endl;
       17	  cout << "*" << name << "*" << endl;
       18	
       19	  for(int i = 0; i < n + 2; i++) {
       20	    cout << "*";
       21	  }
       22	  cout << endl;
       23	
       24	  
       25	  return 0;
       26	}
    
    1. 
      *****************
      *Tristram Shandy*
      *****************
      
    2. 
      **********
      *Tristram*
      **********
      
    3. 
      ***************
      *Tristram Shandy*
      ***************
      
    4. 
      ********
      *Tristram*
      ********
      
    next prev start
  12. What does this code do?
    
    int sum = 0;
    for(int i = 1; i <= 10; i += 2) {
      sum += i;
    } 
    cout << sum << endl;
    
    1. Prints the sum of the 1 + 2 + ... + 10
    2. Prints the sum of the 1 + 2 + ... + 5
    3. Prints the sum of the 2 + 4 + 6 + ... + 10
    4. Prints the sum of the 1 + 3 + 5 ... + 9
    next prev start
  13. What is output?
    
    int a = 1;
    for( int k = 0; k < 4; k++) {
      a *= 2;
    }
    cout << a << endl;
    
    1. 4
    2. 8
    3. 16
    4. 32
    next prev start