0% found this document useful (0 votes)
24 views11 pages

Spring2015 Final Solutions

This document contains the final exam for CS204 (Advanced Programming) held on May 27, 2015. It includes various programming questions covering topics such as memory allocation, operator overloading, class destructors, GUI programming, exception handling, and multithreading. Each question has specific requirements and instructions for the students to follow.

Uploaded by

polatcan.polat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views11 pages

Spring2015 Final Solutions

This document contains the final exam for CS204 (Advanced Programming) held on May 27, 2015. It includes various programming questions covering topics such as memory allocation, operator overloading, class destructors, GUI programming, exception handling, and multithreading. Each question has specific requirements and instructions for the students to follow.

Uploaded by

polatcan.polat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

May 27, 2015

CS204 (Adv. Programming) Final Exam


1a 1b 1c 2 3 4 5 6 7 8 9 10 11 TOTAL

Name : İnci Çiftebağlar


ID :
Notes: a) Please answer the questions only in the provided space after each question.
b) Duration is 150 minutes.
c) Closed-book, closed-notes, no calculators and no computers. One single sided
A4 size cheat-note page is allowed.
d) There must be 11 (eleven) pages (including this one) in this booklet. Please
check it out!

QUESTIONS
1)
a) (2 points) Rewrite the following memory allocation and assignment code using new.
double * darr = (double *) calloc(21,sizeof(double));

double * darr = new double[21];

ANSWER: a b / c d – f * - e +
b) (2 points) What is the value of the expression HAMZA*PRANDELLI given the preprocessor
definitions below?
#define PRANDELLI 2
#define HAMZA PRANDELLI+10

2+10*2 = 22

c) (2 points) Fill in the blank. Please give the most general answer?

If you are using a templated class, the class definition, class implementation and the object
instantiations must be in the same _______Translation Unit______ for a proper compilation.
NAME:

2) (13 points)
Write a free function to overload the += operator for DynIntQueue class. This function takes two
DynIntQueue parameters; one for lhs (left hand side) and the other one for rhs (right hand side).
 The function should append the elements of the rhs queue to the rear of lhs queue.
 The rhs queue should not change in the function.
 The function's return type must be selected such cascaded assignments are possible.
Since this is a free function you cannot access the private data members of DynIntQueue class.
Moreover, you cannot assume that the function that you will write is friend of DynIntQueue class.
Thus, you can only use enqueue, dequeue and isEmpty member functions of DynIntQueue
class in your function implementation. Moreover, you can assume that DynIntQueue class has a
properly implemented deep copy constructor and you can use it.
The prototypes of the member functions and copy constructor of DynIntQueue class are given
below for your convenience. You cannot use any other queue functions.
void enqueue(int);
void dequeue(int &);
bool isEmpty() const;
DynIntQueue(const DynIntQueue &);
In this question, you may prefer not to attempt to solve it by signing the “not attempted” box below and secure 3
points. If you sign the “not attempted” box below, you accept that you did not answer this question and you will
receive 3 points. In this case, your answer will not be graded even if you write something as solution.
Not attempted

const DynIntQueue & operator += (DynIntQueue & lhs,


const DynIntQueue & rhs)
{
int val;
DynIntQueue temp(rhs);
while (!temp.isEmpty())
{
temp.dequeue(val);
lhs.enqueue(val);
}
return lhs;
}
NAME:

3) (12 points)
Consider the following two struct declarations, in which the pointers are dynamic arrays and
the integers keep the number of elements in the corresponding arrays.

struct City
{
string * towns;
int numTowns;
};

struct Country
{
City * cities;
int numCities;
};

The following data members exist in the private part of class World. Here, countries is a
pointer to point to a dynamic array of Country, and numCountries is the number of
elements in it.
private:
Country * countries;
int numCountries;

Write the implementations of the destructor for World class that returns al the dynamically
allocated memory back to the heap properly.
In this question, you may prefer not to attempt to solve it by signing the “not attempted” box below and secure 3
points. If you sign the “not attempted” box below, you accept that you did not answer this question and you will
receive 3 points. In this case, your answer will not be graded even if you write something as solution.
Not attempted

World::~World ()
{
int i, j, k;
for (i=0; i<numCountries; i++)
{
for (j=0; j<countries[i].numCities; j++)
{
delete [] countries[i].cities[j].towns;
}
delete [] countries[i].cities;
}
delete [] countries;
}
NAME:

4) (6 points) In a GUI application, there are two Check Box objects, one List Box object and
one Button as shown in the figure below.

When the Button is clicked, a message must be added to the List Box object according to the
following conditions.

 If both of the Check Box objects are selected, then the message is "Both items selected".
 If only one of the Check Box objects is selected, then the message is "Only one item
