PCAP · domain
Object-Oriented Programming
This domain covers Python's object-oriented programming features: class definition, inheritance, polymorphism, encapsulation, special methods, and the @property decorator. Questions present code snippets or design scenarios and ask you to predict behavior, choose the correct syntax, or select the best approach. You must understand method resolution, attribute access, and how Python implements OOP without explicit interfaces.
Focused practice
Practice Object-Oriented Programming questions
Scored sessions drawing only from this domain — pick a length below.
Start 20-question practice test →What this domain covers
What to know about Object-Oriented Programming
You must be able to read and write Python classes that use inheritance, encapsulation, and special methods correctly. The single most important thing is to understand how attribute lookup works, including instance vs class attributes and the role of @property for controlled access.
Creating classes with __init__, instance attributes, and class attributes
Using inheritance, super(), and method overriding for polymorphism
Implementing encapsulation with name mangling and @property getters/setters
Defining special methods like __str__, __repr__, and __eq__
Why learners struggle
Why Object-Oriented Programming questions are commonly missed
RAM questions are commonly missed because learners confuse physical form factors (DIMM vs SO-DIMM) and fail to distinguish between memory speed (MHz) and latency (CL).
- ·DIMM vs SO-DIMM — desktop vs laptop form factor confusion
- ·DDR3 vs DDR4 vs DDR5 — notch position and voltage differences
- ·MHz vs CL — speed vs latency trade-offs in performance
- ·Single-channel vs dual-channel — bandwidth impact misconception
- ·ECC vs non-ECC — error correction support in servers vs desktops
- ·32-bit vs 64-bit — maximum addressable RAM limit
Watch out for
Common Object-Oriented Programming exam traps
- ▸Confusing class attributes with instance attributes, leading to shared state across instances.
- ▸Forgetting that name mangling uses double underscore prefix and does not truly prevent access.
- ▸Misusing @property setter syntax, such as missing @score.setter or incorrect method names.
Question index
All Object-Oriented Programming questions (109)
Click any question to see the full explanation, or start a practice session above.
Given the following code: class Parent: def show(self): print('Parent') class Child(Parent): def show(self): print('Child') c = Child(); c.show(); super(Child, c).show(). What is the output?
Medium2A parent class defines a method `move()`. A child class overrides `move()` but also needs to call the parent's version inside it. Which syntax allows that?
Easy3A junior developer writes a class 'Logger' that should only ever have one instance (singleton). They attempt to implement it by overriding __new__ to always return the same instance. However, when multiple threads attempt to create a Logger, they sometimes get different instances. Which modification will make the singleton thread-safe?
Easy4Refer to the exhibit. What is the output? (Note: actual MRO may vary; choose the one that matches Python 3 C3 linearization.)
Hard5Drag and drop the steps to define and call a function with default arguments in Python into the correct order.
Medium6A programmer writes a class with a method that should be called on the class itself, not on instances. Which decorator is appropriate?
Easy7A class 'MyClass' has a method 'do_something' that uses 'self.__private'. A subclass 'MySubClass' tries to access 'self.__private' and gets an AttributeError. Why?
Hard8A developer wants to ensure that an attribute 'balance' of a BankAccount class cannot be accessed directly from outside the class but can be accessed through a method. Which approach should be used?
Easy9A developer designs a plugin system where each plugin must implement a method 'execute'. Which code snippet correctly enforces that subclasses provide an implementation using the 'abc' module?
Medium10Refer to the exhibit. What is printed?
Hard11Given that MyClass defines __private_attr in __init__, why does this error occur?
Medium12Refer to the exhibit. What will be the output when the code is executed?
Medium13A programmer wants to restrict a class to only allow specific attribute names and reduce memory usage. Which feature should they use?
Hard14A developer creates classes `A`, `B(A)`, `C(A)`, and `D(B, C)`. When calling a method from `D` that is defined in `A`, which class's version is used according to Python's MRO?
Medium15Refer to the exhibit. What happens when the last command is executed?
Hard16A developer is designing a class hierarchy for a library system. They want to ensure that a method 'borrow' in the base class 'Item' can be overridden by subclasses like 'Book' and 'DVD', but the base implementation should not be callable directly. Which approach best achieves this?
Medium17A developer wants to create a class that logs every attribute access on an instance. Which special method should they override?
Easy18A class defines an __init__ method that takes optional arguments. What is the correct way to provide default values?
Easy19Which TWO of the following statements about Python classes are true? (Select exactly 2.)
Medium20Which of the following is a correct use of the @property decorator to create a getter and setter for an attribute named 'score' that ensures score stays between 0 and 100?
Hard21A class `Circle` has a class attribute `pi = 3.14`. An instance `c = Circle()` sets `c.pi = 3.14159`. What is the value of `Circle.pi` after this assignment?
Easy22A class has both `@classmethod` and `@staticmethod` decorators. What is a key difference between them?
Medium23Which of the following correctly uses `__slots__` to restrict attribute creation to only `x` and `y`?
Hard24A class defines a variable `count = 0`. An instance modifies `self.count = 5`. What is the value of `count` in the class namespace?
Easy25A subclass overrides a method but wants to call the parent class's version. Which keyword should be used?
Easy26Match each Python operator to its precedence level (1=highest).
Medium27A team is developing a data processing pipeline where each step is a class that implements a common interface. They have defined an abstract base class DataProcessor with an abstract method process(data). Several concrete subclasses implement process. Now they need to add a new step that logs the data before processing. They want to reuse the existing processing logic without modifying the original classes. Which design pattern should they apply?
Medium28A developer defines a class with an __init__ method that sets instance attributes. Which of the following is the correct way to call the parent class's __init__ from a child class?
Easy29Which three statements about the Method Resolution Order (MRO) in Python are true? (Choose three.)
Medium30A developer wants a class 'Point' to have a readable string representation that returns 'Point(x, y)'. Which special method should be overridden?
Easy31A developer writes a class 'Logger' with a class method 'log(msg)' that writes to a file. Another class 'AppLogger' inherits from 'Logger'. The developer expects both classes to share the same file handle. However, after creating an instance of 'AppLogger', the file handle is different. What is the most likely cause?
Hard32An engineer is debugging an application that uses inheritance. The base class 'Vehicle' defines a method 'start()' that prints 'Vehicle started'. The subclass 'Car' overrides 'start()' to print 'Car started'. The code contains a function that accepts a Vehicle object and calls 'start()'. What is the output if a Car object is passed?
Medium33What is the output of the code?
Medium34A programmer uses a class method to create an alternative constructor for a `Point` class. The method should parse a string like "10,20" and return a `Point` instance with x=10, y=20. Which code snippet correctly implements this?
Medium35A developer is building a simulation of different types of vehicles. They have a base class Vehicle with an attribute speed initialized in __init__. They also have a subclass Car that inherits from Vehicle and adds an attribute fuel_type. The developer wants to ensure that every time a Car object is created, it also initializes the speed attribute from the Vehicle class. Which approach should the developer use?
Easy36A developer is designing a system where a `Car` class needs to reuse functionality from `Engine` and `Transmission` without creating a deep hierarchy. Which OOP principle should be applied?
Medium37A developer creates a Python class with a method that is intended to be overridden in subclasses. Which approach best ensures that the method is not accidentally called on the base class?
Easy38A developer wants a class 'LoggedDict' that behaves like a dict but logs all attribute access in the console. Which method override correctly implements this for getting an attribute?
Hard39You are working on a legacy system that processes financial transactions. The system uses a class hierarchy: Transaction (base), Deposit, Withdrawal, Transfer. Each subclass overrides a method 'process()' to handle its specific logic. The code often runs in a multi-threaded environment and you notice intermittent errors where a transaction is processed twice. The logging shows that the same transaction object is being passed to the process method multiple times. The transaction objects are created from a factory function that caches recently used transactions. The errors seem to occur when two threads call the factory at the same time with the same parameters. After investigating, you find that the factory uses a class-level dictionary to cache objects. Which of the following is the most appropriate solution to prevent double processing?
Hard40Which TWO of the following statements about class attributes in Python are true?
Easy41A company is developing a scientific simulation framework where many different solvers must be interchangeable. The framework should enforce that each solver implements methods 'initialize' and 'step'. Developers want to use abstract base classes. Which approach should the team take to ensure that any subclass of 'Solver' cannot be instantiated unless it defines both methods?
Medium42A development team is building a real-time chat application using Python. The application uses a class 'ChatRoom' that maintains a list of 'User' objects as active participants. Each User object holds a reference back to its ChatRoom to send messages. Over time, the application runs out of memory. Profiling reveals that User objects are not being garbage collected even after users disconnect. The team suspects circular references. Which solution would effectively resolve the memory leak without breaking the functionality?
Hard43A class `Point` has an `__init__` that sets `self.x` and `self.y`. They want to compare points by equality (i.e., `p1 == p2` should work correctly). Which method should they implement?
Easy44Which of the following correctly uses an abstract base class to enforce that all subclasses implement a 'make_sound' method? (Assume ABC imported)
Hard45Which TWO of the following are special methods in Python?
Easy46Given: class A: def method(self): print('A'); class B(A): def method(self): super().method(); print('B'); class C(A): def method(self): super().method(); print('C'); class D(B, C): pass. What is printed by D().method()?
Medium47Which TWO of the following are valid ways to define a property in a Python class? (Select exactly 2.)
Easy48Which THREE statements about the Python method resolution order (MRO) are true? (Select exactly 3.)
Hard49Which TWO statements about static methods (@staticmethod) are correct?
Easy50Which THREE of the following are true about Python's object-oriented programming features?
Hard51Which of the following statements about class inheritance in Python are true? (Choose two.)
Easy52A developer is implementing a simple counter class. The class should start at 0 and increment by 1 each time the 'increment' method is called. Which implementation is correct?
Easy53A programmer wants to ensure that a class attribute is the same for all instances and can be accessed via the class name. Which type of variable should be defined?
Easy54A team is developing a banking system in Python. They have a base class Account with attributes account_number and balance, and a method __init__(self, account_number, balance) that initializes these attributes. They then create a subclass SavingsAccount that adds an attribute interest_rate. In SavingsAccount's __init__, they assign self.interest_rate = rate but do not call super().__init__. When they instantiate SavingsAccount('12345', 1000, 0.02) and attempt to print(balance), an AttributeError occurs: 'SavingsAccount' object has no attribute 'balance'. What is the most appropriate fix to ensure that the SavingsAccount includes balance and account_number without code duplication?
Hard55A team wants a `Logger` class that can be used both as `Logger.log('msg')` on the class itself and as `logger.log('msg')` on an instance, with identical behaviour and no access to instance state. Which decorator should be applied to `log`?
Medium56An abstract base class (ABC) defines an abstract method `process()`. Several subclasses implement it. A function accepts any subclass and calls `process()`. This demonstrates which OOP concept?
Hard57An application uses a class to represent a configuration object that reads settings from a file. The class has a class attribute config_cache that holds a dictionary of loaded configurations to avoid repeated file reads. However, the developer notices that when they modify the dictionary for one instance, it affects all instances. They want to ensure that each instance has its own copy of the configuration data upon initialization. Which change should they make?
Medium58A developer wants to ensure that a class attribute is shared among all instances but cannot be modified from outside the class. Which approach is most appropriate?
Easy59A team is implementing a shape hierarchy with a base class `Shape` that should have an `area()` method. They want to ensure that every subclass must provide its own implementation of `area()`. Which approach should they use?
Medium60Consider a class `D` that inherits from multiple base classes `B` and `C`. The developer wants to call a method from a specific parent class while ensuring correct method resolution order (MRO). Which is the safest way?
Medium61A programmer writes a class with a static method using @staticmethod. What is the primary purpose of using a static method instead of a class method or instance method?
Medium62A developer writes a class `Vector` and wants `v1 + v2` to return a brand-new `Vector`, while `v1 += v2` should mutate `v1` in place and return it. Which pair of special methods achieves this behaviour?
Hard63A developer is working on a class hierarchy for geometric shapes. They have a base class Shape with an abstract method area(). They also have a mixin class Drawable that provides a method draw(). They want to create a class Rectangle that inherits from both Shape and Drawable. However, they encounter a TypeError when trying to instantiate Rectangle because the abstract method area() is not implemented. Which action should they take to resolve this?
Hard64Which THREE of the following are true about the `__init__` method in Python?
Medium65A developer defines a class 'A' with a method 'm' that uses 'self.a'. Class 'B' inherits from 'A' and defines __init__ that sets 'self.a = 10'. An instance of B is created and method m is called. What is the output?
Hard66A class `Vector` defines `__add__` to return a new `Vector`. A developer writes `v1 = Vector(1,2); v2 = Vector(3,4); v3 = v1 + v2`. What is the type of `v3`?
Medium67When a class defines both __getattr__ and __getattribute__, which one is called when accessing an attribute that exists in the instance?
Hard68A class MyClass defines __str__ and __repr__ methods. What is the purpose of __repr__?
Easy69A developer wants to implement a read-only property for a class 'Temperature' that returns the temperature in Celsius but prevents external modification. Which code snippet correctly defines such a property?
Easy70A team is developing a system that must handle different types of documents (PDF, Word, etc.). Each document type has a unique parsing method. To avoid massive conditional logic, which OOP concept should be applied?
Medium71Refer to the exhibit. Which of the following correctly shows the MRO of class D?
Medium72Which TWO statements about Python's name mangling are correct?
Hard73A Python class 'Shape' defines an abstract method 'area'. Subclasses 'Circle' and 'Square' implement 'area'. A function 'calculate_area(shape)' expects a 'Shape' instance. Which principle ensures that the function works correctly without knowing the specific subclass?
Hard74A developer defines a class with a private attribute `_value` and wants to provide controlled access. Which approach is the most Pythonic?
Easy75Which THREE of the following are characteristics of Python's special methods (dunder methods)? (Select exactly three.)
Hard76A developer writes a class `Logger` with a method `log(self, message)`. They want to ensure that when `Logger` is subclassed, any override of `log` must call the parent's `log` method. Which technique best enforces this requirement in Python?
Hard77Match each list method to its effect.
Medium78Consider the following class hierarchy: class A: def method(self): return 'A'; class B(A): pass; class C(A): def method(self): return 'C'; class D(B, C): pass. What is the output of D().method() according to Python's MRO?
Medium79Consider the following code snippet: 'class A: pass; class B(A): pass; class C(A): pass; class D(B, C): pass'. What is the Method Resolution Order (MRO) for class D according to the C3 linearization algorithm used by Python?
Hard80A class has a class attribute that is a list. A developer modifies this list via one instance, and the change is reflected in all other instances. What is the best practice to avoid this unintended sharing?
Hard81Refer to the exhibit. What is the output of the last line?
Hard82Refer to the exhibit. What happens when the last line is executed?
Easy83A Python class 'BankAccount' has a method 'withdraw(amount)' that deducts 'amount' from 'self.balance'. A developer writes a subclass 'SavingsAccount' that overrides 'withdraw' to add a penalty if balance drops below minimum. Which design pattern is being used?
Medium84Which of the following best describes the use of the @abstractmethod decorator in Python?
Hard85A company needs to model different types of employees. They have a base class `Employee` with a method `calculate_pay()`. For hourly employees, pay = hours * rate; for salaried employees, pay = salary. Which design approach is most appropriate?
Easy86A class 'Point' is defined with __slots__ = ['x', 'y']. A developer creates an instance p = Point() and tries to set p.z = 10. What happens?
Medium87A developer defines a class 'Car' with an attribute 'wheels = 4'. They create two instances: car1 = Car() and car2 = Car(). Then they set car1.wheels = 3. What is the value of car2.wheels?
Easy88A junior developer wrote a class representing a bank account with a private attribute balance. They used double underscore prefix (__balance) to make it private. However, in a test script, they are still able to access the attribute using the mangled name _Account__balance. The developer is confused about why encapsulation is not enforced. Which statement best explains this behavior?
Easy89Consider the following code: class A: x = 1; class B(A): pass; class C(A): x = 2; class D(B, C): pass. What is D.x?
Medium90Refer to the exhibit. What is the root cause of the error?
Hard91A programmer wants to create a class that cannot be instantiated directly, only through a factory method. Which approach should be used?
Medium92A class `ServerConfig` has a class attribute `port = 8080`. After deployment, a developer runs `ServerConfig.port = 9090` in one module, and unexpectedly all existing instances now use port 9090. What concept explains this behavior?
Hard93A class has a method that does not access any instance or class data. What decorator should be used to define it as a utility method that can be called on the class itself without requiring an instance?
Easy94Which TWO statements about method overriding in Python are correct?
Medium95A developer wants to ensure that the 'radius' attribute of a Circle class is always non-negative. Which implementation using @property is correct?
Hard96A Python developer is implementing a class that should behave like a sequence and support indexing. Which pair of special methods must be defined to achieve this?
Hard97A class named Counter has a class variable count and an instance method increment. The method is defined as def increment(self): Counter.count += 1. What will be the output after executing the following code? c1 = Counter(); c2 = Counter(); c1.increment(); c2.increment(); print(Counter.count, c1.count, c2.count)
Medium98Refer to the exhibit. Which THREE statements about the class hierarchy are correct?
Hard99A developer writes a class `Circle` with `def __init__(self, radius): self.radius = radius` and wants printing a `Circle` instance to produce a readable description such as `Circle(5)`. Which method should be implemented, and why over the alternative?
Easy100A class inherits from two parent classes that both have a method with the same name. When calling the method on the child, only one parent's version is executed. What Python mechanism determines which one?
Medium101Which TWO of the following are valid ways to define a class method in Python? (Select exactly two.)
Medium102Refer to the exhibit. What will happen when the code is executed?
Medium103A class has an attribute that should be computed on access and cached for subsequent accesses. Which pattern is most appropriate?
Easy104A developer defines a class hierarchy with multiple inheritance: class A, class B(A), class C(A), class D(B,C). The method 'm' is defined only in A. What is the method resolution order for D according to the C3 linearization algorithm?
Medium105Which TWO of the following are valid ways to define a class attribute that is shared among all instances?
Hard106A developer creates a metaclass 'Meta' that modifies class creation by adding a class attribute 'created_by' set to 'Meta'. Which code snippet correctly defines and uses this metaclass?
Hard107A developer wants to ensure that a class attribute is shared across all instances. Which approach should be used?
Easy108Which of the following is true regarding Python's method resolution order (MRO) in multiple inheritance?
Hard109A team is using inheritance but wants to prevent a method from being overridden in subclasses. What Python feature can enforce this?
MediumOther domains
All PCAP exam domains
Frequently asked questions
- What does the Object-Oriented Programming domain cover on the PCAP exam?
- You must be able to read and write Python classes that use inheritance, encapsulation, and special methods correctly. The single most important thing is to understand how attribute lookup works, including instance vs class attributes and the role of @property for controlled access.
- How many questions are in this domain?
- This page lists all 109 Object-Oriented Programming questions in the PCAP question bank. The actual exam draws from this domain proportionally to its weighting in the official exam blueprint.
- What is the best way to practise this domain?
- Start with a short focused session (10 questions) to identify gaps, then work through explanations. Repeat with a longer session once the weak areas feel solid.
- Can I practise only Object-Oriented Programming questions?
- Yes — the session launcher on this page filters questions to this domain only. Choose any session length for inline explanations and scoring.