Final-Term Exam: University of Management and Technology Object Oriented Programming

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

UMT ID # S2019207003

University of Management and Technology


Object Oriented Programming
Final-Term Exam
Name: Muhammad Imran Ahmed Section: V2 ( SPA – Section-A)
Course Code: CS150 Date: 2nd Oct, 2020
Instructions:
1. 4 hours given to you for solving this paper.
2. Plagiarism will lead you to get F grade.
3. Submission through email will be acceptable.
4. Solve coding related questions in hard form with your hands.
5. Paste hand written code in this file at appropriate place.
6. Rename exam document in this format “F2018266015-OOP-EXAM-R2020”.
7. In case of any sort of cheating or attempt to cheat, you will be rewarded
with F grade.
8. You have to write your UMT ID on each page otherwise your 1 marks will
be deducted for each missing UMT ID.

1|Page
UMT ID # S2019207003

Q1:
a. What are the 2 major situations where member initializer list is required? What is the the
difference between member and base initialization lists and when they are used?

b. Differentiate between shallow and deep copy. Provide a coding example also draw
diagram to show these concepts.

c. Compare overloading with overriding. Provide coding example.

d. Discuss the magic of “virtual” in OOP. Why we need to make destructor virtual in
polymorphism.

2|Page
UMT ID # S2019207003

e. Write a piece of code which can show the following.


 Parent’s class constructor always executes before child.
 Composed’s class constructor always executes before container class.

3|Page
UMT ID # S2019207003

Q2. Write code of the following.


1. Ship class
Design a Ship class that has the following members:

a. A member variable for the name of the ship (a string)


b. A member variable for the year that the ship was built (a string)
c. A constructor(s) and appropriate property methods.
d. A virtual print function that displays the ship’s name and the year it was built.

2. CruiseShip class
Design a CruiseShip class that is derived from the Ship class. The CruiseShip class should have
the following members:

a. A member variable for the maximum number of passengers (an int)


b. A constructor(s) and appropriate property methods.
c. A print function that overrides the print function in the base class. The CruiseShip class’s
print function should display the ship’s name, year of built and the maximum number of
passengers.

3. CargoShip class
Design a CargoShip class that is derived from the Ship class. The CargoShip class should have
the following members:

a. A member variable for the cargo capacity in tonnage (an int).


b. A constructor(s) and appropriate property methods.
c. A print function that overrides the print function in the base class. The CargoShip class’s
print function should display only the ship’s name and the ship’s cargo capacity.

4. BattleShip class
Design a BattleShip class that is derived from the Ship class. The BattleShip class should have
the following members:

a. A member variable for the total number of missiles (an int).


b. A constructor(s) and appropriate property methods.
c. A print function that overrides the print function in the base class. The BattleShip class’s
print function should display only the ship’s name and the missiles’ capacity.
5. A Driver Program
Demonstrate the classes in a program that has an array of Ship pointers. The array elements should
be initialized with the addresses of dynamically allocated Ship, CruiseShip, CargoShip and BattleShip
objects. The program should then step through the array, calling each object’s print function.

4|Page
UMT ID # S2019207003

Q3. Encircle the best possible option.


1: What is it called if an object has its own lifecycle and there is no owner?
a) Composition b) Aggregation c) Simple Association d) Inheritance
2: The copy constructor created and triggered by the compiler does
a) Deep Copy b) Shallow Copy c) Memory Copy d) Simple Copy
3: The process in which two objects interact with each other is known as
a) Composition b) Aggregation c) Simple Association d) Inheritance
4: Which of these will always used to fully abstract a class from its
implementation?
a) Abstract b) Interfaces c) Both a) and b) d) None of these
5: The process in which a class and a reference/pointer interacts is known as
a) Composition b) Aggregation c) Simple Association d) Inheritance

5|Page
UMT ID # S2019207003

Q4:
How many times copy constructor called, in the given code when main function
will be executed? Explain the reason of each call of Copy constructor to get full
credit.
class Widget{
public:
Widget() {}
};
Widget f (Widget u){
Widget v(u);
Widget w=v;
return w;
}
int main() {
// your code goes here
Widget x;
Widget y = f(f(x));
return 0;
}

6|Page
UMT ID # S2019207003

Q5.
Consider the following real world electrical appliances and thinking in terms of
OOP (e.g. What are the relationships between them? Can you exploit the
inheritance between any of them? What are the polymorphic methods? Are there
any abstract classes? If so what are the pure virtual methods? etc.).
 Electrical Appliances
oAudio
 Hi-fi
 Radio
 Walkman
oRefrigeration
 Fridge
 Freezer
oTelephone
 Fixed
 Cordless
 Cellular
 All electrical appliances have a turnOn and turnOff method.
 All audio appliances have an adjustVolume method.
 All refrigeration appliances have an adjustTemperature method.
 All telephones that have a dial and hangup method.

Draw the class diagram using UML no code is required.

7|Page

You might also like