You are on page 1of 8

May 4, 2019

CS204 (Adv. Prog.) Second Midterm Exam


1 2 3 4 5 6 7 TOTAL

Name and Lastname : Son Listesilici


SUNET ID :
Notes: a) Please answer the questions only in the provided space after each question.
b) Duration is 110 minutes.
c) Closed-book, closed-notes, no calculators and computers. One single sided A4
size handwritten cheat-note page is allowed.
d) There must be 8 (eight) pages (including this one) in this booklet. Please check
it out!
QUESTIONS
1) (8 points) What is the output of the following program?

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

string & update (string * spt)


{
(*spt)[1] = 'a';
return *spt;
}

string update (string & srf)


{
srf[1] = 'i';
return srf;
}

int main () OUTPUT


{
string s = "loveCS"; saveCS
update(&s)[0] = 's'; liveCS
cout << s << endl; lateCS
s = "loveCS";
update(s)[2] = 'k';
cout << s << endl;

string * p = new string;


*p = "loveCS";
update(p)[2] = 't';
cout << *p << endl;
return 0;
}

This study source was downloaded by 100000882496880 from CourseHero.com on 03-28-2024 06:15:02 GMT -05:00

https://www.coursehero.com/file/65385652/CS204-Spring2019-midterm2-Solutionspdf/
NAME:

2) (14 points) Suppose the class Diagne has been properly implemented in file diagne.h.
You use Diagne class in the following program.

#include <iostream>
#include "diagne.h"
using namespace std;

void camasirMakinesi (Diagne & dr, Diagne * dp)


{
return;
}

Diagne denbirhaltolmaz (Diagne go)


{
return go;
}

int main ()
{
Diagne * dptr = new Diagne(5);
Diagne d1(7);
Diagne d2(8);
camasirMakinesi (d1, dptr);
d2 = denbirhaltolmaz (d1);

return 0;
}

Mark the appropriate entries in the table below with "YES" to reflect the facts about
"construction" and "destruction" of the objects mentioned at the beginning of each row. For
"Construction" mark only one cell. Similarly, for "Destruction" mark only one cell. Each
correct mark is 1 point; each wrong mark is -1 point. No mark is no points for that item. No
negative grade from this question.

Construction Destruction
Created Created Destructed
Not Not
using using copy using
created destructed
constructor constructor Destructor

dr of function camasirMakinesi YES YES

dp of function camasirMakinesi YES YES

go of function denbirhaltolmaz YES YES


value returned from function
denbirhaltolmaz YES YES
dptr of main (not the assignment
to it)
YES YES
dynamically created object in
main YES YES

d2 of main YES YES

This study source was downloaded by 100000882496880 from CourseHero.com on 03-28-2024 06:15:02 GMT -05:00

https://www.coursehero.com/file/65385652/CS204-Spring2019-midterm2-Solutionspdf/
NAME:

3)
a) (4 points) Write -19 in binary and in 2's complement format using 8 bits.

11101101

b) (2 points) What is the smallest number that you can represent in 1's complement format
and using 5 bits? Give this value in decimal, not in binary.

-(24-1) = -15

c) (5 points) Suppose you use a computer system in which signed numbers are represented in
4-bits and using 2's complement format. What would be the value in the box? Give your
answer in binary and show your work.

1100 - 0010 = 1010

𝒙 − 𝟎𝟎𝟏𝟎 = 𝟏𝟎𝟏𝟎 ⟹ 𝒙 = 𝟏𝟎𝟏𝟎 + 𝟎𝟎𝟏𝟎 = 𝟏𝟏𝟎𝟎

1010
+ 0010
------------
1100

d) (6 points) Consider program piece and answer the following questions.


unsigned char x = 255;
char y = -1;

What is the value of x+y in binary? Did you encounter carry overflow while calculating
x+y ? Show your work in detail and explain your reasoning.

x and y in binary are the same and equal to 11111111.


So 𝒙 + 𝒚 = 𝟏𝟏𝟏𝟏𝟏𝟏𝟏𝟏 + 𝟏𝟏𝟏𝟏𝟏𝟏𝟏𝟏 = 𝟏𝟏𝟏𝟏𝟏𝟏𝟏𝟎

11111111
+ 11111111
Since there is carry out of
-----------------
the most significant bit,
111111110
there is carry overflow.

This study source was downloaded by 100000882496880 from CourseHero.com on 03-28-2024 06:15:02 GMT -05:00

