CS200 Final Exam Fall 2023 SOLUTION
CS200 Final Exam Fall 2023 SOLUTION
Maximum Marks: 50
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;
Page 1 of 13
2. What is the output of the following program? [5]
int main()
{
int c[] = { 1, 2, 3 };
int s = 3;
cout << f(c, s, 1);
return 0;
}
1 4 9 16 25 16 9 16 25 3
Page 2 of 13
3. What is the output of the following program? [10]
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];
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];
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
private:
int rows;
int cols;
T ***m;
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
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]
string str;
while(getline(file, str))
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]
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){
Page 12 of 13
}
while(q != 0);
return head;
Page 13 of 13