selected".
 If none of the Check Box objects is selected, then the message is "No items selected".

Write the body of the function below in order to carry out this task. You do not need to
control/implement horizontal and vertical scroll bars of the List Box object. The names of the
Check Box objects are check1 and check2. The name of the List Box object is list1.

void Ccs204_gui_questionDlg::OnBnClickedButton1()
{

if(check1.GetCheck() == 1 && check2.GetCheck() == 1)


{
list1.AddString(_T("Both items selected"));
}
else if(check1.GetCheck() == 1 || check2.GetCheck() == 1)
{
list1.AddString(_T("Only one item selected"));
}
else
{
list1.AddString(_T("No items selected"));
}

In this question, you may prefer not to attempt to solve it by signing the “not attempted” box below and secure 1 point. If you sign the “not
attempted” box below, you accept that you did not answer this question and you will receive 1 point. In this case, your answer will not be
graded even if you write something as solution.
Not attempted
NAME:

5) (8 points) Fill in the boxes of the program below with appropriate data types so that the
program catches all the exceptions and never crashes.

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

void hey (int x)


{
if (x % 2 == 0)
throw "even";
}

int main()
{
int ni;
double nd;
string s= "error";
try
{
cin >> nd;
if (nd > 3.14)
throw nd;
cin >> ni;
hey(ni);
try
{
cin >> ni;
hey(ni);
if (ni > 0)
throw s;
else
throw ni;
}
catch ( string catchvar1)
{ may be interchanged
cout << catchvar1 << endl;
}
catch ( int catchvar2)
{
cout << catchvar2 << endl;
}
}
catch ( double catchvar3)
{ may be interchanged
cout << catchvar3 << endl;
}
catch ( const char * catchvar4)
{
cout << catchvar4 << endl;
}
}
NAME:

6) (12 points) Consider the following two-dimensional doubly linked list node struct definition and a
sample depiction of this data structure. NULL NULL NULL NULL NULL NULL

struct node {
int info; NULL … NULL

node *right;
node *left; NULL … NULL

node *down;
node *up; … NULL
NULL
};
.. .. .. .. .. ..
. . . . … . .
This 2D doubly linked list is not
defined as a class. Moreover, … NULL
NULL
there is no particular head node
since all nodes are reachable … NULL
NULL
starting with any node.
NULL NULL NULL NULL NULL NULL

The function below is a partial solution for the following problem:


Write a function that takes a node pointer, say anyPtr, that points to any node of
this 2D doubly linked list as parameter. This function should return the number of rows
and number of columns of this 2D doubly linked list as two reference parameters.
Please note that anyPtr can point to any node in the list, either on the borders or
somewhere in the middle. You cannot make any assumption about the position of
anyPtr, but you can assume that anyPtr is not NULL.
However, the function is incomplete. Complete this function by filling the boxes with appropriate
statements or expressions. You are not allowed to delete or update anything. Moreover, you cannot
add anything other than the code that you are going to write in the boxes.
void MatrixSize (node * anyPtr, int & numRows, int & numCols)
{
numRows = 1 ;

numCols = 1 ;

while ( anyPtr->right != NULL )


{
anyPtr = anyPtr->right ;
}

while ( anyPtr->up != NULL ) In this question, you may prefer


{ not to attempt to solve it by
anyPtr = anyPtr->up ; signing the “not attempted” box
} below and secure 3 points. If you
sign the “not attempted” box
while ( anyPtr->left != NULL )
{ below, you accept that you did
not answer this question and you
numCols = numCols + 1 ; will receive 3 points. In this case,
your answer will not be graded
anyPtr = anyPtr->left ; even if you write something as
}
solution.
while ( anyPtr->down != NULL ) Not attempted
{

numRows = numRows + 1 ;

anyPtr = anyPtr->down ;
}
}
NAME:

7) (7 points) Consider the following class declarations and definitions.


class Sports
{
public:
Sports()
{
cout << "Sports Constructor" << endl;
myName="";
}
~Sports ()
{
cout << "Sports Destructor" << endl;
}
protected:
string myName;
};

class Tennis : public Sports


{
public:
Tennis()
{
cout << "Tennis Constructor" << endl;
myName="Tennis";

}
~Tennis()
{
cout << "Tennis Destructor" << endl;
}
};

a) What is the name of derived class? Which inheritance method is used here?

The name of the derived class is Tennis


The name of the inheritance method is public inheritance

b) What is the output of the main function given below?

int main()
{
Tennis t;
Sports s;
return 0;
Sports Constructor
}
Tennis Constructor
Sports Constructor
Sports Destructor
Tennis Destructor
Sports Destructor
NAME:

