Object-Oriented Programming (OOP) MCQs for NSCT
Object-oriented programming (OOP) organises code around objects that bundle data with the methods that act on it, built on four ideas: encapsulation, inheritance, polymorphism and abstraction. It is topic 9 of the Programming (C++/Java/Python) area, which carries 10% of the NSCT according to HEC's Areas of Competencies. In 2026 candidates averaged 47.8% in Programming, per the HEC NSCT report and APP.
The three languages follow the same ideas with different rules, so a good OOP question always says which language it uses. Read that word first.
Classes
A class is a blueprint that declares fields (data) and methods (behaviour). An object is one instance of it, with its own copy of the instance fields. A constructor initialises a new object: it shares the class name in Java and C++, and is __init__ in Python.
class Student:
school = "FAST" # class attribute, shared
def __init__(self, name):
self.name = name # instance attribute
a, b = Student("Sara"), Student("Umar")
print(a.name, b.name, a.school) # Sara Umar FAST
Exam angle: a static field in Java or C++, or a class attribute in Python, is shared by every object. A mutable class attribute such as a list is a classic Python bug.
Inheritance
Inheritance lets a subclass reuse and extend a parent class; it models an "is-a" relationship. "Has-a" relationships use composition instead (a Car holds an Engine field).
| Java | C++ | Python | |
|---|---|---|---|
| Syntax | class B extends A |
class B : public A |
class B(A): |
| Multiple class inheritance | No (many interfaces allowed) | Yes (virtual inheritance for the diamond) |
Yes (C3 method resolution order) |
| Call the parent | super(...), super.m() |
A::m(), initialiser list |
super().m() |
Constructors run from the top of the hierarchy down. Exam angle: in Python, a subclass __init__ replaces the parent's, so forgetting super().__init__() leaves the parent's attributes unset.
Polymorphism
Polymorphism means one interface, many forms: the same call does different things depending on the object.
| Overloading | Overriding | |
|---|---|---|
| Where | Same class (or scope) | Subclass redefines an inherited method |
| Signature | Same name, different parameters | Same name and parameters |
| Decided | Compile time (static) | Run time (dynamic dispatch) |
| Python | Not supported; the last def wins |
Supported |
Dynamic dispatch means the method is chosen by the object's actual class, not the variable's declared type. In Java every non-static, non-private, non-final instance method is dispatched this way. In C++ only functions declared virtual are; a non-virtual call through a base pointer runs the base version. Fields and static methods are never dispatched dynamically in Java. Exam angle: in Animal a = new Dog(); a.sound();, Java runs Dog's sound().
Encapsulation
Encapsulation hides an object's data behind methods, so the class can enforce its own rules (for example, a balance that may not go negative).
| Level | Java | C++ |
|---|---|---|
private |
Same class only | Same class (and friends) |
| (no keyword) | Package-private: same package | private in a class, public in a struct |
protected |
Same package and subclasses | Class, friends and derived classes |
public |
Everywhere | Everywhere |
Python has no enforced access control: _name is a convention, and __name is name-mangled to _Class__name. Exam angle: Java's protected is wider than C++'s because it includes the whole package.
Abstraction
Abstraction exposes what an object does and hides how. Abstract classes and interfaces are the tools.
| Java | C++ | Python | |
|---|---|---|---|
| Abstract class | abstract class; may have fields, constructors, concrete methods |
Any class with a pure virtual function = 0 |
Subclass of ABC with an @abstractmethod |
| Interface | interface: constants only, abstract methods, plus default/static (Java 8+) |
No keyword; a class of only pure virtual functions | No keyword; ABCs or duck typing |
| Instantiate? | No | No | No (TypeError) |
Exam angle: choose an abstract class when related classes share state and code, and an interface when unrelated classes share a capability.
Common traps
- Thinking a different return type alone is a valid overload; it is a compile error in Java and C++.
- Expecting a non-virtual C++ function to be overridden through a base pointer or reference.
- Expecting Java fields or static methods to behave polymorphically.
- Narrowing access when overriding (public to protected) in Java; the compiler rejects it.
- Writing
w = w;instead ofthis.w = w;in a constructor. - Deleting a derived C++ object through a base pointer without a virtual destructor, which is undefined behaviour.
Worked examples
Understanding. In Java, which kind of polymorphism does method overloading provide? Answer: compile-time (static) polymorphism. The compiler picks the method from the argument types; overriding gives run-time polymorphism.
Application. In Python, class A: def f(self): return "A" + self.g() with def g(self): return "1", and class B(A): def g(self): return "2". What does B().f() return?
Answer: "A2". f is inherited from A, but self is a B, so self.g() dispatches to B.g.
Analysis. A C++ Shape class has double area() { return 0; }, and Circle defines its own area(). A std::vector<Shape*> of circles reports total area 0. What is wrong?
Answer: area() is not virtual. Calls through Shape* are bound at compile time to Shape::area. Declaring virtual double area() (better, = 0 to make Shape abstract) enables dynamic dispatch. Add a virtual destructor too.
Practise
The quiz below has 100 Object-Oriented Programming questions, which you can filter by tier and difficulty. For longer code-based practice, see OOP code questions.
Practise: 100 questions
FAQs
What are the four pillars of OOP?
Encapsulation (hiding data behind methods), inheritance (reusing a parent class), polymorphism (one call, many behaviours) and abstraction (exposing what, hiding how). Some textbooks list three pillars and treat abstraction as part of encapsulation, so read the options carefully.
What is the difference between an abstract class and an interface in Java?
An abstract class can hold instance fields, constructors and concrete methods, and a class can extend only one. An interface holds constants and method declarations (plus default and static methods since Java 8), and a class can implement many. Neither can be instantiated directly.
Does Python support method overloading?
Not by signature. If a class defines two methods with the same name, the second replaces the first. Python achieves similar results with default arguments, *args and **kwargs, or functools.singledispatchmethod for type-based dispatch. Overriding works as in Java.
Why does C++ need the virtual keyword?
C++ binds member function calls at compile time by default, which is faster. virtual tells the compiler to dispatch through the object's vtable at run time, so a call through a base pointer or reference reaches the derived override. Java makes this behaviour the default for instance methods.
Next step
Continue with Memory Management MCQs, go back to the Programming hub, or try a timed NSCT mock test.