CS140 Midterm Exam. February 17, 2011. James S. Plank

In all of these questions, when I use variables they will be of the following types: If I give a code fragment, you may assume that it is properly defined within a main() program, and it has all the requisite include statements. In particular, there are no compilation problems with any of the programs or code snippets on this test.

Question #1: How do I read an integer on the command line?

A: cin >> i;
B: i = argv[1];
C: iss = argv[1]; iss >> i;
D: s = argv[1]; i = s.atoi();
E: cin >> s; i = s.atoi();
F: oss.str(cin); oss >> i;
G: iss.str(argv[1]); iss >> i;
H: cin >> iss; iss >> i;
I: cin >> oss; oss >> i;
J: oss.str(argv[1]); oss >> i;
K: oss = argv[1]; oss >> i;
L: iss.str(cin); iss >> i;

Question #2: How do I read an integer from standard input? Use the same answers as for Question #1.

Question #3: Suppose I write a procedure avg() whose job is to average a vector of integers. Which of the following is the proper prototype for avg()?

A:void avg(vector <int> &v, int &retval);
B:double avg(vector <int> &v);
C:void avg(vector <int> &v, double &retval);
D:double avg(vector <int> v);
E:int avg(vector <int> v);
F:int avg(vector <int> &v);
G:void avg(vector <int> v, double &retval);
H:void avg(vector <int> v, int &retval);

Question #4: Why should you declare data that is part of a class as protected and methods as public?

A: Because it makes the programs easier to read.
B: Because the compiler won't allow it otherwise.
C: Because that way, users of the class cannot mess with the data.
D: Because you can change a method's implementation that way.
E: Because it is more efficient from a performance standpoint.

Question #5: Suppose I say: i = 14; printf("%02x\n", i);. What will be the output?

A: 14
B: 0014
C: 0x14
D: 0x0014
E: 16
F: 0016
G: 0x16
H: 0x0016
I: d
J: 0d
K: 00d
L: 0xd
M: 0x0d
N: 0x00d
O: e
P: 0e
Q: 00e
R: 0xe
S: 0x0e
T: 0x00e
U: f
V: 0f
W: 00f
X: 0xf
Y: 0x0f
Z: 0x00f

Question #6: Which of the following prints the string s's value only if it is an integer?

A: oss.clear(); oss.str(s); if (!(oss >> i)) cout << i << endl;
B: if (!(s >> i)) cout << i << endl;
C: i = atoi(s.c_str()); if (i != 0) cout << i << endl;
D: iss.clear(); iss.str(s); if (iss >> i) cout << i << endl;
E: oss.clear(); oss.str(s); if (oss >> i) cout << i << endl;
F: iss.clear(); iss.str(s); if (!(iss >> i)) cout << i << endl;
G: if (!(i = s.int())) cout << i << endl;
H: if (s >> i) cout << i << endl;
I: if (i = s.int()) cout << i << endl;
J: i = atoi(s); if (i != 0) cout << i << endl;

Question #7: What is the proper way to declare a variable dv that is a vector of vector of doubles?

A: vector <double> dv();
B: double **dv;
C: vector <vector <double>> dv;
D: Before any code: typedef vector <double> DVec; Then: dv = vector <DVec>;
E: Before any code: typedef vector <double> DVec; Then: vector <DVec> dv;
F: vector <double> dv(10);
G: vector <double> dv[];

Question #8: What is the output of the following program?

main()
{
  vector <int> v;
  int i;

  v.resize(5, 5);
  v.push_back(1);
  v.resize(10, 10);
  v.resize(8);
  v.resize(10, 11);
  for (i = 0; i < v.size(); i++) printf("%d ", v[i]);
  printf("\n");
}

A: 1 2 3 4 5 1 10 10 11 11
B: 11 11 11 11 11 11 11 11 11 11
C: 10 10 10 10 10 10 10 10 11 11
D: 5 5 5 5 5 1 10 10 11 11
E: 1 2 3 4 5 6 7 8 9
F: 5 5 5 5 5 10 10 10 10 10
G: 5 5 5 5 5 10 10 10 11 11 11 11 11 11 11 11 11 11
H: 5 5 5 5 5 10 10 10 11 11
I: 5 5 5 5 5 10 10 10 10 10 10 10 10 10 10
J: 5 5 5 5 5 1 10 10 11 11 11 11 11 11 11 11 11 11
K: 5 5 5 5 5 1 10 10 10 10 10 10 10 10 10 10
L: 10 10 10 10 10 10 10 10 10 10

Question #9: I want to print s and i so that that s is left-justified and padded to 30 characters, then there is a space, and then i is right-justified and padded to 5 characters:

A: printf("%-30s %-05d\n", s, i)
B: printf("%-30s %-05d\n", s.c_str(), i)
C: printf("%-30s %-5d\n", s, i)
D: printf("%-30s %-5d\n", s.c_str(), i)
E: printf("%-30s %05d\n", s, i)
F: printf("%-30s %05d\n", s.c_str(), i)
G: printf("%-30s %5d\n", s, i)
H: printf("%-30s %5d\n", s.c_str(), i)
I: printf("%30s %-05d\n", s, i)
J: printf("%30s %-05d\n", s.c_str(), i)
K: printf("%30s %-5d\n", s, i)
L: printf("%30s %-5d\n", s.c_str(), i)
M: printf("%30s %05d\n", s, i)
N: printf("%30s %05d\n", s.c_str(), i)
O: printf("%30s %5d\n", s, i)
P: printf("%30s %5d\n", s.c_str(), i)

Question #10: The program q10.cpp is to the right. What is the output of this program when it is run as follows:
UNIX> q10 ABC 
main(int argc, char **argv)
{
  char *cs;
  string s1, s2;

  cs = argv[1];
  s1 = argv[1];
  s2 = s1;

  cs[0] = 'X';
  s1[1] = 'Y';
  s2.push_back('Z');

  cout << argv[1] << endl;
  cout << cs << endl;
  cout << s1 << endl;
  cout << s2 << endl;
}





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

string a(string s)
{
  s[0] += 2;
  s.push_back(s[0]);
  return s;
}

main(int argc, char **argv)
{
  vector <string> v;
  int i;

  v.push_back("A");
  for (i = 0; i < 4; i++) v.push_back(a(v[i]));
  for (i = 0; i < v.size(); i++) cout << v[i] << endl;
}

Question #11: What is the output of the program to the left.

Question #12: Suppose I turn the parameter declaration of a() to "string &s." Now what is the output of the program?

Question #13: Write a procedure i_like_tea(). This procedure should take a string as an argument, and returns a vector of integers. This vector should contain the indices of the words in the string that contain the letter 't', upper case or lower case. For example, if the input string is "Thor gets his thunder", it should return the vector { 0, 1, 3 }. Your procedure should use stringstreams, and the string method find(). Don't bother with include statements or using namespace std.