https://www.coursehero.com/file/65385652/CS204-Spring2019-midterm2-Solutionspdf/
NAME:

4)
a) (6 points) Suppose you can/cannot do the followings in your program:
 In class Piglet implementation, you can access the private data members of class
Eeyore and also of class Tigger.
 In class Eeyore implementation, you cannot access the private data members of class
Piglet, but you can access the private data members of class Tigger.
 In class Tigger implementation, you cannot access the private data members of class
Piglet and of class Eeyore.
What are the friend relationships among these three classes? Give your answer by filling the
appropriate cells in the following table with true or false. Each correct cell is 1 point; each wrong
cell is -1 point; empty cell is 0 point (minimum grade for this question is 0).

class Piglet is class Eeyore is class Tigger is


defined as friend defined as friend defined as friend
In the definition of class Piglet, false false
In the definition of class Eeyore, true false
In the definition of class Tigger, true true

b) (2 points) Consider the following instantiations of templated class Brand. How many different
version(s) of class Brand is/are compiled in the object file?
Brand<int> michaelKors(22);
Brand<string> gucci("dollar");
Brand<int> louisVuitton(19);

2 versions are compiled (one for int and the other is for string)
c) (8 points) The following function takes an integer vector as parameter and copies its content
into a newly generated dynamic integer array. The dynamic array is returned as the function's
return value and its size is returned as a reference parameter. Rewrite the templated version of this
function so that it works with any vector/array element type.
int * convertVectorToDynamic (const vector<int> & v, int & size)
{
int i; In part (c) of this question, you may prefer not to attempt to solve
size = v.size(); it by signing the “not attempted” box below and secure 2 points.
int * darr = new int[size]; If you sign the “not attempted” box below, you accept that you
for (i=0; i<size; i++) 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
darr[i]=v[i]; as solution.
return darr; Not attempted
}

template <class Type>


Type * convertVectorToDynamic (const vector<Type> & v, int & size)
{
int i;
size = v.size();
Type * darr = new Type[size];
for (i=0; i<size; i++)
darr[i]=v[i];
return darr;
}

This study source was downloaded by 100000882496880 from CourseHero.com on 03-28-2024 06:15:02 GMT -05:00

https://www.coursehero.com/file/65385652/CS204-Spring2019-midterm2-Solutionspdf/
NAME:

5) In this question you will write the implementation of = operator and * operator of a
Matrix class that stores real numbers. This class, called Matrix, has the following private
data members:
private:
double ** myMatrix;
int numRows, numCols;

Here, myMatrix is dynamic matrix of real numbers with numRows rows and numCols
columns. The constructor of the Matrix class creates such a dynamic matrix if the number
of rows and columns are given as positive values; otherwise, an empty matrix is created
(where myMatrix is set to NULL). You can assume existence of such a constructor; you will
not implement it. There are also some other member functions to fill the content of the matrix
(not to be implemented in this question). You can assume the existence of properly
implemented deep copy constructor and destructor for the Matrix class in this question. You
do not need to implement those functions.

a) (9 points) Write the implementation of * operator for Matrix class as a member


function. This operator will take a double, say coefficient, as the right hand side
parameter. The * operator will return a Matrix object, of which the elements are equal to
the elements of the left-hand side matrix (i.e. the matrix on which this operator function will
be called) multiplied by coefficient.

In part (a) 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

Matrix Matrix::operator * (double coefficient)


{
Matrix result(*this); //Matrix result = *this
for (int i = 0; i < numRows; i++) //this->numRows OR result.numRows
{
for (int j = 0; j < numCols; j++) //this->numCols OR result.numCols
{
result.myMatrix[i][j] = result.myMatrix[i][j] * coefficient;
// result.myMatrix[i][j] = myMatrix[i][j] * coefficient; OR
// result.myMatrix[i][j] = this->myMatrix[i][j] * coefficient;
}
}
return result;
}

This study source was downloaded by 100000882496880 from CourseHero.com on 03-28-2024 06:15:02 GMT -05:00

https://www.coursehero.com/file/65385652/CS204-Spring2019-midterm2-Solutionspdf/
NAME:

5) Continued
b) (9 points) Write the implementation of deep assignment operator, operator = for
Matrix class as a member function. This operator must allow cascaded assignments, and
your implementation must provide protection against self-assignment and empty matrix case.
These are actually the classical approaches in all assignment operator implementations that
we have seen in class. Thus, there is no particular trick in this question.

In part (b) 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

