CS115 FinalPrep
CS115 FinalPrep
Last name:
First name:
Student ID:
University of Regina
Department of Computer Science
CS115 - 991/002
Fall 2023
Final Examination
December 15, 2023
9:00 - 12:00
Location: CK GYM 2
80 marks
Instructors:
(991): Vatika Tayal
(002): Sultan Ahmed
Page 1 of 20
CS 115 - 991/002 - Fall 2023 - Final Exam
Question 1:
1. (4 marks) Look at the following structure declaration:
struct Point
{
int x;
int y;
};
Write program statements that:
(a): declare an instance of structure Point (named center).
Point center;
center.x = 12;
center.y = 15;
Page 2 of 20
CS 115 - 991/002 - Fall 2023 - Final Exam
2. (6 marks) For the following program code, give the content that will be printed. You
need to show the steps of your work indicating how the functions are called and returned
from when the value of the variable is changed.
int count=3;
class Exam
{
public:
Exam()
{
count++;
cout<<"Constructor is called."<<endl;
}
~Exam()
{
cout<<"Destructor is called."<<endl;
count--;
count--;
}
};
main()
{
Exam e1, e2, e3;
{
Exam e4;
}
cout<<count;
}
e1 makes 4
e2 makes 5
e3 makes 6
e4 makes 7
destructor for e4 makes 5
Page 3 of 20
CS 115 - 991/002 - Fall 2023 - Final Exam
Question 2:
1. (4 marks) Given the following pairs of prototypes, and assuming that only safe
coercions are performed, identify which function the compiler would match to each
the function call. Your answer should be A, B, C (neither is a safe match), or D (am-
biguous: both are safe matches and neither is preferred).
(a): void myMax(float f1, float f2); // A
void myMax(int i1, int i2); // B
myMax(7, 9);
Page 4 of 20
CS 115 - 991/002 - Fall 2023 - Final Exam
2. (6 marks) Answer the questions (a) to (d) based on the following code:
class Drug
{
private:
char category[10];
char company[20];
char date_of_manufacture[10];
public:
Drug();
void enterdrugdetails();
void showdrugdetails();
};
(a): How many bytes will be required by an object of class Drug and an object of class
PainReliever, respectively?
drug will require 40 bytes and PainReliever will require Drug’s 40, Tablet’s
50 and its own 4 + 20 + 4 bytes which will sum upto 118 bytes
Page 5 of 20
CS 115 - 991/002 - Fall 2023 - Final Exam
(b): Write the names of all the data fields that are accessible from an object of class PainRe-
liever.
char tablet_name[30];
char volume_label[20];
int dosage_units;
char side_effects[20];
int use_within_days;
(c): Write the names of all the members that are accessible from member functions of
class Tablet.
char category[10];
char company[20];
char date_of_manufacture[10];
char tablet_name[30];
char volume_label[20];
(d): Write the names of all the member functions that are accessible from objects of class
PainReliever.
void enterdrugdetails();
void showdrugdetails();
void entertabletdetails();
void showtabletdetails ();
PainReliever();
void enterdetails();
void showdetails();
Page 6 of 20
CS 115 - 991/002 - Fall 2023 - Final Exam
Question 3:
1. (5 marks) You are given an array (called A[]) of N integer values. Write a function
that returns true if the array is a palindrome; otherwise, it returns false. [Note: An
array is a palindrome if the reverse of the array is the same as of the original array.]
Page 7 of 20
CS 115 - 991/002 - Fall 2023 - Final Exam
Page 8 of 20
CS 115 - 991/002 - Fall 2023 - Final Exam
Question 4
1. (7 marks) Given the following prototype of the selection sort function, write the
function in C++. Note that A[] is an array of N elements. After the function is ex-
ecuted, the array should be sorted in ascending order.
{
int searchIndex, indexOfMin, tempValue;
Page 9 of 20
CS 115 - 991/002 - Fall 2023 - Final Exam
To search for the item 82 using binary search in the given array:
We start by considering the entire array as the search range: 10, 20, 30, 40,
50, 60, 70, 80, 90.
We then find the mid-point of the search range, which is at index 4 (value:
50).
Since 50 is less than 82, we know that if 82 exists in the array, it must be
located in the right half of the array.
We update the search range to be the right half of the current search range:
60, 70, 80, 90.
We repeat the process: finding the mid-point, comparing it with 82, and
updating the search range, until we either find 82 or the search range
becomes empty.
The elements that are searched during this process are: 50, 70, 80, 90.
The search ends without finding 82, indicating that it does not exist in the
array.
Page 10 of 20
CS 115 - 991/002 - Fall 2023 - Final Exam
Question 5:
1. (3 marks) What is meant by Dynamic Memory Management? Explain why there is
a need for dynamic memory?
2. (7 marks) Consider a two dimensional (2D) matrix of integer values, where the number
of rows, the number of columns, and the elements are provided by user. Write a program
that:
(a): Reads in the number of rows and columns entered by the user.
(b): Declares a dynamically allocated 2D array to store the matrix elements.
(c): Reads in all the elements needed to fill in the matrix.
(d): Calculates the sum of the elements and print the sum.
(e): Deallocates the array.
Page 11 of 20
CS 115 - 991/002 - Fall 2023 - Final Exam
#include <iostream>
int main() {
// (a) Read the number of rows and columns from the user
int rows, cols;
cout << "Enter the number of rows: ";
cin >> rows;
cout << "Enter the number of columns: ";
cin >> cols;
// (d) Calculate the sum of the elements and print the sum
int sum = 0;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
sum += matrix[i][j];
}
}
cout << "Sum of all elements: " << sum << endl;
return 0;
} Page 12 of 20
CS 115 - 991/002 - Fall 2023 - Final Exam
Question 6:
1. (6 marks) You are given a string “A string”.
First, create an array of char data type that stores string. Second, create a pointer
(named pc) that points to the same string i.e. that can access the same string.
Print on one line the array element at index 0, the character that is pointed to by pc,
and the letter t of the string(using the pointer). Then, increase pc by 2. In another
line, print the character that is pointed to pc, and the letters r and g of the string
(using the pointer).
You should demonstrate your work with the help of memory diagram.
Expected output is:
AAt
srg
Page 13 of 20
CS 115 - 991/002 - Fall 2023 - Final Exam
2. (4 marks) Consider the following program, where the arguments in function swap
are passed by reference.
void swap(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
int x = 0;
int y = 1;
swap(x, y);
return 0;
}
Implement the same program (both main and swap functions) so that the arguments
are passed by pointer.
#include <iostream>
int main() {
int x = 0;
int y = 1;
swap(&x, &y);
return 0;
}
Page 14 of 20
CS 115 - 991/002 - Fall 2023 - Final Exam
Question 7:
1. (3 marks) What is the purpose of inheritance in C++? Can we access private mem-
bers of a base class in a derived class? Elaborate.
Page 15 of 20
CS 115 - 991/002 - Fall 2023 - Final Exam
2. (7 marks) Consider a class network as shown in the figure below.[Considering all the
classes are derived in public mode]
The class master derives information from both account and admin classes, which in
turn derive information from the class person. Declare all four classes.
Write a program that instantiates an object (called masterobject) of the class master.
Then, every accessible field of masterobject should be updated to any legal value, and
displayed.
DYI !!
Page 16 of 20
CS 115 - 991/002 - Fall 2023 - Final Exam
Page 17 of 20
CS 115 - 991/002 - Fall 2023 - Final Exam
Question 8:
1. (10 marks) Consider the following interface of a linked list abstract data type.
struct Node
{
int item;
Node* next;
};
class LinkedList
{
Node* ptr;//pointer to the head of linked list
public:
LinkedList();
int GetLength();
bool IsEmpty();
bool IsFull();
Node* FindItem(int itemToFind);
Node* FindPreviousItem(int itemToFind);
void Insert(int newItem);
void Delete(int itemToDelete);
void PrintLinkedList();
void DeleteLinkedList();
Node* DuplicateLinkedList();
};
The Delete function deletes the first occurrence of itemToDelete from the linked list.
If itemToDelete does not exist in the linked list, no deletion occurs. Implement the
Delete function.
Page 18 of 20
CS 115 - 991/002 - Fall 2023 - Final Exam
// If itemToDelete is found
if (current->next != nullptr) {
Node* temp = current->next;
current->next = current->next->next;
delete temp;
}
}
Page 19 of 20
CS 115 - 991/002 - Fall 2023 - Final Exam
Page 20 of 20