CS140 Midterm 1

Spring 2018

Instructions

  1. Write your name clearly at the top of the exam.
  2. This paper exam has 9 problems worth 105 points.
  3. The coding portion of the exam is worth 45 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. (12 points) For each of the following questions, select the best answer from the following list. Sometimes two answers may seem close but these terms were precisely defined in either lab or lecture so you should choose the term that most precisely fits the definition:

    linkercompilerlscdconstructor
    g++vectorarraystructclass
    privateprotectedpublicsubstrfind
    makemakefileargcargvvi
    mutator methodaccessor methodheader fileobject fileexecutable

    1. _________________________ The Unix command that changes the directory.

    2. _________________________ The access protection you should use to declare the member functions of a class that are in the class's Application Programmer Interface (API).

    3. __________________________ The name of the files produced when you execute g++ with the "-c" option (these files have a .o extension).

    4. _________________________ The name of a method that modifies a class's data.

    5. _________________________ The software that combines two or more object files (.o files) into an executable and replaces references to functions with jumps to functions that are defined in other files.

    6. _________________________ The name of the file where you put class declarations that you wanted included in multiple files.

  2. (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. In what file should the method definitions for Picture be placed? Give the name of the file, following the naming conventions discussed in class.
      
      
      
      
    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.
      
      
      
      
      
             

  3. (8 points) The following two constructors both correctly initialize the variables in the following class:
    class Person {
          string firstname;
          string lastname;
          string address;
    }
    
    (a)(b)
    Person::Person(string fn, string ln, string addr) {
        firstname = fn;	  
        lastname = ln;
        address = addr;
    }
    	
    Person::Person(string fn, string ln, string addr):
        firstname(fn), lastname(ln), address(addr) {}
    			
    Answer the following questions.

    1. ______ Which constructor is more efficient (a or b)?
    2. Why is your selected constructor more efficient? Your answer must be no longer than 3 sentences.
  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) separate chaining, and b) quadratic probing. For separate chaining, use commas to separate the keys that would go into an entry. For example, if the keys x, y, and z all went into the same entry, you would write (x, y, z) in that entry. You should use the hash function:
      h(key) = key % 11
      
       Separate ChainingQuadratic Probing
      0  
      1  
      2  
      3  
      4  
      5  
      6  
      7  
      8  
      9  
      10  

    2. For quadratic probing please also complete the following table showing the comma, separated list of entries you would probe in order to insert each key (e.g., if you examined entries 2, 3, and 7 in order to insert 32, then you would write (2, 3, 7) next to 32 in the below 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 quadratic probing hash table.

      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. (5 points) Suppose that you search a maze collecting animals that are denoted by the letters A-Z. Each animal is worth points, with A standing for 2 points, B standing for 4 points, C standing for 6 points, etc. (Z stands for 52 points). You are given the following variable declarations:
        char animal;   // the letter of the most recently collected animal
        int totalPoints; // the total number of points earned thus far
      
    Write a single C++ statement that will convert the character stored in animal to the correct point value and add that point value to totalPoints
    
    
    
    

  8. (10 points) Given the following variable declarations, write a printf statement that:
    string name;
    double salary;
    int age;
    
  9. (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 main() {
      istringstream buffer;
      int num;
      double num1;
      int sum1 = 0;
      double sum2 = 0;
      string input;
    
      while (cin >> input) {
        buffer.clear();
        buffer.str(input);
        if (buffer >> num1) {
          if (input.find('.') == string::npos) {
    	buffer.clear();
    	buffer.str(input);
    	buffer >> num;
    	sum1 += num;
          }
          else {
    	sum2 += num1;
          }
        }
        else {
          cout << input << endl;
        }
      }
      
      cout << "sum1 = " << sum1 << endl;
      cout << "sum2 = " << sum2 << endl;
    }
    			       
    UNIX> g++ mystery.cpp
    UNIX> cat input.txt
    3.6 14.5
    -5 Brad
      Smiley
    20. Jill Nancy 17.4
    50
    UNIX> ./a.out < input.txt 10 Mario