const Matrix & Matrix::operator = (const Matrix & rhs)


{
if (this != &rhs)
{
for (int i = 0; i < numRows; i++)
delete [] myMatrix[i];
delete [] myMatrix;
numRows = rhs.numRows;
numCols = rhs.numCols;
if (rhs.myMatrix == NULL) //OR if (numRows==0 && numCols==0)
myMatrix = NULL;
else
{
myMatrix = new double*[numRows];
for (int i = 0; i < numRows; i++)
{
myMatrix[i] = new double[numCols];
for (int j = 0; j < numCols; j++)
myMatrix[i][j] = rhs.myMatrix[i][j];
}
}
}
return *this;
}

This study source was downloaded by 100000882496880 from CourseHero.com on 03-28-2024 06:15:02 GMT -05:00

https://www.coursehero.com/file/65385652/CS204-Spring2019-midterm2-Solutionspdf/
NAME:

6) (15 points) The function below is a partial solution for the following problem:
Write a function that takes two integer LinkedList parameters, say list1
and list2. This function returns true if list1 and list2 have the same
amount of elements and the corresponding elements in both of the lists are all
the same. Otherwise, the function returns false. If both lists are empty, the
function returns true, but if only one of them is empty, it returns false.
Implementation must utilize LinkedListIterator. The member functions of the
LinkedList class will not be used.
However, the function is incomplete. Complete this function by filling in the boxes with
appropriate expressions and function calls.
You are not allowed to delete or update anything in the code. Moreover, you cannot add
anything other than the code that you are going to write in the boxes.
For your convenience the member functions of the LinkedListIterator class are given as
follows: void Init() void Next() bool HasMore() int Current()

bool isSame (const LinkedList & list1, const LinkedList & list2)
{
LinkedListIterator iter1 (list1);
LinkedListIterator iter2 (list2);

iter1. Init() ;

iter2. Init() ; trues can be omitted

while ( iter1.HasMore()==true && iter2.HasMore()==true )


{

if ( iter1.Current() != iter2.Current() )
{
return false;
}
iter1. Next() ;

iter2. Next() ;
}

if ( iter1.HasMore()==false && iter2.HasMore()==false )

return true;
else
return false;
}

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

This study source was downloaded by 100000882496880 from CourseHero.com on 03-28-2024 06:15:02 GMT -05:00

https://www.coursehero.com/file/65385652/CS204-Spring2019-midterm2-Solutionspdf/
NAME:

7) (12 points) The following main function gives the output shown below.
int main()
{ 5 9 in Coder
int a=5, b=9;
Coder CS204(a,b);
5 9 in Debugger
Debugger HW6(a,b); 5 9 in main
CS204.Print();
HW6.Print(); 6 10 in Coder
cout << a << " " << b << " in main" << endl << endl;
5 10 in Debugger
CS204.Update(); 5 10 in main
CS204.Print();
HW6.Print();
cout << a << " " << b << " in main" << endl << endl; 6 11 in Coder
HW6.Update(); 6 11 in Debugger
CS204.Print(); 6 11 in main
HW6.Print();
cout << a << " " << b << " in main" << endl << endl;
return 0;
}

Class declarations, member function and constructor implementations of the Coder and Debugger
classes are partially given below. Complete the class declarations and constructor implementations by
filing in the boxes below such that the main function gives the corresponding output.
You are not allowed to delete or update anything in the code. Moreover, you cannot add anything other
than the code that you are going to write in the boxes.
class Coder const int & x is OK
{
public:
Coder ( int x, int & y ):

myC(x), myS(y) {}
void Print () {
cout << myC << " " << myS << " in Coder" << endl;
}
void Update () {
myC++;
myS++;
}
private:

int myC;
int & myS;

};

class Debugger
{
public:
Debugger ( int & x, int & y ):

myC(x), myS(y) {}
void Print () {
cout << myC << " " << myS << " in Debugger" << endl;
}
void Update () {
myC++;
myS++;
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
private: sign the “not attempted” box below, you accept that you did not
answer this question and you will receive 3 points. In this case,
int & myC; your answer will not be graded even if you write something as
int & myS; solution.
Not attempted

};source was downloaded by 100000882496880 from CourseHero.com on 03-28-2024 06:15:02 GMT -05:00
This study

https://www.coursehero.com/file/65385652/CS204-Spring2019-midterm2-Solutionspdf/
Powered by TCPDF (www.tcpdf.org)

You might also like