0% found this document useful (0 votes)
4 views12 pages

2019 SP Final

The document is an exam paper for the Object Oriented Programming course at the National University of Computer and Emerging Sciences, Lahore Campus, for the Spring 2019 semester. It includes four questions that cover topics such as designing a game with classes, creating a music player application using inheritance and polymorphism, implementing the Sieve of Eratosthenes algorithm, and analyzing code segments for output. The exam consists of a total of 75 marks and is designed to assess students' understanding of object-oriented programming concepts.

Uploaded by

Aiyshah Meerab
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)
4 views12 pages

2019 SP Final

The document is an exam paper for the Object Oriented Programming course at the National University of Computer and Emerging Sciences, Lahore Campus, for the Spring 2019 semester. It includes four questions that cover topics such as designing a game with classes, creating a music player application using inheritance and polymorphism, implementing the Sieve of Eratosthenes algorithm, and analyzing code segments for output. The exam consists of a total of 75 marks and is designed to assess students' understanding of object-oriented programming concepts.

Uploaded by

Aiyshah Meerab
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/ 12

National University of Computer and Emerging Sciences, Lahore Campus

Course: Object Oriented Programming Course Code: CS 217


Program: BS(Computer Science) Semester: Spring 2019
Duration: 180 Minutes Total Marks: 75
Paper Date: 14-May-2019 Page(s): 12
Section: ALL Section:
Exam: Final Roll No:
Instruction/Notes:1. Use the provided space to solve the questions.
2. Extra sheets may be used, but may not be attached.
3. In case of any ambiguity make a reasonable assumption.
Question # 1 (20 points)
We have designed a game called treasure hunt. In this game a 2D class Player
board contains different types of treasures. The treasures are {
dynamically allocated objects of type Treasure (see code on the private:
right). So each board square contains a pointer to an allocated Treasure ** bag;
Treasure object. int bagsize;
A dice is rolled for each player at the same time and the players int totalpoints;
move on the board according to the number that turns up on their string name;
dice. A player collects the treasure on the square on which he stops, public:
by adding the pointer of that treasure into his bag (see code on the //Player methods
right). The bags are initially empty. Since more than one player can };
land on the same square, various players may have pointers to the class TreaureHunt
same objects in their bags. Also, pointers to these objects remain in {
the board. The board and the bags share pointers to the same private:
Treasure *** board;
Treasure objects, without creating new copies of these objects.
int rows;
Our code contains a class called TreasureHunt (see code on the int cols;
right) which contains the board (containing pointers to different Player ** players;
Treasure objects), its dimensions, and a list of players. int playerCount;
Each player contains a bag, which is an array of Treasure pointers, public:
which is updated with each move of the player on the board. //treasureHunt methods
};
For example, assume that the game has three players.
Assume we have the following 4×3 board at the start of game (each struct Treasure{
square contains a pointer to a int value;
Diamond Bronze Diamond treasure object whose string description;
Gemstone Sword Ring
Silver Gold Sapphire description is shown.) }
Coin Neckless Gemstone
Gold Ruby Silver
Brik Gemstone Pendent
Bag of Gold Silver
gemstones Coin Brick

At the end of the game, the players’ bags may contain the pointers as follows (the actual situation will depend
on the numbers that turn up on each dice):

School of Computer Science Page 1 of 12


players[0].bag

Diamond Gold
Gemstone Neckless

Silver
Coin
Diamond
players[1].bag
Ruby Gold Silver Ring
Gemstone Brik Pendent

players[2].bag

You job is only to write a destructor for the class TreasureHunt to deallocate all the allocated memory
of the game. Make sure that no object is deleted more than once and there are no memory leaks. You can
add more code utility methods to the class TreasureHunt to divide the work among functions.

School of Computer Science Page 2 of 12


School of Computer Science Page 3 of 12
School of Computer Science Page 4 of 12
Question # 2 (20 points)

In this question you will need to use inheritance, polymorphism, friend class and down casting.

You have designed a new music player application. Let’s call it OOPMP, short for OOP Music Player.
OOPMP will have a playlist in which songs can be added or removed.
Each song will have lyrics and name. As we cannot keep audio for lyrics, let’s assume that the lyrics are in
the form of a char*.
Songs will have further types as follow:
1. MP3: this will be an audio song, only containing lyrics, each MP3 song will also have a type (for
example pop, rock, classical etc.)
2. MP4: this will be a video song, they will have lyrics as well as video (again, as we cannot keep video
we will assume it to be a char* containing a description of the scenes.)
3. sMP4: short for secret MP4, this type of song will also have lyrics and video. It is called secret MP4
because only OOP music players can play their real lyrics and video. If anyone else tries to play it,
they will only hear bleep as lyrics and see glitches as videos.
Design and code your classes keeping in mind the above mentioned instructions and the following main
function and its desired output (on next page).

int main() TYPE:


