Sabanci University: CS204: Advanced Programming, Fall 2014, FINAL Duration: 150 Minutes
Sabanci University: CS204: Advanced Programming, Fall 2014, FINAL Duration: 150 Minutes
/120
/22
/15
/16
/15
/15
/16
PLEASE READ BEFORE THE EXAM
The exam is a closed-book, closed-notes exam. Please read the questions carefully. If you
don’t want to answer a question, please put a check (✔) symbol to the boxes above to get
%20 of that question.
SU ID: _______________________
#ifndef _LinkedList_H
#define _LinkedList_H
#include <iostream>
using namespace std;
Node() {}
Node (const int& s,
Node<T>* link)
: info(s), next(link) {}
};
template <class T>
class LinkedList {
public:
LinkedList ();
~LinkedList ();
virtual void add(T val) {
cout << "what do I do?\n";
}
virtual void get(T& val) {
cout << "What do I do?\n";
}
bool isEmpty() {
return !head;
}
protected:
Node<T>* head;
};
#endif
#include “LinkedList.cpp”
LinkedList.h
1
Q1: The LinkedList class .h is given above and the .cpp file is below. Complete the implementation of
the destructor. (10 pts)
#include <iostream>
using namespace std;
}
LinkedList.cpp
2
Q2: Suppose you have the following LinkedListDemo.cpp. (15 pts)
int main () {
LinkedList<int> l;
return 1;
} LinkedListDemo.cpp
a) How do you create a project and get the executable for this file in Visual Studio C++. You are
only allowed to change LinkedListDemo.cpp. Do your modifications on the box above and state
which files you need to add to the project and to which directories. (5 pts)
b) Overload the << operator for the LinkedList class to print the elements in the list from first (head)
to the last element by putting only a single – (dash) in between. For example, if the list contains 1,
2, and 3 the output is 1-2-3. (Plase note that there is no newline or dash at the end). (7 pts)
LinkedList.h
c) What happens when you try to compile the project you created for LinkedListDemo.cpp if the
virtual function definitions in LinkedList.h were (3 pts)
#ifndef _IntQueue_H
#define _IntQueue_H
#include "LinkedList.h"
class IntQueue :
public:
IntQueue();
~IntQueue();
void add(int);
void get(int& val);
private:
};
#endif
IntQueue.h
a) Fill the first empty rectangular box in the IntQueue.h given above to have public inheritance from
LinkedList. Fill the second one to add a tail pointer to the IntQueue class which will be used to
efficiently enqueue an item. (3 pts)
b) A partial implementation of the IntQueue.cpp is given below. Please complete the implementation.
If the queue is empty during get, throw an exception which tells “the queue is empty”. You don’t
need to catch it here. (10 pts)
#include <iostream>
#include "IntQueue.h"
IntQueue::IntQueue() {
cout << "cons Q\n";
... //complete this part
}
IntQueue::~IntQueue() {
cout << "dest Q\n";
}
4
c) What is the output for the following main.cpp (assuming the IntQueue class is correctly
implemented as described above)? (2 pts)
#include <iostream>
#include "IntQueue.h"
using namespace std;
int main() {
IntQueue q;
q.add(1);
q.add(2);
q.add(3);
int a;
q.get(a);
cout << a << endl;
q.get(a);
cout << a << endl;
return 1;
}
mainQ3c.cpp
5
Q4: Suppose that you also inherited an IntStack class from the LinkedList class as you did for
IntQueue. Its implementation (the cpp file) is given on the left below. The header file is the same with
IntQueue.h (see Q3) except that IntStack does not have a tail pointer. (15 pts)
#include <iostream>
int main() {
#include "IntQueue.h"
IntQueue q;
#include "IntStack.h"
q.add(1);
q.add(2);
get_X(q); //X can be 1,2,3,4
void get_1(LinkedList<int> l) {
int a = -1;
return 1;
l.get(a);
}
cout << a << endl;
} mainQ4b.cpp
(continued)
void get_2(LinkedList<int>& l) {
int a = -1;
l.get(a);
cout << a << endl;
}
void get_3(IntQueue l) {
int a = -1;
l.get(a);
cout << a << endl;
}
void get_4(IntQueue& l) {
int a = -1;
l.get(a);
cout << a << endl;
}
mainQ4b.cpp
6
Assuming we are using the IntQueue class given in Q3, write the outputs when X in the main.cpp
above is replaced with 1, 2, 3, and 4. If you claim that the compiler complains for some value of X
do not give an output and put “does not compile” as your answer for that value and tell why. If
you claim that the program compiles but crashes after some point write the output until that point
and explain why it crashes. Otherwise, just write the output. (5 pts)
c) Answer the previous question (b) for the main function below (the part on the left having the
functions remains exactly the same). (5 pts)
int main() {
IntStack s;
s.add(1);
s.add(2);
get_X(s); //X can be 1,2,3,4
return 1;
}
mainQ4c.cpp
7
Q5: The IntStack implementation is given in Q3. (20 pts)
a) Add a move constructor and a copy constructor to the IntStack class. Keep your constructor
implementations self contained, i.e., write everything you need inside the function. Please do not
use an extra helper function. (10 pts)
IntStack.h
IntStack.cpp
8
#include <iostream>
#include "IntStack.h"
b) The IntStack class now has three constructors:
using namespace std; default (D), copy (C), and move (M). Write which of these
constructors are called for the statements on the left (s1 is
IntStack foo() {
IntStack x; given as an example) assuming no optimization is performed
return x; by the compiler. Please note that more than one constructor
}
type can be called for each statement. (5 pts)
int main() {
IntStack s1; s1: D
IntStack s2(s1);
IntStack&& s3 = foo();
IntStack s4(s3); s2:
IntStack s5 = s2;
IntStack s6 = move(s2); s3:
return 1;
} s4:
s5:
mainQ5a.cpp s6:
IntStack& sr1 = s;
IntStack& sr2 = foo();
sr2:
const LinkedList<int>& lr1 = foo();
LinkedList<int>&& lr2 = q;
const LinkedList<int>& lr3 = s;
lr1:
return 1;
}
lr2:
lr3:
mainQ5b.cpp
9
Q6: You are given a DumbStackUser class as shown below. (15 pts)
class DumbStackUser {
public:
DumbStackUser() {
cout << "def cons D\n";
static int var = 0;
var++;
if(var == 1) {
throw var;
}
};
DumbStackUser(bool actDumb) {
cout << "cons D\n";
try {
if(actDumb) {
s.get(val);
}
} catch (int a) {
cout << "caught int exception\n";
}
}
~DumbStackUser() {
cout << "des D\n";
}
private:
int val;
IntStack s;
};
DumbStackUser
Given the following mainQ6x.cpp’s please write the outputs (do not forget to include the constructor
and destructor calls). If you think that there is an uncaught exception, please write the output until that
exception is thrown and also write which throw statement is responsible.
a) (5 pts)
int main() {
try {
try {
DumbStackUser d = DumbStackUser(true);
} catch (string s) {
cout << "caught string exception: "<< s << "\n";
throw 1.0;
} catch (char s[]) {
cout << "caught char [] exception: " << s << "\n";
throw 1.0;
} catch (const char s[]) {
cout << "caught const char [] exception: " << s << "\n";
throw 1;
} catch (...) {
cout << "inner catch all\n";
throw 1;
}
} catch (double d) {
cout << "caught double exception\n";
} catch (...) {
cout << "outer catch all\n";
}
return 1;
}
mainQ6a.cpp
10
b) (5 pts)
int main() {
DumbStackUser d;
try {
DumbStackUser d;
} catch (int d) {
cout << "caught int exception: " << d << "\n";
} catch (...) {
cout << "outer catch all\n";
}
return 1;
} mainQ6b.cpp
c) (5 pts)
int main() {
try {
DumbStackUser d;
} catch (int d) {
cout << "caught int exception: " << d << "\n";
} catch (...) {
cout << "outer catch all\n";
}
DumbStackUser d;
return 1;
}
mainQ6c.cpp
11
Q7: Please answer the following questions: (15 pts)
a) Suppose that the IntStack class has a member mutex variable as shown below in IntStack.h. The
IntStack.cpp is untouched and you cannot change it.
. . .
#include <mutex>
You are asked to implement a function transfer(IntStack* stacks, int from, int to) which
gets an array of stacks and two integers from and to. It gets an element from the IntStack object
stacks[from] and adds it to stacks[to]. The function needs to be thread-safe. Please complete the
empty box in the implementation of transfer function below. (7 pts)
#include <iostream>
#include <string>
#include <thread>
#include <mutex>
#include "IntStack.h"
using namespace std;
}
int main() {
IntStack* stacks = new IntStack[10];
for(int i = 0; i < 10; i++) {
for(int j = 0; j < 100; j++) {
stacks[i].add(i);
}
}
thread threads[100];
for (int i = 0; i < 100; ++i) {
threads[i] = thread(transfer, stacks, i % 10, (i + 1) % 10);
}
return 1;
}
mainQ7a.cpp
12
#include <iostream>
#include <string>
#include <thread>
#include "IntStack.h"
using namespace std;
int a = 0;
void incrementFiveTimes() {
for(int i = 0; i < 5; i++) {
a++;
}
}
int main() {
thread threads[2];
for (int i = 0; i < 2; ++i)
threads[i] = thread(incrementFiveTimes);
13
Q8: Please answer the following questions. (15 pts)
a) Fill in the box below with an appropriate expression so that the function returns one's complement
of num. (3 pts)
return
}
b) What does the following function do? Please explain briefly. (4 pts)
int main() {
int (*plus)(int, int) = add;
int add;
add = oper(15, 10, plus);
cout << add << endl;
return 1;
}
d) Fill the rectangles in the output on the right for the code excerpt on the left. (4 pts)
int a[5];
int* ap = &(a[0]);
int* ap2 = &(a[4]);
cout << ap2 - ap << endl;
cout << "ap is " << ap << endl;
ap
is
0x7fff504b4ab0
cout << " ap2 is " << ap2 << endl;
ap2
is
14