CS140 Midterm 2

Spring 2015

Instructions

  1. Write your name clearly at the top of the exam
  2. You must answer all the questions on the exam. The exam has 8 problems. Make sure that you have all 8 problems.
  3. Write your answers on the exam pages and hand in the exam when you are finished.
  4. You have until the end of the class period to finish the exam. When time is called you must immediately drop your pen/pencil and hand in your exam.
  5. Ensure that you write clearly and legibly and that your diagrams are easy to read. Failure to do so may result in the deduction of points.
  6. The exam is closed note, closed book, closed neighbor.
  7. The total points available for this exam (lab coding questions plus this paper exam) is 120 points. You will be graded out of 100 however, so in effect you have 20 extra credit points to work with.
  8. Good luck!

    
    
  1. (5 points) 0x3e9a + 0x4b3 = _______________ (I want the hexadecimal result)

  2. (5 points) Suppose you are given the following code fragment:
    unsigned char h = 23;
    h = (h << 2) ^ 0xa6;
    
    What is the hexadecimal value of h after the above code executes?

  3. (16 points) For each of the following questions circle the best answer from the above list. Sometimes it may seem as though two or more choices would be equally good. In those cases think about the operations that the data structures support and what I said about them in class. Then choose the data structure whose operations are best suited for the problem. You may have to use the same answer for more than one question.
     
         a. vector
         b. list
         c. deque
         d. map
         e. set
         f. multimap
         g. multiset
         h. hash table
    

  4. (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:
    
    
    
    
    

  5. (6 points) _____________________________________ What term is used to describe the problem with the following code? If you can't think of the term, then for 3 points give a 2 sentence or less answer that describes the problem.
      int *x;
      x = new int;
      *x = 40;
      x = new int;
      *x = 60;
    

  6. (10 points) Explain what is wrong with the following code segment. Use no more than 3 sentences to describe the problem.
        map<string, set<string> > friendsMap;
        map<string, set<string> >::iterator friends_it;
        set<string> friendsSet;
        string friend1, friend2;
        while (cin >> friend1 >> friend2) {
           friends_it = friendsMap.find(friend1);
           friendsSet = friends_it->second;
           friendsSet.insert(friend2);
        }
    
    The intent of the above code is to maintain a map keyed by name. For each name we keep track of that person's friends. The above code is supposed to add friend2 to friend1's set of friends. You should assume that the input is error-free.
    
    
    
    
    
    
    
    
    
  7. (10 points) Draw a list diagram that shows what the following list looks like pictorially after the following code executes. Make sure that you include sentinel nodes and that you show where each list iterator points. Your diagram should look like the ones in the class notes.
    list<string> names;
    list<string>::iterator nit, nit1;
    list<string>::reverse_iterator rnit;
    names.push_back("joe");
    names.push_back("mary");
    names.push_front("nancy");
    names.push_front("tom");
    nit = names.begin();
    nit++;
    rnit = names.rbegin();
    nit1 = names.end();
    
  8. (10 points) Assume you have the following code:
    class Person {
    public:
      string name;
      Person *bestFriend;
    
      Person(string n) { name = n; }
    };
    
    string names[] = { "mickey", "bugs", "daffy", "winnie" };
    vector<Person *> friends;
    Person *newPerson;
    int i;
    
    for (i = 0; i < 4; i++) {
        friends.push_back(new Person(names[i]));
    }
    for (i = 0; i < 3; i++) {
        friends[i]->bestFriend = friends[i+1];
    }
    friends[3]->bestFriend = friends[2];
    
    Draw a diagram that shows the vector, the Person objects that get created, and the objects to which each pointer points.