8 (12 points) The following main function on the left gives the output given on the right.
int main()
{
Bird birdy("cikcik");
Eagle kartal("BJK");
Eagle * aguia = new Eagle("Benfica");
birdy.fly();
kartal.fly(); I am a generic bird, I do not fly
aguia->fly(); My name is eagle BJK. I always fly
Bird * bptr = aguia;
My name is eagle Benfica. I always fly
bptr->fly();
birdy=kartal; My name is eagle Benfica. I always fly
birdy.fly(); I am a generic bird, I do not fly
}

Complete the class declarations and definitions of the Bird and Eagle classes below by writing all the
necessary code in the public parts (implementations should also be there) such that the abovementioned
main function gives the corresponding output. Do not add anything else other than the ones that can be
written in public parts. Moreover, do not add functions that are not needed.

class Bird
{
public:

Bird(string name)
:myName(name)
{}
string getName()
{
return myName;
}
virtual void fly()
{
cout << "I am a generic bird, I do not fly" << endl;
}

private:
string myName;
};

class Eagle : public Bird


{
public:

Eagle(string name)
: Bird(name)
{}
virtual void fly()
{
cout << "My name is eagle " << getName() << ". I always fly" << endl;
}

};
In this question, you may prefer not to attempt to solve it by signing the “not attempted” box below and secure 3 points. If you sign the “not
attempted” box below, you accept that you did not answer this question and you will receive 3 points. In this case, your answer will not be
graded even if you write something as solution.
Not attempted
NAME:

9)
a) (8 points)

Consider the following function.

int FourthStar (short num)


{
if (num>>15 == -1)
return 1;
if ((num & 0x8000) == 0)
return 2;
return 3;
}

What does the function return if the parameter num is:

i) 0

The function returns 2

ii) -10

The function returns 1

iii) 10

The function returns 2

iv) -1

The function returns 1

b) (3 points)

Write a single assignment statement that sets 4th bit of the integer variable CimBomStars to 1
without changing the other bits.

CimBomStars = CimBomStars | 8;
//0x8, 0x08, . . ., 0x00000008 OK
NAME:

10) (6 points)
Give all possible outputs that the multithreaded program below may generate when
executed. Clearly specify the output alternatives as output1, output2, output3, etc.

In this question, you may prefer not to attempt to solve it by signing the “not attempted” box below and secure 1
point. If you sign the “not attempted” box below, you accept that you did not answer this question and you will
receive 1 point. In this case, your answer will not be graded even if you write something as solution.
Not attempted

//Assume all #includes are done here


using namespace std;

//Assume ramdom_range function is defined here

void fourStars()
{
cout << "4 stars\n";
}

void threeStars()
{
this_thread::sleep_for(chrono::milliseconds
(random_range (0,1000)));
cout << "3 stars\n";
}

int main()
{
thread fb(&threeStars);
cout << "2 stars\n";
thread gs(&fourStars);
fb.join();
gs.join();
cout << "1 star\n";
return 0;
}

Output 1 Output 2 Output 3


2 stars 2 stars 3 stars
3 stars 4 stars 2 stars
4 stars 3 stars 4 stars
1 star 1 star 1 star
NAME:

11) (7 points) In this question you are asked to add necessary mutex controls (lock and
unlock) to a given incomplete multithreaded program.
The aim of the following incomplete program is to fill the global array Storage with all
values between 1 and 50000 in any order. Each value between and including 1 and 50000
must appear only once in Storage; however, the order of the values is not important.

The program below does not work as intended. In this implementation, the mutex lock()
and unlock() operations on theMutex object are missing. Specify the place(s) where you
need to have these operations. Mark these place(s) on the code clearly; ambiguous answers
will not be accepted.
Moreover, your solution must allow concurrent execution of the threads as much as possible.
In other words, your solution must not cause a particular thread to take over the execution
until it is over without letting the other threads to execute.
In this question, you may prefer not to attempt to solve it by signing the “not attempted” box below and secure 2
points. If you sign the “not attempted” box below, you accept that you did not answer this question and you will
receive 2 points. In this case, your answer will not be graded even if you write something as solution.

Not attempted

mutex theMutex;
int Storage[50000];
int index=0;

void StoreOne (int location, int value)


{
Storage[location ]=value;
}

void StoreAll (int start)


{
for (int i=0; i<10000; i++) theMutex.lock();
{
StoreOne(index, start+i);
index++;
} theMutex.unlock();
}

int main()
{
thread t[5];
int tc;
for (tc=0; tc<5; tc++)
{
t[tc]=thread(&StoreAll, 1+tc*10000);
}
for (tc=0; tc<5; tc++)
{
t[tc].join();
}
}

You might also like