2021 COSC 202 Midterm: Answers and Grading Guide

October 12, 2021.
James S. Plank

1-TF: Questions 1 through 12

These were true-false questions from banks. Each was worth 2 points with no partial credit. Here are answers to the example exam:
  1. True.
  2. True.
  3. False -- it calls the assignment overload.
  4. True.
  5. False -- you need to also declare a constructor with no parameters.
  6. False -- you should throw an exception.
  7. False -- regardless of whether you use a copy constructor a class will have a default one defined.
  8. True -- you lose the pointer on iterations 1 through 4.
  9. False -- protected means that a method in the class ican call it.
  10. False -- you can do both. The second one won't be very useful.
  11. True -- once for x, and five times for the vector.
  12. No -- its regular constructor will be called five times.


2-V-of-V: Question 13

This came from a bank, where the program was the same, but the inputs were different. The question in the example yields the following array:

v[0]: 7, 3, 4, 2
v[1]: 1, 9, 8, 6
v[2]: 7, 5
v[3]: 7, 4, 3

So the answers were:

1: 4
2: 7
3: 6
4: 4

Grading: Each part was worth 2.5 points. I assigned the following partial credit:


3-Hash: Question 14

This came from a bank, where the hash table was shifted one or two places, but the interrelationship was the same. Here are answers to the example exam:
  1. 10/20, 1/2. 0.5, etc.
  2. 4
  3. One (I gave full credit to zero): Entry 4 is empty, so we may conclude instantly that "Frank" is not in the table.
  4. 6 and 7 are non-empty, so it goes into 8.
  5. 12 and 13 are non-empty, so it goes into 12+4 = 16.
  6. 7 is non-empty, so we try 7+1 = 8 which is is the answer.
Grading: Three points per part.

Partial Credit:

  1. 2 points for 10/19, 1 point for 5/8.
  2. 1.5 points if you're off by one or ten.
  3. No partial credit.
  4. 0.5 points if you were off by 8.
  5. 1 point if you were off by 1. 0.5 points if you were off by -14.
  6. 1 point if you were off by 12.


4-Ptr: Question 15

This came from a bank where the variable names were different and the pictures looked different, but the interrelationship between the variables is the same. Here are answers to the example exam:
  1. H
  2. K
  3. D
  4. B
  5. A
  6. I
  7. x4 -- the integer holding 51 doesn't have a variable, so it has to have been created with new.
  8. You can make cases for both yes and no here, so everyone got this one correct if they answered yes or no. The answer is really yes -- if you resize x1 and then set x6[1], that sets x1[0] to 0. However, I gave full credit to both answers.
  9. No. You can set x6[1] after x5 is declared, when its value is uninitialized.
Grading: Subquestions 1-6 were 1.5 points. The rest were 1 each. When you see the grading for subquestions 1-7, if your answer was "Yes", it means you got it correct. If your answer was "X-Y", then it means you answered X, and the correct answer was Y. The following answers got partial credit of 0.5 points:
B-D D-B H-I I-K J-I J-K K-H K-I 


5-Exc: Question 16

This one came from a bank, but all of them had the same flow and answers. Here is the answer to the example exam: The answer is

AFBDCEC

Grading: You received one point for each of these:


6-List: Question 17

First off, zero is an even number. Try Merriam Webster, MathWorld or Wikipedia.

There were a few ways to do this, with the cleanest being to loop through the vector by doing "i += 2" and calling push_back() on the list:

list <string> p(const vector <string> &v)
{
  list <string> rv;
  size_t i;

  for (i = 0; i < rv.size(); i += 2) {
    rv.push_back(v[i]);
  }
  return rv;
}

Grading: As with most programming questions, I typically start you with 10 points and make deductions. When your program is too far afield, I typically assign a few points and grade with "Please see the answer". If that's what you received, it meant that you answer was too far off, and you should be reading the program above to understand what the answer should have looked like.

Common deductions. As I mentioned, I was strict. Some of the deductions were not for correctness, but for logic and readability. You need to remember that others will eventually read your code, or you will eventually read your code again after a period of time. Your code should be as clean and readable as possible, and with a simple and straightforward question like this one, I expected clean and readable code, even on an exam.

All of the following were one point deductions:

All of the following were two point deductions:

These were larger deductions:


Question 18

The approach is to build a vector of vectors. Let's call it v. Then: The best code to do this only uses an if statement to test for throwing the exception. Otherwise, you can find the proper index with (s[0]-'A')/7. That's the code below. Only one student gave me this answer. The next best code used an if statement for each of those cases above. That answer also received full credit.

Here's the best code:

int main()
{
  vector < vector <string> > v;
  v.resize(4);
  string line;
  size_t index, j;

  while (getline(cin, line)) {
    if (line[0] < 'A' || line[0] > 'Z') throw (string) "Bad Line";
    index = (line[0] - 'A') / 7;
    v[index].push_back(line);
  }

  for (index = 0; index < v.size(); index++) {
    for (j = 0; j < v[index].size(); j++) cout << v[index][j] << endl;
  }
  return 0;
}

Again, if your grade said "Please see the answer", it means that you were too far off, and you received 0-5 points. If this happpened to you on either question, I believe you should be programming more. Work on D2 250's from Topcoder -- I have lots of them (besides the in-lab programs) with annotated solutions: http://web.eecs.utk.edu/~jplank/topcoder-writeups/.

Grading: You typically started with 16 points and received deductions. The three big ones were:

Here are other common deductions: I didn't take off for the following, but I noted them, because they are programming practices that you should avoid: