CS202 Midterm Exam on 3/9/2023

The exam was given on canvas with many questions coming from banks. This is an example exam composed of one question from each bank.

Question 1 - 1 point

Enter your username. This is because Canvas doesn't automatically put your username into their exams.

Question 2 - 0 points

This is an essay box so you can write explanations to your answers if you feel the need. This is so that you don't clutter up your answers. You don't need to use this, but I've learned that some students want it.

Question 3 - 24 points

The following is in the file Animal.hpp:
#include <vector>
#include <string>

class Animal {
  public:
    Animal();
    Animal(const Animal &a);

    void ggg(std::string x, std::string &y) const;
    void ggg(int &i) const;
    void hhh(std::string x, const std::string &y);
    void iii(const int &i);

    std::string tiger;

  protected:
    int lion;
    std::string jaguar;
};

And the following is in the file Animaltest.cpp:

#include <vector>
#include <iostream>
#include "Animal.hpp"
using namespace std;

int main()                        
{                                 
  Animal a1, a2, a3, a4;            
  vector <Animal> av;               
  string s1, t1, s2, t2;          
  int i1, i2;                     
                                  
  i1 = 75;                        
  i2 = 85;                        
                                  
  s1 = "simba";                    
  t1 = "tony";                  
  s2 = "jaba";                    
  t2 = "speedy";                  
                                  
  a1.tiger = "sherekhan";
  a2 = a1;
  a3.tiger = "speedy";
  a4.tiger = "hangry";
  av.resize(1);
  av.at(0).tiger = "lazy";
  av.resize(4, a3);                
  
  a1.ggg(s1, t1);
  a2.ggg(i1);
  a3.hhh(s2, t2);
  a4.iii(i2);
}

Now, suppose that a friend has implemented all of the methods defined in Animal.hpp in the file Animal.cpp. Please answer the following questions. Every answer should be one word. For parts D through O, if there can be multiple answers, then answer multiple If the answer is the empty string, then answer empty.

A. Animal is a class that exhibits polymorphism. Please answer T or F: [A]

B. Animal defines a customized copy constructor. Please answer T or F: [B]

C. Animal defines a customized assignment overload. Please answer T or F: [C]

D. Please tell me the value of s1 just before main() returns: [D]

E. Please tell me the value of s2 just before main() returns: [E]

F. Please tell me the value of t1 just before main() returns: [F]

G. Please tell me the value of t2 just before main() returns: [G]

H. Please tell me the value of i1 just before main() returns: [H]

I. Please tell me the value of i2 just before main() returns: [I]

J. Please tell me the value of a1.tiger just before main() returns: [J]

K. Please tell me the value of a2.tiger just before main() returns: [K]

L. Please tell me the value of a3.tiger just before main() returns: [L]

M. Please tell me the value of a4.tiger just before main() returns: [M]

N. Please tell me the value of av.at(0).tiger just before main() returns: [N]

O. Please tell me the value of av.at(1).tiger just before main() returns: [O]


Question 4 - 18 points

Below is a hash table of size 20. I am only showing whether an entry in the hash table is empty (-) or full (X):
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
- - - X X - - - - X - - X X X X X - - X

In the following table, you are wanting to insert a new value into the hash table above. Please tell me into which index the new value will go. The table includes the hash value, the collision resolution function, and if necessary, a second hash value. Put your answers into the slot in the table. If there is no answer, then please answer -1.

Please answer each question independently -- in other words, answer each question according to the table above, as if you haven't answered any of the previous questions.

Question Hash Value Collision Resolution Strategy 2nd Hash Value if Necessary Your Answer
A369033Linear Probing-[A]
B1077784Double Hashing1686145[B]
C62794Double Hashing1945959[C]
D1320609Linear Probing-[D]
E342232Quadratic Probing-[E]
F418294Quadratic Probing-[F]
G1573160Linear Probing-[G]
H413252Double Hashing44968[H]
I1108415Quadratic Probing-[I]


Question 5 - 2 points

Please look at the following Unix commands, and enter the output of the last command. The answer will be one line with no spaces.
UNIX> cat aft.cpp
#include <iostream>
using namespace std;

int main()
{
  string s, o;

  while (cin >> s) o.push_back(s[0]);
  cout << o << endl;
  return 0;
}
UNIX> cat mop.txt
quo nnw chi
abo omy dun
ete
UNIX> g++ aft.cpp
UNIX> ./a.out < mop.txt

Question 6 - 2 points

Please look at the following Unix commands, and enter the output of the last command. The answer will be one line with no spaces.
UNIX> cat teal.cpp
#include <iostream>
using namespace std;

int main()
{
  int i;
  int sum;

  sum = 0;

  while (cin >> hex >> i) sum += i;
  cout << sum << endl;
  return 0;
}
UNIX> cat dunk.txt
e
a
1
x
6
UNIX> g++ teal.cpp
UNIX> ./a.out < dunk.txt

Question 7 - 2 points

Please look at the following Unix commands, and enter the output of the last command. The answer will be one line with no spaces.
UNIX> cat shin.cpp
#include <iostream>
using namespace std;

int main()
{
  int i;
  int j;
  int p;

  j = -1;
  p = 0;

  while (cin >> i) {
    if (j == -1) j = i;
    p = p ^ i;
  }
  cout << (p ^ j) << endl;
  return 0;
}
UNIX> g++ shin.cpp
UNIX> echo 1061 2915 | ./a.out

Question 8 - 3 points

Please look at the following Unix commands, and enter the output of the last command. The answer will be one line with no spaces.
UNIX> cat sat.cpp
#include <iostream>
using namespace std;

int main()
{
  int n, i;
  string s;
  string p;

  while (cin >> n) {
    for (i = 0; i < n; i++) {
      if (cin >> s) p += s[0];
    }
  }
  cout << p << endl;
  return 0;
}
UNIX> cat most.txt
3 3
8 2
4 1
9 4
5 5
0 6
7
UNIX> g++ sat.cpp
UNIX> ./a.out < most.txt

Question 9 - 3 points

Please look at the following Unix commands, and enter the output of the last command. The answer will be one line with no spaces.
UNIX> cat jeep.cpp
#include <vector>
#include <iostream>
using namespace std;

int main()
{
  int n, i;
  string s;
  vector <string> sv;
  
  sv.resize(5);

  while (cin >> n >> s) sv[n%5] += s;

  s = "";
  for (i = 0; i < sv.size(); i++) s += sv[i];

  cout << s << endl;
  return 0;
}
UNIX> cat icy.txt
64 40
96 32
10 69
31 99
0 17
89 25
UNIX> g++ jeep.cpp
UNIX> ./a.out < icy.txt

Question 10 - 3 points

Please look at the following Unix commands, and enter the output of the last command. The answer will be one line with no spaces.
UNIX> cat deck.cpp
#include <vector>
#include <iostream>
using namespace std;

int main()
{
  int i, j;
  string rv;

  vector < vector <int> > dv;

  dv.resize(5);
  for (i = 3; i <= 15; i += 3) {
    dv[i%5].push_back(i/3);
  }
  for (i = 0; i < dv.size(); i++) {
    for (j = 0; j < dv[i].size(); j++) {
      rv.push_back('a' + dv[i][j]);
    }
  }
  cout << rv << endl;
  return 0;
}
UNIX> g++ deck.cpp
UNIX> ./a.out

Question 11 - 12 points

The following program is in p.cpp:

#include <vector>
#include <iostream>
#include <sstream>
using namespace std;

int main()
{
  vector <int> a;
  vector <int *> p, q;
  vector <int> *dp;
  int *b;
  int i, n;

  dp = &a;

  for (i = 0; i < 4; i++) p.push_back(new int);

  for (i = 0; i < 4; i++) {
    cin >> n;
    *(p[i]) = n;
  }

  for (i = 0; i < 4; i++) {
    cin >> n;
    q.push_back(p[n]);
    a.push_back(*(p[n]));
  }

  for (i = 0; i < 4; i++) {
    cin >> n;
    b = p[i];
    *b += n;
  }
    
  for (i = 0; i < 4; i++) {
    cin >> n;
    a[i] += n;
  }


  for (i = 0; i < 4; i++) cout << *(p[i]) << ":";
  cout << endl;

  for (i = 0; i < 4; i++) cout << *(q[i]) << ":";
  cout << endl;

  for (i = 0; i < 4; i++) cout << a[i] << ":";
  cout << endl;

  for (i = 0; i < 4; i++) cout << (*dp)[i] << ":";
  cout << endl;

  return 0; 
}

We compile and run the program on the following input (given to the program as standard input):

44 69 24 88
2 0 0 1
1 8 3 5
10 6 9 2

There are four lines of output, and each output is a word composed of four numbers separated by colons, with a colon at the end. Like:

1:2:3:4:

There are no spaces in the output.

Please enter the first line of output:
Please enter the second line of output:
Please enter the third line of output:
Please enter the fourth line of output:


Question 12 - 15 points

Implement a procedure called ltovl() that takes a single parameter -- a const, reference parameter that is a list of strings. It should return a vector of lists of strings. The vector's size should be two. The list in element 0 of the vector should contain all of the strings in the input list that begin with a capital letter. The list in element 1 should contain all of the other strings in the input list. The strings in both lists should be in the same order in which they appear in the input list.

After that, implement a main() that tests ltovl(). It reads strings on standard input and appends them to a list. It then calls ltovl() on the list and stores the answer. It prints the answer by traversing the vector, and for each element of the answer, it:

I want all of the details correct here, including include files, namespace directives, proper types and syntax. No "auto" variables.

Remember to set the format of your essay box to "preformatted", and you can resize it to make it bigger while you write your program.

Here's example input and output to the whole program:

UNIX> g++ program.cpp
UNIX> cat input.txt
fred Luther binky Baby-Daisy Toad thorpaw 
aesthete persecute
   Seagram

escadrille           Heterodyne
UNIX> ./a.out < input.txt
0 Luther Baby-Daisy Toad Seagram Heterodyne
1 fred binky thorpaw aesthete persecute escadrille
UNIX>