NAME: _____________________________________           GRADE (TA use only) _____________

CS 102 Exam I, March 10, 2011

  1. (6 points)   Examine the following code segment and indicate the output for each part.

            if(x != 20 && y)  
              x++;
            else if(x < 0 || y == 20)
              y--;
            else x += y;
    
            cout << x << "  " << y << endl; 

    • (6 points)   Hint: Substitute actual values to see what the code does.
    • (3 points)   What gets printed from the code segment below?
    • (6 points)   Examine the following code.
         cout << " Please enter an integer  ";
         cin >> x;
         switch(x)  {
           case 30:  x--;  x--;  x--;  break;
           case 5:   x = x * x;  
           case 27:  x--;  break;
           case 25:  x %= 6;
           case 20:  x += 35;
           default:  x++;
         }
           
         cout << x << endl;
      
        a. What is printed if the input is 30?


        b. What is printed if the input is 5?


        c. What is printed if the input is 20?


    • (5 points)   With input   33   75   14   96 what is printed? Watch the line feeds.
             for(i = 0; i < 3; i++)  {
                  cin >> x;
                  cout << x % 5 << "  ";
              }
              cout <<  endl << i << "  " << x << endl;
      
      


    • (8 points)   What is the output from the program below?
              #include<iostream>
              using namespace std;
      
              void mystery(int&, int);
      
              int main()
              {
                int x, y, z;
      
                x = 22;  y = 44; z = 66;
                mystery(x, y);
                cout << "In main:  "  << x << " " << y << " " << z << endl;
      
              }
      
              void mystery(int& x, int b)  {
                int z;
              
                x = 33;  b = 55; z = 77;
                cout << "In mystery:  "  << x << " " << b << " " << z << endl;
              }
             



    • (4 points)

    • (8 points)   Study the code below.
           for(i = 2, x = 1; i <= 10 && x <= 20; i++)
             x = x * i;
           cout << x << "  " << i << endl;

    • (4 points) You have the following declarations:
          char array[NROWS][NCOLS];    // NROWS and NCOLS are global constants
      
      Write a code segment to put a dollar sign in the upper right and lower left corners of array.




    • (4 points) You have the following code:
          const int SIZE = 10;
          int array[SIZE] = {0}, index;
          
          for(index = 1; index < SIZE; index += 2)   // start at index 1
            array[index] = 5;
      Fill in the values for the array elements.
                    0    1    2    3    4    5   6    7    8    9 
        


    • (5 points) Given the following declarations, what value is computed by each of the program statements below? For part e, include the decimal point, and show your answer to two decimal places. Watch integer division!
          int w = 256, x = 43, y = 10, z;
          double c;
      
          a.  z = x/y + y/x;      z:
      
          b.  z = x % y;          z:
      
          c.  z = w % y * y;      z:
         
          d.  z = w % 100 / y;    z:
      
          e.  c = x/y; (Careful!) c:  


    • (4 points) Write one logical expression to test whether x is in the range -10 through +10, inclusive. Note: all we want is the expression.



    • (4 points)   Carefully study the nested loops. What is displayed?
        a. i = j = x = y = 0; // set variables to 0 for(i = 15; i > 10; i--) { x++; for(j = 44; j < 44; j++) y++; } cout << i << " " << j << " " << x << " " << y << endl; b. i = j = x = y = 0; // set variables to 0 for(i = 10; i < 10; i++) { x++; for(j = 0; j > 33; i--) y++; } cout << i << " " << j << " " << x << " " << y << endl;



    • (8 points) Examine the following code segment.
        ifstream infile;
        infile.open ("mydata.dat");
        if (infile.fail())
        {
          cout << "Input file failed to open.\n");
          exit(1);
        }
        
      
        a. What actual file holds the data originally?
      
      
      
        b. Is infile an object or a class?
      
      
        
        c. What effect does exit(1) have on the program?
      
      
      
        d. True or false, the open statement initializes the
           stream variable infile by connecting it to mydata.
      
      
      


    BONUS:   In recognition of the outstanding effort of the Tennessee Lady Vols basketball team, this test has 25 free points rather than 20. Congratulations on winning the SEC!!!


    Key for CS 102 Exam I, March 10, 2011

      1.   a. -15  -1
           b. 80  60
           c. 31  50
      
      2.  a. if(x <= y)  cout << "Okay.\n";  -OR-  if(y >= x)  cout << "Okay.\n";
          b. if(x < y)  cout << "Okay.\n";   -OR-  if(y > x)   cout << "Okay.\n";
          c. if(x <= y)  cout << "Okay.\n";  -OR-  if(y >= x)  cout << "Okay.\n";
      
      3.  5  5
      
      4.  1. 27
          2. 24
          3. 56
      
      5.  3  0  4  
          3  14 
      
      6.  In mystery:  33 55 77
          In main:  33 44 66 
      
      7.  a. Function mystery() truncates the two rightmost digits of a.
          b. int mystery(int);  -OR-  int mystery(int parm_name);
      
      8.  a. 24  5
      
          b. i = 2;
             x = 1;
             while(i <= 10 && x <= 20)
             {
               x = x * i;
               i++;
             }
      
      9.  array[0][NCOLS-1] = '$';
          array[NROWS-1][0] = '$';
      
      10. 0  5  0  5  0  5  0  5  0  5
      
      11. a. 4
          b. 3
          c. 60
          d. 5
          e. 4.00
      
      12. if(x >= -10 && x <= 10)
      
      13. a. 10  44  5  0
          b. 10  0  0  0
      
      14.	a. mydata.dat
          b. object
          c. The program terminates.
          d. true