Sample MT2 (Gradescope) Solutions
Q1 Dynamic allocation
(Expected time: 2 minutes): Indicate whether each of the following is a valid/necessary reason to use dynamic allocation (i.e. we should or must use dynamic allocation for the given reason).
1.1) When we want to use pass-by-reference.
( ) True (X) False
1.2) When the size of an array to be allocated is unknown.
(X) True ( ) False
1.3) When we want the allocation to be on the stack
( ) True (X) False
1.4) When we need to allocate a pointer to a pointer
( ) True (X) False
Q2 Multiple choice
(Expected time 6 minutes)
2.1) The expression new vector<int>[10]
will return what type of data:
( ) int*
(X) vector<int>*
( ) vector<int>
( ) vector< vector<int> >
2.2) When an object is allocated, a constructor for the object will only be called if it is allocated on the stack (e.g. BigInt b("123")
) and not when it is dynamically allocated object (e.g. new BigInt;
).
( ) True (X) False
2.3) A class can have more than 1 destructor?
( ) True (X) False
2.4) Given the following code in a function:
string s;
string* p = &s;
Then, it would be appropriate to deallocate s by calling:
delete p;
( ) True (X) False
2.5) This code would have memory errors and/or segfault (yes or no):
vector<int> y(3);
y.push_back(5);
( ) True (X) False
2.6 A vector can read the front item in $O(1)$.
(X) True ( ) False
2.7) An ifstream
behaves similarly to what other object discussed in this class?
( ) char* (X) cin ( ) cout ( ) string ( ) deque
2.8) Given the following code:
int x;
char buf[100];
ifstream ifile("dat.txt");
ifile >> x;
ifile.getline(buf,100);
Suppose dat.txt
file contained the following line of text:
103 midterm 2 on Thursday
What will the contents of buf
be?
( ) 103 midterm 2 on Thursday
(X) _midterm 2 on Thursday
(where _
is a space character)
( ) midterm 2 on Thursday
( ) midterm
Q3 Command Line Arguments
(Expected time: 8 minutes) Consider the following program:
#include <iostream>
using namespace std;
int main(int argc, char * argv[])
{
char* mystr = new char[argc];
for(int i=0; i < argc-1; i++){
mystr[i] = argv[argc-i-1][i];
}
mystr[argc-1] = '\0';
cout << mystr << endl;
// About to return
return 0;
}
3.1) Determine the output of the program (assumed it is named prog1
) if it is run with the following command line arguments:
$ ./prog1 abcde fghij klmno pqrst
(Just enter exactly what will be printed by this program but don’t worry about the endl
).
Answer: plhd
3.2) Write a line of code that should replace // About to return
to make the program fully correct. Put a space between each keyword or symbol you use.
Answer: delete [] mystr;
Q4 Tracing
(Expected time: 7 minutes) Consider the program below and specifically whether lines accessing data members will compile due to public
/private
access issues.
4.1) Line 7 is valid (will compile):
(X) True ( ) False
4.2) Line 11 is valid (will compile):
(X) True ( ) False
4.3) Line 20 is valid (will compile):
( ) True (X) False
4.4) Line 21 is valid (will compile):
(X) True ( ) False
4.5) Line 25 is valid (will compile):
( ) True (X) False
4.6) Line 27 is valid (will compile):
(X) True ( ) False
4.7) A1’s doit()
could correctly be marked as a const
member function and the code would compile.
( ) True (X) False
4.8) A2’s process()
could correctly be marked as a const
member function and the code would compile.
(X) True ( ) False
Q5 Constructors
(Expected time: 12 minutes) Consider the following program and then show EXACTLY what would be printed on each line of output. If the program outputs few lines than answer blanks below, just enter NA
in the blank.
5.1) 1st line of output:
Answer: C1i
5.2) 2nd line of output:
Answer: f1:start
5.3) 3rd line of output:
Answer: C1d
5.4) 4th line of output:
Answer: f1:end
5.5) 5th line of output:
Answer: val=3
5.6) 6th line of output:
Answer: val=3
5.7) 7th line of output:
Answer: NA
5.8) 8th line of output:
Answer: NA