CS140 Midterm 1

Spring 2020

Instructions

  1. Write your name clearly at the top of the exam.
  2. This paper exam has 8 problems worth 100 points.
  3. The coding portion of the exam is worth 50 points.
  4. The total exam is 150 points and you will be graded out of 150 points. For example 125/150 will be scored as 83%.
  5. Ensure that you write clearly and legibly. Failure to do so may result in the deduction of points.
  6. No electronic devices are allowed and all electronic devices that you have with you must be turned off, including calculators.
  7. You may not use any notes for this exam.
  8. Good luck!

  1. (10 points) Suppose I want to declare a class named Picture and use it in a program named Photoshop.cpp. Answer the following questions:

    1. In what file should the class declaration for Picture be placed? Give the name of the file, following the naming conventions discussed in class.
      
      
      
    2. How would you include in a .cpp file the file containing Picture's class declaration?
      
      
      
      
    3. Using a single g++ statement, how could I compile all of these files into an executable named Photoshop?
      
      
      
    4. Suppose instead that I wanted to separately compile these files and then link together their object files into an executable named Photoshop. Write the set of statements that would be required to perform this separate compilation and then linking into the executable.
      
      
      
      
      
    5. Suppose I want to execute Photoshop from the command line by 1) redirecting stdin from a file named bvz.pgm, and 2) redirecting stdout to a file named edited_bvz.pgm. Write the command that I should use to execute Photoshop.
      
      
      
      
      
             

  2. (6 points) The following program is supposed to print the sum of two numbers.
    #include <iostream>
    using namespace std;
    
    int main(int argc, char **argv) {
       int num1, num2;
    
       cin >> num1 >> num2;
       cout << num1 + num2 << endl;
       return 0;
    }
        

    When the user tries to execute the program with the command:

    ./sum 3 6
    	
    nothing happens--nothing gets printed and you do not get returned to the unix prompt. It appears as though the program is in an infinite loop, but it is not. Why is the program unresponsive and what must you do to get the program to print something and terminate? Your answer must be no longer than 4 sentences.

  3. (16 points) Declare a class named Checkerboard that you could place in a .hpp file. Do not write the method definitions as these would not go in a separate .cpp file. Checkerboard should have the following variables and methods:

  4. (12 points) The following code fragment is trying to shift all elements in a vector 3 entries to the right and replace the vacated entries with 0:
        vector<int> v;
        int i;
        int originalSize;
    
        // (1) resize the vector
        originalSize = v.size();
        v.resize(v.size() + 3);
    
        // (2) shift the existing entries to the right
        for (i = 0; i < originalSize; i++) {
            v[i+3] = v[i];
        }
    
        // (3) fill the vacated entries with 0's
        for (i = 0; i < 3; i++) {		    
            v[i] = 0;
        }		    
    
    Suppose that v contains the contents:
    01234567
    1020304050607080

    The code fragment is intended to produce the following result:

    012345678910
    0001020304050607080

    The code fragment as written does not achieve its intended purpose. Answer the following questions:

    1. Show what the contents of the vector actually look like after the above code fragment executes. I have provided too many vector entries. Leave the undefined entries empty.

      0123456789101112
       

    2. What is the code fragment doing wrong? You must limit your answer to 3 sentences or less.
    3. Rewrite one or more of the numbered code blocks so that the code fragment correctly shifts the existing vector elements and assigns 0's to the vacated entries. Clearly indicate which numbered code block(s) you are altering.
  5. (24 points)

    1. Insert the keys 32, 12, 48, 19, 52, 58, 30, 25 into the following two hash tables using a) linear probing, and b) quadratic probing. You should use the hash function:
      h(key) = key % 11
      
       Linear ProbingQuadratic Probing
      0  
      1  
      2  
      3  
      4  
      5  
      6  
      7  
      8  
      9  
      10  

    2. For both linear and quadratic probing please complete the following two tables showing the comma, separated list of entries you would probe in order to insert each key (e.g., if linear probing examined entries 2, 3, and 7 in order to insert 32, then you would write (2, 3, 7) next to 32 in the linear probing table--this is only an example--it is not the actual set of entries you will probe for 32). This will allow us to assign you partial credit if you do not get the entries correct in the linear and quadratic probing hash tables.

      keylinear probing
      32 
      12 
      48 
      19 
      52 
      58 
      30 
      25 
      keyquadratic probing
      32 
      12 
      48 
      19 
      52 
      58 
      30 
      25 

  6. (12 points) Please read the following instructions before attempting this problem:

    1. 0x6D = ____________________________
      
            
    2. (1001 0101) ^ (1010 1100) = _____________________________
      
      
      
      
      
      
            
    3. (1001 0101) | (1010 1100) = _____________________________
      
      
      
      
      
      
            
    4. (1010 1100) >> 3 = _______________________________
      
      
      
      
      
      
      		  
  7. (8 points) Suppose I have the following declarations and code:
         int a, b;
         int *x, *y;
    
    Also suppose that the above variables are assigned the following memory addresses:
         a: 0x1000
         b: 0x1004
         x: 0x1008
         y: 0x100c
     
    After the following code sequence executes, what are the values of a, b, x, and y?
           x = &b;
           y = x;
           a = 10;
           b = 40;
           *x = 30;
           *y = *x * 3;
    
         a:
    
    
         b:
    
    
         x:
    
    
         y:
    
    
    
    
    
  8. (12 points) Behold the following program that has been placed in a file named mystery.cpp. On the right are a set of Unix commands that will compile the program and run it on an input file. What is the output of the last Unix command?
    #include <iostream>
    #include <sstream>
    using namespace std;
    
    int mystery() {
       double num;
       int sum = 0;
       istringstream buffer;
       string input;
    
       while (cin >> input) {
          try {
             buffer.clear();
             buffer.str(input);
             if (buffer >> num) {
                 if (input.find('.') != string::npos) {
                     throw num;
                 }
                 else {
                    sum = sum + (int)num;
                 }
             }
             else {
                 cout << "Oops-that was not a number!\n";
                 throw (string) "input was not a number";
             }
             cout << "input = " << num << endl;
    	 cout << "intermediate sum = " << sum << endl;
          }
          catch (double n) {
             cout << input << " is a double\n";
          }
       }
       cout << "leaving sum\n";
       return sum;
    }
          
    int main() {
        int result;
        try {
           result = mystery();
        }
        catch (const string &e) {
           cout << e << endl;
           cout << "exiting program with an error\n";
           return 1;
        }
        cout << "sum = " << result << endl;
        return 0;
    }
    			       
    UNIX> g++ mystery.cpp
    UNIX> cat input.txt
    3.6 16 14.5
    -5 Brad
      Smiley
    20. Jill Nancy 17.4
    50
    UNIX> ./a.out < input.txt