OOP Final Spring'22
OOP Final Spring'22
Instructions:
Return the question paper and make sure to keep it inside your answer sheet.
Read questions completely before answering. There are 7 questions, 4 sides on 2 pages.
In case of any ambiguity, you may make assumption. But your assumption should not
contradict any statement in the question paper.
You are not allowed to write anything on the question paper (except your ID and section).
Time: 180 minutes. Max Marks: 53 Marks
Q 1. [20 min, 5 Marks (1 each), CLO #1] Write short answers to the following questions:
a. Consider the following four calls to a global template function:
i) func (2, "hello", 3); ii) func ('x', 5.4, 'y'); iii) func (3.1, "oop", 6.2); iv) func (1, 2, 3);
Write the definition of this template function (only its skeleton) that uses minimum
possible number of generic types such that these calls can be successfully made.
b. Assume there is a class that is inherited from two parent classes (multiple inheritance).
Is it possible for Diamond Problem to occur even if none of parent or child classes contain
any functions at all and only have variables? Justify your answer using an example.
c. What is the difference between virtual function and pure virtual function?
d. Briefly explain with examples, IS-A and HAS-A relationships.
e. Should a default catch block always be the last of the catch blocks if there are more than
one blocks present? Justify your answer.
Q 2. [15 min, 5 Marks (0.5 each), CLO #1] Fill in the blanks of the following questions.
a. If we have an object from ofstream class, then the default mode of opening the file is
_____.
b. _____________ creates different versions of a function at runtime.
c. ___________ returns true if a file open for reading has reached the end.
d. _________ feature can be implemented using encapsulation.
e. The block that is used to check for errors is called as the _____ block
f. Class template can be created using ________ syntax.
g. Member functions having the same name in base and derived class is called as ______.
h. __________keyword is used to handle the exception.
i. The following expression: (3 > 6) && (7 > 4) will return ______.
j. A function that is called automatically each time an object is destroyed is a ________.
1 OF 4
Q 3. [15 min, 5 Marks (1 each), CLO #2] Review following given codes and identify any errors
that might occur. If there are no errors, then write output of the programs.
a) template<class T, class U, class b) class Base{
V=double> public:
class A { virtual void display()=0;
T x; };
U y; class Child: public Base{
V z; public:
static int count; virtual void display(){
}; cout<<"Never Give up!!!"<<endl;
int main() };
{ };
A<int, int> a; int main(){
A<double, double> b; Child obj;
cout << sizeof(a) << endl; cout<<sizeof(obj)<<endl;
cout << sizeof(b) << endl; return 0;
return 0; }
}
c) class A{ d) int main() {
public: int var = 0;
void square (int *x){ try {
*x = (*x)++ * (*x); try {
} throw var;
void square (int *x, int *y){ }
*x = (*x) * --(*y); catch (int ex) {
} ex+=10;
}; cout << "Error handling :: Val :" << var<<" Ex :
int main(){ "<<ex<<endl;
A obj; throw;
int number = 10; }
obj.square(&number, &number); }
cout <<number; catch (...) {
return 0; cout << "All Exception Catch: val :"<< var<<" Ex
} : "<<ex<<endl;
}
return 0;
}
e) class A{
int x;
public: A(int i) { x = i; }
void print() { cout << x; }
};
class B: virtual public A{
public: B():A(10) { }
};
class C: virtual public A{
public: C():A(10) { }
};
class D: public B, public C {
};
int main(){
D d;
d.print();
return 0;
}
2 OF 4
Q 4. [40 min, 12 Marks (3 each), CLO #3] Consider the given classes Sport and Tournament.
class Sport
{ string current_champ;
int start_year;
string headquarter_location;
};
class Tournament
{
Sport * sport;
};
a. Write a default constructor for Tournament class that initializes Sport class reference.
Also, write an overloaded constructor for Tournament class that initializes Sport class
reference and receives the attributes for Sport in parameters.
b. Write a function Begin_Tournament in class Tournament. This function should receive
Sport as argument and print "Tournament begins" if the headquarters of the given sport
is either Karachi or Toronto.
c. Create a new class World Cup and inherit it from Tournament. Override the function
Begin_Tournament in this derived class. The derived class function should print "World
Cup begins" if the starting year of given sport is after 1950 and if the headquarters of the
sport is neither Karachi nor Toronto.
d. Create a global generic function name PrintIt that takes any attribute of Sport as input
and display its value.
Q 5. [30 min, 9 Marks (3 each), CLO #5] Suppose you are running a jumping castle playland and
for safety reasons want to implement a rule that at most 10 people can be on the jumping castle
at any one time. People are allowed to go on for as long as they wish. In order to make certain
there are not too many people on the castle you station an employee at the entrance/exit. This
employee will count people as they enter and count them as they exit. If the jumping castle is
full then a person wishing to enter will need to wait until someone first exits.
a. Write a program that displays the current number of people on the jumping castle, have
an “Enter” function that increments the people count, an “Exit” function that
decrements the people count. The program should repeat indefinitely and every time
asks the employee to either enter or exit people.
b. The people count should not be permitted to go beyond 10 when employee enters 11th
person it should throw an exception that prints a string message “Count Overflow”
c. Also, there shouldn't be a negative number of people on the castle! When employee
decrements at 0th position it should throw an exception that prints a string message
“Count Underflow”).
Q 6. [20 min, 9 Marks (3 each), CLO #3] Declare a class FILE as an abstract class. The class contains
data members like size, location, created date and modified date, and functions like open() and
print(). Open() is a pure virtual function and print() is a virtual function.
a. Derive three classes (PDF file, ASCII File, and PS File) from FILE class, such that there is
slight difference between the implementation of print() function in all derived classes.
b. Create 3 File class pointers in main, and store the reference objects of each subclass in
them and call their respective print() functions polymorphically. Delete all allocated
objects memory before program exits.
3 OF 4
c. Develop a global function that can be used to identify two objects of FILE type and finds
duplicates. The function should be able to access class members but make sure that
function must not be able to change any value. The new function compares size and
location of two files and if size of the objects matches and location is same, the function
returns true otherwise, false. The function signature is given below for your reference.
bool operator * (File file_object_1, File file_object_1).
Q 7. [30 min, 8 Marks (4 each), CLO #4] Let’s assume four classes as given in the above picture.
You are provided a “datafile.bin” file where 100’s of objects of these classes are stored for an
electronic shop. The sequence of the objects in “datafile.bin” is also
described in the above given diagram. Write a program where:
a. Read the “datafile.bin” to extract all objects, and print their
details.
b. Calculate total price of all HP, ASUS, and DELL Laptops,
separately. Create another file “report.txt” where store total
sale amount with respect to each type of the laptop. The
report.txt file should look like as given in the adjacent figure.
BEST OF LUCK!
4 OF 4