0% found this document useful (0 votes)
19 views20 pages

CS115 FinalPrep

The document outlines the final examination details for CS 115 at the University of Regina, scheduled for December 15, 2023. It includes instructions for the exam, a breakdown of questions and marks, and various programming tasks related to C++ concepts such as structures, classes, dynamic memory management, and algorithms. The exam consists of multiple questions requiring code writing, explanations, and problem-solving related to computer science principles.

Uploaded by

dapperonion
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)
19 views20 pages

CS115 FinalPrep

The document outlines the final examination details for CS 115 at the University of Regina, scheduled for December 15, 2023. It includes instructions for the exam, a breakdown of questions and marks, and various programming tasks related to C++ concepts such as structures, classes, dynamic memory management, and algorithms. The exam consists of multiple questions requiring code writing, explanations, and problem-solving related to computer science principles.

Uploaded by

dapperonion
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/ 20

CS 115 - 991/002 - Fall 2023 - Final Exam

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

• Answer the questions directly on the question sheets.


• Check that you have all 20 pages including this cover page.
• Please do not provide any opportunity for others to copy any of your work as that
might be considered academic misconduct on both sides.
• Make sure your final answers are clearly readable.
• This is a closed-book exam. The only objects that should be near you are your writ-
ing tools and your student ID. Possession of any electronic device will be considered
academic misconduct.

Question Number Total questions/marks Marks obtained


1 2 questions(4+6)marks / 10
2 2 questions(4+6)marks / 10
3 2 questions(5+5)marks / 10
4 2 questions(7+3)marks / 10
5 2 questions(3+7)marks / 10
6 2 question(6+4) marks / 10
7 2 question(3+7)marks / 10
8 1 question / 10
Total grade / 80

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;

(b): assign 12 to x of center, and 15 to y of center.

center.x = 12;
center.y = 15;

(c): read the value of y of center.

cin >> center.y;

(d): display the contents of x and y of center

cout << center.x << center.y;

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

constructor statement is printed four times


destructor statement is printed one time.

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);

(b): void zipIt(float f1); // A


void zipIt(string s1); // B
String s = "Trouble";
zipIt(s);

(c): void crunch(string s1, string s2); // A


void crunch(string s1); // B
double e = 2.71828;
crunch(e);

(d): void mixed(int i1, double d1); // A


void mixed(double d1, int i1); // B
int k3 = 3, k4 = 4;
mixed(k3, k4);

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();
};

class Tablet : public Drug


{
public:
char tablet_name[30];
char volume_label[20];
Tablet();
void entertabletdetails();
void showtabletdetails ();
};

class PainReliever : public Tablet


{
private:
int dosage_units;
char side_effects[20];
int use_within_days;
public:
PainReliever();
void enterdetails();
void showdetails();
};

(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.]

bool IsPalindrome(int array[], int num)


{
for (int i = 0; i <= num/2; i++)
{
if(array[i] != array[num - i -1])
return false;
}
return true;
}

Page 7 of 20
CS 115 - 991/002 - Fall 2023 - Final Exam

2. (5 marks) Explain the idea of how a two-dimensional array can be simulated by


an one-dimensional array. Illustrate it with the help of C++ code.

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


{
for (int j = 0; j < cols; j++)
{
array1[i * cols + j] = array2[i][j];
}
}

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.

void selectionSort( int A[], int N );

{
int searchIndex, indexOfMin, tempValue;

for (int currentIndex = 0; currentIndex < size; currentIndex++)


{
indexOfMin = currentIndex;

// Find index of smallest element in unsorted section of elements


for (searchIndex = currentIndex + 1; searchIndex < size; searchIndex++)
if (intArray[indexOfMin] > intArray[searchIndex])
indexOfMin = searchIndex;

// Exchange items at position indexOfMin and currentIndex


if (indexOfMin > currentIndex)
{
tempValue = intArray[indexOfMin];
intArray[indexOfMin] = intArray[currentIndex];
intArray[currentIndex] = tempValue;
}
}
}

Page 9 of 20
CS 115 - 991/002 - Fall 2023 - Final Exam

2. (3 marks) You are given the following array of integers:


10, 20, 30, 40, 50, 60, 70, 80, 90.
You are asked to apply the binary search algorithm as explained in class to search for
an item 82. Identify the elements that are searched for this purpose.

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?

Dynamic Memory Management refers to the allocation and deallocation of


memory during program execution. It's necessary because:

Variable Memory Requirements: Memory needs may not be known at


compile time, such as with user input or dynamic data structures.
Flexibility: Dynamic allocation allows memory to be allocated only when
needed and deallocated when no longer required, leading to efficient
memory usage.
Dynamic Data Structures: Implementing dynamic data structures like linked
lists requires allocating and deallocating memory as elements are added or
removed.
In summary, dynamic memory management is needed to allocate memory
at runtime, adapt to changing memory requirements, and efficiently manage
memory resources in programs.

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>

using namespace std;

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;

// (b) Declare a dynamically allocated 2D array


int** matrix = new int*[rows];
for (int i = 0; i < rows; ++i) {
matrix[i] = new int[cols];
}

// (c) Read in all the elements needed to fill in the matrix


cout << "Enter the elements of the matrix:" << endl;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cout << "Element at position [" << i << "][" << j << "]: ";
cin >> matrix[i][j];
}
}

// (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;

// (e) Deallocate the array


for (int i = 0; i < rows; ++i) {
delete[] matrix[i];
}
delete[] matrix;

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

DYI - it’s easyy


just remember this - “You should demonstrate your work with the help
of memory diagram.”

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>

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;
}

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.

The purpose of inheritance in C++ is primarily to promote code reusability


and to establish an "is-a" relationship between classes. Inheritance allows
derived classes to inherit properties and behaviors from base classes,
reducing code redundancy and promoting a modular design approach.

Regarding accessing private members of a base class in a derived class,


no, we cannot directly access private members of a base class in a derived
class. Private members are only accessible within the class that defines
them. However, we can access them indirectly using member functions or
friend functions. This ensures encapsulation and data integrity.

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

void Delete(int itemToDelete) {


// If the linked list is empty, return
if (head == nullptr) {
return;
}

// If the itemToDelete is at the head of the linked list


if (head->item == itemToDelete) {
Node* temp = head;
head = head->next;
delete temp;
return;
}

// Traverse the linked list to find the node containing itemToDelete


Node* current = head;
while (current->next != nullptr && current->next->item != itemToDelete) {
current = current->next;
}

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

You might also like