{ pop
/*Creating MP3 song, arguments are LYRICS:
name of song, lyrics and type*/ Because I'm happy...
MP3 *s1= new MP3("Happy",
LYRICS:
"Because I'm happy...", Because I'm happy...
"pop") ; VIDEO:
//play s1, it will just print the lyrics A man walking..
s1->play();
LYRICS:
/*Creating MP4 song, bleeeeeeep
arguments are name of song, VIDEO:
lyrics and description of video*/ glitchhhh
MP4 *s2= new MP4("Happy",
"Because I'm happy...", Currently Playing: Happy
TYPE:
"A man walking..");
pop
s2->play(); LYRICS:
Because I'm happy...
/*Creating sMP4 song, arguments are
name of song, lyrics and type*/ Currently Playing: Happy
sMP4 *s3= new sMP4("Born Free", LYRICS:
"Born free...", Because I'm happy...
"Different pictures\nappearing on screen..."); VIDEO:
s3->play(); /*note that playing song of sMP4 A man walking..
does not show real lyrics and video*/
Currently Playing: Born
Free
//Creating a music player of type OOPMP
LYRICS:
OOPMP myPlayer(3); Born free...
VIDEO:
//Adding songs to OOPMP Different pictures
myPlayer.addToPlayList(s1); appearing on screen...
myPlayer.addToPlayList(s2);
myPlayer.addToPlayList(s3);

/*invoking memeber function to play all songs in playlist


note that now sMP4 song shows the real lyrics and video*/
myPlayer.playAll();
}

School of Computer Science Page 5 of 12


School of Computer Science Page 6 of 12
School of Computer Science Page 7 of 12
Question # 3 (20 points)
The Sieve of Eratosthenes is an algorithm to find all the prime numbers between 2 and an input number n. For
example, if n=23, the algorithm finds 2, 3, 5, 7, 11, 13, 17, 19 and 23. The algorithm works as follows:
(1) Create an array, A, containing all numbers between 2 and n.
(2) Start with p=2
(3) Remove all multiples of p from A (excluding p itself), since these multiples cannot be prime.
(4) Set p = the next number in A after all the removals in step(3)
(5) If p<n/2, repeat from (3).
Write a function called Eratosthenes which returns a dynamic, int*, array, containing all the prime numbers
between two given numbers m and n. The size of the array should be exactly one more than the total number
of prime numbers. The function should place a -1 at the end of the array to mark the end of the output.

School of Computer Science Page 8 of 12


School of Computer Science Page 9 of 12
Question # 4 (3*5 = 15 points)
(a) Write the output of code segment given below for following inputs.
void main()
{
double ** arr;
int size;
try
{
cout << "Enter Size of array: ";
cin >> size;
if (size <= 0)
throw exception("Array size must be greater than zero \n");
else if (size == 0)
throw size;
arr = new double*[size];
for (int i = 0; i < size;i++){
arr[i] = new double[size];
}
}
catch (bad_alloc ba){
cout << ba.what()<< endl;
}
catch (int s){
cout << "Wrong size: " << s << endl;
}
catch (out_of_range o){
cout << o.what() << endl;
}
catch (exception e){
cout << e.what() << endl;
}
}
Size = -1

Size = 0

Size = 50000

School of Computer Science Page 10 of 12


(b) Write the output of code segment below.
class strwrap{ Output
string s;
public:
strwrap(const char * str){
s=str;
}
void whatprint1(){
(this+2)->whatprint2();
}
void whatprint2(){
(this-1)->print();
}
void print(){
cout<<s<<endl;
}

};

int main() {
strwrap arr[3]={"What will", "be printed", "on the screen?"};
arr[0].whatprint1();
}

(c) Consider the following function template sort that takes an array of elements as input, sorts and prints it.
main1 function gives an example of how it is used for integer array and its output. Identify if this function
will work for an array of char* type, if not write a code such that the main2 function works and produces
desired output as shown below, i.e. sorts the strings in given array in ascending order of their length and prints
them.
void sort(T* arr, int n) //main1 output
{ void main () 89
int i, j; { 435
for (i = 0; i < n; ++i) int arr[5]={89,10154,687,435,8054}; 687
{ sort(arr, 5); 8054
for (j = 0; j < n-i-1; ++j) 10154
{ }
/*Comparing consecutive data
switching values if value
//main2 Desired
at j > j+1*/
void main2() output
if (arr[j] > arr[j+1]) {
{ xa
T temp= arr[j]; abc
char **cArray= new char*[3]; mnop
arr[j]= arr[j+1]; cArray[0]= new char[3];
arr[j+1]= temp; strcpy(cArray[0],"xa");
}
cArray[1]= new char[4];
}
strcpy(cArray[1],"abc");
}
cArray[2]= new char[5];
for (int i=0; i<n; i++) strcpy(cArray[2],"mnop");
cout<<arr[i]<<endl;
sort(cArray, 3);
}
}

Space for part (c)

School of Computer Science Page 11 of 12


School of Computer Science Page 12 of 12

You might also like