OOP Model Paper
OOP Model Paper
Instructions: Attempt ALL questions. Write clearly. Each section has its own marks.
3. In C++, which access specifier allows a member to be accessed by child class but NOT from
outside?
5. Which OOP pillar is achieved when you make data private and provide public getter/setter
functions?
2. The access specifier that allows access only within the class itself is called __________________.
3. In inheritance, the class that inherits properties from another class is called the
__________________ class.
4. A pure virtual function is declared using the syntax: virtual void funcName() =
__________________.
5. The destructor of a class named 'Student' is written as __________________.
Q1. What is the difference between a class and an object? A student says 'class and object are the
same thing.' Do you agree? Justify your answer with a real life example.
(3 marks)
Q2. Explain the difference between pass by value and pass by reference. If you want to swap two
numbers using a function, which method will you use and WHY? What happens if you use the wrong
method?
(3 marks)
Q3. What is Encapsulation? A programmer makes all data members public in a class. What problems
can this cause? How does making data private with getter/setter functions solve this problem?
(3 marks)
Q4. Explain the difference between Default Constructor and Parameterized Constructor. Why does a
constructor have NO return type — not even void? What would happen if you tried to call a
constructor manually?
(3 marks)
Q5. What is Inheritance in OOP? Can a child class access private members of its parent class? What
keyword do you use to allow child class access but block outside access? Why is inheritance useful
— what problem does it solve?
(3 marks)
Q6. What is the difference between Encapsulation and Abstraction? A student says 'both are the
same — they both hide things.' Correct the student with a proper explanation and one example for
each.
(3 marks)
Q1. (7 marks)
Write a complete C++ program for a BankAccount class with the following specifications:
Q2. (8 marks)
Write a complete C++ program demonstrating Inheritance with the following specifications:
• A parent class Animal with: name (string), age (int), eat() and sleep() functions
• A child class Dog inheriting Animal with: breed (string) and bark() function
• A child class Cat inheriting Animal with: color (string) and meow() function
• In main: create one Dog and one Cat object, set all values, and call all functions
• Show that child objects can use both parent functions AND their own functions
Q3. (7 marks)
Study the following code carefully and answer the questions below:
#include<iostream>
#include<string>
using namespace std;
class Student {
private:
string name;
int marks;
public:
Student() {
name = "Unknown";
marks = 0;
}
Student(string n, int m) {
name = n;
marks = m;
}
Student(Student &s) {
name = [Link];
marks = [Link];
}
void setMarks(int m) {
if (m >= 0 && m <= 100)
marks = m;
else
cout << "Invalid marks!" << endl;
}
void display() {
cout << "Name: " << name << endl;
cout << "Marks: " << marks << endl;
}
~Student() {
cout << name << " object destroyed!" << endl;
}
};
int main() {
Student s1;
Student s2("Ali", 85);
Student s3(s2);
[Link]();
[Link]();
[Link]();
[Link](150);
[Link](95);
[Link]();
return 0;
}
Answer the following questions about the code above:
(b) [1 mark] What will be the output of [Link]()? Which constructor was called for s3?
(c) [2 marks] What will happen when [Link](150) is called? What will [Link]() show after
[Link](95)?
(d) [1 mark] How many constructors does this class have? Name each one.
(e) [2 marks] What will be printed when the program ends (after return 0)? In what order will
destructors be called and why?