GRADE (TA use only) __________________

(1 point) USER NAME: ___________________________

CS 102 Exam I, October 2, 2007

  1. (4 points)
    1. Use the preprocessor directive #define to set the constant PI to 3.14159.


    2. Use the compile-time directive const to set the constant NUM to the integer 100.



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

            if(x != 23 && y)  
              x++;
            else if(x < 0 || y == 23)
              y--;
            else x += y;
    
            cout << x << "  " << y << endl; 
      1. x is -48 and y is 0

      2. x is 33 and y is 55

      3. x is 16 and y is 23

    • (4 points)   Hint: Use truth tables.
      1. Rewrite the following C++ statement to make it more robust (less prone to error).
          if(x / y && y != 0)    cout << x / y << endl;


      2. Rewrite the following C++ code segment to make it more efficient and to remove any unnecessary code.

              if(x < y && y > x)  cout << "Not good.\n";
              cout << "Carry on.\n";


      3. Rewrite the following C++ code segment to make it more efficient and to remove any unnecessary code.

              if(x < y && y >= x)  cout << "Not good.\n";
              cout << "Carry on.\n";


      4. Rewrite the following C++ code segment to make it more efficient and to remove any unnecessary code.

              if(x < y || y >= x)  cout << "Not good.\n";
              cout << "Carry on.\n";





    • (8 points)   What gets printed from each code segment below?
      1.    x = 5;  y = 5; 
           if (y <= x)
             if (x > 8)
               x++;
             else
               y--;
           cout << x << "  " << y << endl;

      2.    x = 5;  y = 5; 
           if (y <= x)  {
             if (x > 8)
               x++;
           }
           else
             y--;
           cout << x << "  " << y << endl;

      3.    x = 4;  y = 8; 
           if (y <= x)
             if (x > 8)
               x++;
             else
               y--;
           cout << x << "  " << y << endl;

      4.    x = 4;  y = 8; 
           if (y <= x)  {
             if (x > 8)
               x++;
           }
           else
             y--;
           cout << x << "  " << y << endl;

    • (6 points)   Examine the following code.
         cout << " Please enter an integer  ";
         cin >> x;
         switch(x)  {
           case 60:  x--;  x--;  break;
           case 5:   x = x * x;  
           case 31:  x--;  break;
           case 25:  x = 33;
           case 24:  x += 4;
           default:  x++;
         }
           
         cout << x << endl;
      
      1. What is printed if the input is 60?


      2. What is printed if the input is 5?


      3. What is printed if the input is 24?









    • (8 points)   Examine the code below and answer the questions.
              cin >> x >> y;
      
              if (x > 5)  
                if (y < 3)  
                  cout << "Electrical\n";
                else                
                  cout << "Engineering\n";
              else  
                if (y > 4)         
                  cout << "Computer\n";
                else              
                  cout << "Science\n"; 
        In terms of x and y, under what conditions will each word be printed?

        1. Electrical


        2. Engineering


        3. Computer


        4. Science


    • (6 points)   What is the output from the program below?
              #include<iostream>
              using namespace std;
      
              void func(int, int);
      
              int main()
              {
                int x, y, z;
      
                x = 30;  y = 34; z = 38;
                func(x, y);
                cout << "In main:  "  << x << " " << y << " " << z << endl;
      
              }
      
              void func(int x, int b)  {
                int z;
              
                x = 75;  b = 77; z = 72;
                cout << "In func:  "  << x << " " << b << " " << z << endl;
              }
             









    • (4 points)
      1. What (not how) does the following function do? That is, what happens to the argument?
          int mystery(int x)  
          {
            return x % 100;
          } 
      2. Write a prototype for function mystery( ).


    • (10 points)
      1. Write a complete C++ function maximum( ) that returns the maximum of three integers. Your code must have only one exit point to receive full credit.

















      2. Write one C++ statement to call maximum( ) and display:       The maximum is XXX.
        where XXX is the returned value. Don't forget the period.



    • (6 points)   With input   .56   .44   .22   .01 what is printed? Watch the line feeds.
             for(i = 0; i < 3; i++)  {
                  cin >> cents;
                  cout << cents << "  ";
              }
              cout <<  endl << i << "  " << cents << endl;
      
      

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


      2. Rewrite the code above as a functionally equivalent while loop.






    • (9 points)   Fill in the code described in the three *** comments.






    Key for CS 102 Exam I, October 7, 2007

    CS 102 Test 1               October 2, 2007
    
    1. 1. #define PI 3.14159
       2. const int NUM = 100;
    
    2. 1. -48  -1
       2. 34  55
       3. 17  23
    
    3. 1. if(y != 0 && x /y)  cout << x / y << endl;
    
       2. if(x < y)  cout << "Not good.\n";
          cout << "Carry on.\n";
       -OR- if(y > x)  cout << "Not good.\n";
            cout << "Carry on.\n";
    
       3. if(x < y)  cout << "Not good.\n";
          cout << "Carry on.\n";
    
       4. if(y >= x)  cout << "Not good.\n";
          cout << "Carry on.\n";
    
    4. 1. 5  4
       2. 5  5
       3. 4  8
       4. 4  7
    
    5. 1. 58
       2. 24
       3. 29
    
    6. 1. x > 5 and y < 3
       2. x > 5 and y >= 3
       3. x <= 5 and y > 4
       4. x <= 5 and y <= 4
    
    7. In func:  75  77  72
       In main:  30  34  38
    
    8. 1. mystery( ) returns the two rightmost digits of the argument.
       2. int mystery(int);    -OR-    int mystery(int identifier);
    
    9. 1. int maximum(int x, int y, int z)
          {
            int max;    // ultimately, holds max value
          
            max = x;    // assume the largest is x
            if(y > max)  max = y;
            if(z > max)  max = z;
            return max;
          }
    
       2. cout << "The maximum is " << maximum(x, y, z) << ".\n";
    
    10. 0.56  0.44  0.22  
        3  0.22
    
    11. 1. 24  5
    
        2. i = 2;  x = 1;
           while(i <= 10 && x <= 20)  {
    	     x = x * i;
             i++;
           }
           cout << x << "  " << i << endl;
    
    12. double circumference();
        Circle o_shape(10.14);
        return PI * radius * radius;
    
    BONUS 1. 91
          2. 536