0% found this document useful (0 votes)
36 views13 pages

CS200 Final Exam Fall 2023 SOLUTION

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)
36 views13 pages

CS200 Final Exam Fall 2023 SOLUTION

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/ 13

Time: 2 hrs.

Maximum Marks: 50

Roll No. Name:

CS200: Introduction to Programming:


Final Exam (Fall 2023)

Give your answers in the space provided below each question. Where required, write
C++ code in proper syntax. Don’t forget to write your name and roll number above.
1. What is the output of the following program? [3]

int main(){
const int SIZE = 5;
double *a = new double[SIZE] {1.5, 2.5, 3.5, 4.5, 5.5 };
double*p = a;

for(int i = 0; i < SIZE;i++ ){


cout << *(p+i) << " ";
}
cout << endl;
p++;
for(int i = 1; i < SIZE;i++ ){
cout << *p++ << " ";
}
cout << endl;

for(int k = SIZE-1; k > -1; k--){


cout << (*--p) + k << " ";
}
cout << endl;
delete []a;
return 0;
}

// write the program output below.

1.5 2.5 3.5 4.5 5.5


2.5 3.5 4.5 5.5
9.5 7.5 5.5 3.5 1.5

Page 1 of 13
2. What is the output of the following program? [5]

int f(int c[], int s, int t)


{
cout << t*t << " ";
if (s == 0){ return 1;}
if (s < 0){ return 0;}
return f(c, s-c[0], ++t)
+
f(c, s-c[1], ++t);

int main()
{
int c[] = { 1, 2, 3 };
int s = 3;
cout << f(c, s, 1);
return 0;
}

// write the program output below.

1 4 9 16 25 16 9 16 25 3

Page 2 of 13
3. What is the output of the following program? [10]

class MyException void function1(Test d, int i)


{ {
public: //cout << "Start: Function1" << endl;
MyException(string str) { msg = str; } d.setDescription("11");
void print() { cout << msg << endl; } try{
private: function2(d, i + 1);
string msg; } catch(MyException e){
}; e.print();
throw 101;
class Test }
{
public: cout << "End Function1" << endl;
Test(string s) { desc = s; } }
Test(const Test &other) int main()
{ desc = other.desc;} {
Distance d5(128);
void setDescription(string d) try{
{ desc = d;} Test t("M");
Distance d1(55);
private: function3(t, 2);
string desc; Distance d2(77);
}; }
catch (MyException &e)
class Distance { { e.print();}
public: catch (...)
Distance(int d) { dist = d;} { cout << "Unknown exception " << endl;}
~Distance(){ cout << dist << endl;}
private: try{
int dist; Test d("N");
Distance d2(77);
}; function1(d, 1);
Distance d3(77);
void function3(Test d, int i)
{ }
cout << "Start: Function3" << endl; catch (MyException &e)
d.setDescription("33"); {e.print();}
if (i % 3 != 0) catch(int e){
throw 111; cout << "Exception: " << e << endl;
cout << "End: Function3" << endl;
} }
catch (...)
void function2(Test d, int i) {cout << "Unknown exception" << endl;}
{ return 0;
cout << "Start: Function2" << endl; }
d.setDescription("2");
function3(d, i + 1);
throw MyException("Execption 2");
cout << "End: Function2" << endl;
}

Page 3 of 13
//Write the program output below.

Start: Function3
55
Unknown exception
Start: Function2
Start: Function3
End: Function3
Execption 2
77
Exception: 101
128

Page 4 of 13
4. The constructor of the class Matrix below dynamically allocates some memory. Read this
code carefully and insert code in the destructor that deallocates all the memory allocated in
the constructor in proper order. [6]

class Matrix
{
private:
int rows;
int cols;
double ***m;
public:
Matrix(int r, int c, double init)
{
rows = r;
cols = c;
m = new double **[rows];
for (int i = 0; i < rows; i++)
m[i] = new double *[c];

for (int i = 0; i < rows; i++)


{
for (int j = 0; j < cols; j++)
{
m[i][j] = new double(init);
}
}
}
~Matrix() {

cout << "~Matrix" << endl;


for(int i=0;i<rows;i++) {
for(int j = 0; j < cols; j++){
if(m[i][j] != nullptr)
delete m[i][j];
}
}

for (int i = 0; i < rows; i++)


delete [] m[i];

delete [] m;

Page 5 of 13
}

};

5. Rewrite the code of the Matrix class below so that the main() function works properly. [6]

Page 6 of 13
class Matrix
{
private:
int rows;
int cols;
double ***m;
public:
Matrix(int r, int c, double init)
{
rows = r;
cols = c;
m = new double **[rows];
for (int i = 0; i < rows; i++)
m[i] = new double *[c];

for (int i = 0; i < rows; i++)


{
for (int j = 0; j < cols; j++)
{
m[i][j] = new double(init);
}
}
}
};

int main(){
Matrix<double> m1(5,6, 3.56);
Matrix<int> m2(5,6, 3);
Matrix<char> m3(5,6, 'A');
Matrix<string> m4(5,6, "ABC");
return 0;
}

Page 7 of 13
//Rewrite the Matrix class below

template <class T>


class Matrix
{
public:

private:
int rows;
int cols;
T ***m;

Matrix(int r, int c, T init)


{
rows = r;
cols = c;
m = new T **[rows];
for (int i = 0; i < rows; i++)
m[i] = new T *[c];

for (int i = 0; i < rows; i++)


{
for (int j = 0; j < cols; j++)
{
m[i][j] = new T(init);
}
}
}
};

Page 8 of 13
6. Modify the classes below so that all the dynamically allocated memory is freed/deallocated
when the “delete aPtr” statement gets executed. You CANNOT modify the code in the
main() function. Insert code in the designated empty blocks only. [6]

class A {
private:
int *a;
string *s;
public:
A() {
a = new int[10];
s = new string[10];
}

virtual ~A() {
delete []a;
delete []s;
}

};

class B : public A {
private:
float *f;
public:
B() { f = new float[10];}

~B() {
delete []f;
}

};

int main() {
A* aPtr = new B;
delete aPtr;
Page 9 of 13
return 0;
}

7. The following output statement results in execution of multiple calls to the same function.
List down the function calls in proper sequence along with parameters, i.e., mention the
function name and parameters in each call. [4]

cout << "Hello World!" << 15 << "Dec" << 2023 << endl;
Page 10 of 13
# Function name and parameters

operator<<(cout, “Hello World”);


operator<<(cout, 15);
operator<<(cout, “Dec”);
operator<<(cout, 2023);
operator<<(cout, ‘\n’);

8. Give a brief description of each line of code below. Your description should clearly specify
what is done by each line of code. [4]

Line of code Description


ifstream file("D:/data.txt", ios::in);

string str;

while(getline(file, str))

cout << file.tellg() << endl;

file.close();

Page 11 of 13
9. Create a function intToList which is passed a positive integer, and it returns a linked list
representing that integer. See below for examples of inputs and outputs. Further below,
you will find the Node structure for the linked list. [6]

Input (Parameter) Output (returned by the function)

489

4 8 9 X

0 X

class Node {
public:
int data;
Node *next;
Node(int x) { data = x; next = NULL;}
};
Node * intToList(int n){

Node* head = nullptr;


if(n < 10){
head = new Node(n);
return head;
}
int q = n, r;
Node * temp = nullptr;
do {
r = q % 10;
temp = new Node(r);
temp->next = head;
head = temp;
q = q / 10;

Page 12 of 13
}
while(q != 0);
return head;

Page 13 of 13

You might also like