AN Solution · Computer Science Notes ⬇ PDF
AN SolutionComputer Science · Notes
PAGE1
AN
AN Solution Pakistan

Short Questions & Answers

Computer Science
Chapter 3 — Object Oriented Programming Using Python
Complete short-question notes prepared from the textbook · For test preparation
A Basics of OOP
Q1.What is Object-Oriented Programming (OOP)?
OOP is a method of designing programs using objects and classes, where each object represents a real-world entity with specific properties and behaviours. It emphasises encapsulation, inheritance and polymorphism to manage data and behaviour efficiently.
Q2.Define a class.
A class is a blueprint or template that defines the properties (attributes) and actions (methods) of an entity. It is used to create multiple objects with similar characteristics.
Q3.Define an object.
An object is an instance of a class that holds actual values for its attributes. It represents a real-world entity in code.
Q4.What is the relationship between a class and an object?
A class is like a blueprint, while an object is the real thing created from that blueprint. One class can be used to create many objects.
Q5.What are attributes and methods?
Attributes are the properties (data/variables) of an entity and methods are the actions (functions) it can perform. Example — a Person has attributes name, age, gender and methods eat(), sleep(), work().
Q6.Write any two benefits of OOP.
Modularity — code is divided into modules, making it easier to manage; and Reusability — classes and objects can be reused in many programs, reducing repetition. (It also gives easier maintenance and scalability.)
Q7.How does OOP make programs easier to maintain?
Programs built with OOP are easier to maintain and update because a change in one part does not affect the entire system.
Q8.Name the core concepts of OOP.
Classes and objects, encapsulation, inheritance and polymorphism.
Q9.What is the purpose of the __init__ method?
The __init__ method initialises (sets) the attribute values, such as the name, automatically when an object is created.
Q10.What does self represent?
self is the reference to the current object, passed automatically to instance methods.
Q11.How do you create an object of a class?
By calling the class name with the required values, e.g. object1 = Student("Ali").
AN SOLUTION PAKISTAN · COMPUTER SCIENCE · CHAPTER 3
AN SolutionComputer Science · Notes
PAGE2
B Encapsulation & Access Control
Q12.Define encapsulation.
Encapsulation is an OOP concept in which we bundle data (variables) and methods (functions) into a single unit (class) and restrict direct access to the data.
Q13.Does Python have strict “private” or “protected” keywords?
No. Unlike Java or C++, Python does not have strict private or protected keywords; it relies on naming conventions to signal how data should be accessed.
Q14.Why is encapsulation important?
It protects data from unwanted access, improves security, makes code easier to manage, and allows controlled modification.
Q15.Give a real-life example of encapsulation.
A bank account — the balance is hidden and can only be accessed using methods like deposit() and withdraw(); you cannot directly change the balance.
Q16.What are the levels of access control in Python?
Public members, Protected members and Private members.
Q17.What are public members?
Members accessible from anywhere — inside or outside the class. By default, all members in Python are public. Syntax: self.attribute_name.
Q18.What are protected members?
Members intended for internal use within the class and its subclasses. It is a convention (not a restriction) that they should not be accessed from outside. Syntax: self._attribute_name (single underscore).
Q19.What are private members?
Highly restricted members that cannot be easily accessed from outside the class. Syntax: self.__attribute_name (double underscore).
Q20.What is name mangling?
Name mangling is a mechanism Python uses to change the internal name of a private variable to prevent accidental access.
Q21.In the BankAccount example, what is the output of print(account.get_balance())?
1200 — starting 1000, deposit 500, withdraw 300 → 1000 + 500 − 300 = 1200.
Q22.Why does print(account.__balance) give an error?
Because __balance is a private variable, so direct access from outside the class is not allowed.
Remember: single underscore _name → protected · double underscore __name → private · no underscore name → public.
AN SOLUTION PAKISTAN · COMPUTER SCIENCE · CHAPTER 3
AN SolutionComputer Science · Notes
PAGE3
C Inheritance
Q23.Define inheritance.
Inheritance allows a class to take the properties and methods of another class. The new class (subclass) can add or modify features of the parent class.
Q24.What are the benefits of inheritance?
It promotes code reuse, reduces redundancy, and makes it easier to extend programs without rewriting existing code.
Q25.Name the types of inheritance.
Single inheritance, Multiple inheritance and Multilevel inheritance.
Q26.What is single inheritance?
Single inheritance occurs when a child class (subclass) inherits attributes and methods from exactly one parent class (superclass). It is the most fundamental form of inheritance in Python.
Q27.What is a parent class (base class)?
The class whose properties are inherited (for example, Animal in the Dog example).
Q28.What is a child class (derived class)?
The class that inherits from the parent. It is defined by passing the parent class name in parentheses: class Child(Parent):
Q29.What is method overriding?
When a child class provides its own specific implementation of a method that is already provided by its parent class.
Q30.What is the use of the super() function?
The super() function is used to call the parent class’s version of a method.
Q31.What is multiple inheritance?
Multiple inheritance allows a single class to inherit features from more than one parent class, combining behaviours from different sources — e.g. a Smartphone inheriting from both a Camera and a Computer class.
Q32.What is multilevel inheritance?
Multilevel inheritance occurs when a class inherits from another derived class, creating a chain (Grandparent → Parent → Child). The grandchild class can access all methods and attributes of its parent and grandparent.
Q33.In multilevel inheritance, what can the “grandchild” class access?
All the methods and attributes from both its immediate parent and its grandparent.
AN SOLUTION PAKISTAN · COMPUTER SCIENCE · CHAPTER 3
AN SolutionComputer Science · Notes
PAGE4
D Example / Code-Based Short Questions
Q34.What does the display() method do in the Student class?
It prints the student’s name.
Q35.In class Dog(Animal):, which type of inheritance is shown?
Single inheritance — Dog is the child class inheriting from the parent class Animal.
Q36.If my_dog = Dog("Buddy"), what is the output of my_dog.eat()?
Buddy is eating. (the eat() method is inherited from the parent class Animal).
Q37.What is the output of my_dog.bark()?
Buddy says Woof! (the bark() method is defined in the child class Dog).
Q38.In class Child(Father, Mother):, which type of inheritance is used?
Multiple inheritance — the child inherits from two parent classes.
Q39.Which prefix is used for a private variable in Python?
A double underscore __, e.g. self.__balance.
Q40.Which prefix is used for a protected member?
A single underscore _, e.g. self._attribute_name.
Q41.Write a simple Python class with one attribute and one method.
Example of a basic class (from the chapter):
class Student: def __init__(self, name): self.name = name def display(self): print("Student Name:", self.name) object1 = Student("Ali") object1.display() # Student Name: Ali
Tip for the test: learn the one-line definitions (OOP, class, object, encapsulation, inheritance and its three types) and the three access levels with their underscore symbols — these are the most common short questions from this chapter.
© AN Solution Pakistan · Prepared for student test preparation · Chapter 3