OOP - MID I - Spring 2020 - Solution
OOP - MID I - Spring 2020 - Solution
Instructions:
Except your Roll No and Section, DO NOT WRITE anything on this paper.
Return the question paper with your answer sheet.
Read each question completely before answering it. There are 3 questions on 2 pages.
In case of any ambiguity, you may make assumption but your assumption must not contradict any statement in the
question paper.
All the answers must be solved according to the SEQUENCE given in the question paper, otherwise points will be
deducted.
b. What can be done if we don't want any objects to be copied in our program?
Answer: We can add a copy constructor in that class and change its access level to private, hence making
both user-defined and compiler-provided copy constructors unusable.
d. Can a static member function call non-static member functions? If yes, then how?
Answer: A static member function cannot directly call any non-static member functions. However, it can use
an object of the class to call non-static member functions.
e. Can two different unrelated classes in a single program contain functions with the same name and parameter
signature?
Answer: Yes. Since the naming scope of two unrelated classes are different in a program.
1 OF 2
Q No. 2 Consider the following scenario: [2.5 x 4 = 10 Points]
ABC University is a large institution with several campuses. Each campus has a different name, address, and distance to
the city center.
The University consists of a number of faculties, such as the Art Faculty, the Science Faculty, and so on. Each faculty has a
name, dean and building. ABC University offers different programmes and each programme has a code (valid format: P-nnn,
where n can be any digit), title, level and duration (1, 2 or 3). Each programme comprises several courses, different programmes
have different courses. Each course has a code (valid format C-nnn, where n can be any digit) and course title.
Each of the students is enrolled in a single programme of study which involves a fixed set of core courses specific to that
programme as well as a number of electives taken from other programmes. Students work on courses and are awarded a grade
in any course if he/she passes the course. Otherwise the student has to re-take the failed course. The system needs to record
the year and term in which the course was taken and the grade awarded to the student. Every student has a unique ID. The
system also keeps the student name, birthday and the year he/she enrolled in the course.
a. Identify all the classes and objects that will be needed.
Campus
Faculty
Programme
Course
Result
Student
c. Explain how Object Oriented approach can make your program more robust, secure and efficient with reference to the
given scenario.
[FREE RESPONE] each of the above entity’s data (instance) is contained within the objects, making it hidden
from accidental changes and access from outside the object. For example student1 cannot access the data
contained in student2 instance. OR ANY OTHER OOP approach
d. Identify any one of the member functions which the compiler can inline.
Any of the member functions from Campus class can be inlined as there are setters mainly. For e.g. the
following:
Campus(string cName, string cAddress, float cDistance):name(cName),
address(cAddress),distance(cDistance)
2 OF 2
Q No. 3 [5 x 2 = 10 Points]
a. Consider the code snippet given below, identify and explain the error and also provide the corrected code.
Note: The access specifiers for the given attribute and function are not allowed to be changed
class Employee
{
string name;
set_Name(string a)
{
name = a;
}
};
int main()
{
Employee e1;
e1.set_Name("Rehan");
}
#include<iostream>
using namespace std;
class Employee
{
string name;
set_Name(string a)
{
name = a;
}
public:
my_func(string a)
{
set_Name(a);
}
};
int main()
{
Employee e1;
//e1.set_Name("Rehan");
e1.my_func("Rehan");
}
3 OF 2
b. Consider the code snippet given below, in which the constructors of class Car have been labeled as 1, 2, 3, and 4:
class Car {
public:
Car(); // 1
Car(int x); // 2
Car(int x, int y); // 3
Car(const Car& b); // 4
};
int main() {
Car b1(10); // A
Car* p = new Car(11); // B
Car arr[10]; // C
Car b2 = b1; // D
return 0;
}
A 2
B 2
C 1
D 4
- Good Luck -
4 OF 2