wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python OOP Quiz

Total questions: 151

Worksheet time: 1hrs 16mins

Name
Class
Date
1.

What is the purpose of the __init__ method?

a)

To delete an object

b)

To initialize the object

c)

To inherit from another class

d)

To print values

2.

Which of these is used to refer to the instance itself inside a class method?

a)

self

b)

this

c)

cls

d)

me

3.

Which of the following denotes a private attribute in Python?

a)

*name

b)

**name

c)

name**

d)

name*

4.

Which method is automatically called when an object is created?

a)

__str__

b)

__new__

c)

__init__

d)

__call__

5.

What is the correct way to define a class named 'Car'?

a)

def Car:

b)

class Car():

c)

object Car():

d)

Car = class()

6.

Select the valid instance attribute declarations.

a)

self.name = name

b)

name = self.name

c)

self_name = name

d)

this.name = name

7.

Which of the following are valid visibility modifiers in Python?

a)

Public

b)

Protected (_)

c)

Private (__)

d)

Global

8.

What is inheritance in Python used for?

a)

To override built-in functions

b)

To pass attributes to a class

c)

To derive new classes from existing ones

d)

To hide class data

9.

Which function is used to call the parent class method?

a)

parent()

b)

super()

c)

base()

d)

main()

10.

What does MRO stand for in Python?

a)

Method Run Order

b)

Method Resolution Order

c)

Multiple Resolution Output

d)

Main Root Order

11.

Which type of inheritance leads to the diamond problem?

a)

Single

b)

Multiple

c)

Multilevel

d)

Hierarchical

12.

What keyword is used to inherit from a base class?

a)

extends

b)

inherits

c)

super

d)

class Derived(Base):

13.

Choose the correct example of multiple inheritance.

a)

class C(A, B):

b)

class C(B -> A):

c)

class C = A + B:

d)

class C inherit A, B

14.

Select all the classes involved in MRO.

a)

Base class

b)

Derived class

c)

Sibling class

d)

Interface class

15.

What are the benefits of using super() in inheritance?

a)

Calls the base class constructor

b)

Avoids hardcoding class names

c)

Improves maintainability

d)

Slows down performance

16.

What does polymorphism allow in OOP?

a)

Use same name methods for different classes

b)

Overload constructors

c)

Create multiple instances

d)

Hide data

17.

Which method can be overridden in a subclass?

a)

Any method

b)

Only static methods

c)

Only private methods

d)

Only class methods

18.

Which module provides abstract base classes in Python?

a)

abc

b)

abstract

c)

base

d)

interface

19.

Which decorator is used to declare an abstract method?

a)

@classmethod

b)

@abstractmethod

c)

@staticmethod

d)

@override

20.

Polymorphism can be achieved through:

a)

Method overriding

b)

Method hiding

c)

Method chaining

d)

Method duplication

21.

Select all that apply to abstract classes:

a)

Can contain abstract methods

b)

Cannot be instantiated

c)

Must override all methods

d)

Defined using 'abc' module

22.

Which of these describe polymorphism?

a)

One interface, multiple implementations

b)

Single inheritance only

c)

Multiple classes with same methods

d)

Overriding methods

23.

What is encapsulation in OOP?

a)

Hiding internal state

b)

Accessing private data

c)

Using multiple classes

d)

Calling superclass methods

24.

Which keyword is used to create a property in Python?

a)

@getter

b)

@access

c)

@property

d)

@value

25.

What is the purpose of getter methods?

a)

To modify data

b)

To return attribute values

c)

To initialize class

d)

To set property values

26.

Select all the benefits of using @property:

a)

Controlled access

b)

Validation

c)

Cleaner syntax

d)

Multiple inheritance

27.

Which decorators are typically used for controlled access?

a)

@property

b)

@<property>.setter

c)

@classmethod

d)

@staticmethod

28.

Which of these demonstrate encapsulation?

a)

Private attributes

b)

Getters and setters

c)

Inheritance

d)

Global variables

29.

How can you define a setter in Python?

a)

@property_name.setter

b)

@setter.property

c)

@property.setter

d)

@setproperty

30.

What will be the output?

class A: 

def process(self): 

print("A.process") 

class B(A): 

def process(self): 

super().process() 

print("B.process") 

class C(A): 

def process(self): 

super().process() 

print("C.process") 

class D(B, C): 

def process(self): 

super().process() 

print("D.process") 

d = D() 

d.process() 

a)

A.process B.process C.process D.process

b)

A.process C.process B.process D.process

c)

D.process B.process C.process A.process

d)

B.process C.process D.process A.process

31.

Which methods will be executed in order?

class Base: 

def call(self): 

print('Base') 

class Left(Base): 

def call(self): 

super().call() 

print('Left') 

class Right(Base): 

def call(self): 

super().call() 

print('Right') 

class Sub(Left, Right): 

def call(self): 

super().call() 

print('Sub') 

s = Sub() 

s.call() 

a)

Base → Right → Left → Sub

b)

Base → Left → Right → Sub

c)

Sub → Left → Right → Base

d)

Base → Right → Sub → Left

32.

What will the following output?

class Parent: 

value = 100 

class Child(Parent): 

def init(self): 

self.value = 200 

c = Child() 

print(c.value) 

print(Child.value) 

print(Parent.value) 

a)

200 200 100

b)

200 100 100

c)

100 200 100

d)

200 100 200

33.

Identify the error in this code:

from abc import ABC, abstractmethod 

class Shape(ABC): 

@abstractmethod 

def area(self): 

pass 

class Circle(Shape): 

pass 

c = Circle() 

print(c.area()) 

a)

No error, output is None

b)

Circle must implement area() method

c)

Shape class is wrong

d)

circle() constructor is missing arguments

34.

What will be printed?

a)

5

b)

10

c)

Error

d)

None

35.

What will be the output of the following code?

a)

Animal speaks

b)

Dog barks

c)

Error

d)

Nothing

36.

What attribute will the object 'p' have after this code?

a)

name

b)

self

c)

p

d)

Alice

37.

Which method will be called when creating an object?

a)

__init__

b)

hello

c)

__new__

d)

self

38.

What will the following print?

a)

A

b)

B

c)

A B

d)

B A

39.

What is printed?

a)

4

b)

Error

c)

None

d)

Undefined

40.

Which line properly creates a private attribute?

a)

self.hidden

b)

self._hidden

c)

self.__hidden

d)

hidden

41.

What is the method resolution order for this hierarchy?

a)

D → B → A → C

b)

D → B → C → A

c)

D → C → B → A

d)

D → A → B → C

42.

Which decorator is used to define a getter property?

a)

@getter

b)

@property

c)

@age

d)

@get_age

43.

What does the following code do?

a)

Prints 5

b)

Error

c)

Prints None

d)

Prints _value

44.

What will happen?

a)

Dog runs

b)

Error because of abstract method

c)

Nothing

d)

Dog walks

45.

What programming paradigm focuses on data flow and avoids changing state?

a)

Procedural

b)

Functional

c)

Object-oriented

d)

Declarative

46.

Which OOP concept restricts direct access to an object's data and behavior?

a)

Inheritance

b)

Composition

c)

Encapsulation

d)

Polymorphism

47.

A class is best described as:

a)

A reusable function

b)

A blueprint for creating objects

c)

A special loop structure

d)

A method container

48.

When one class derives features from another, it's called:

a)

Abstraction

b)

Encapsulation

c)

Inheritance

d)

Reflection

49.

Which concept allows methods to behave differently based on the object?

a)

Inheritance

b)

Overloading

c)

Polymorphism

d)

Shadowing

50.

What defines a class instance in memory?

a)

Class name

b)

Object

c)

Attribute

d)

Module

51.

What function initializes object attributes upon instantiation?

a)

del

b)

init

c)

call

d)

setup()

52.

What keyword refers to the current object within class methods?

a)

self

b)

this

c)

obj

d)

ref

53.

Which symbol combination is typically used for private attributes?

a)

--name

b)

name__

c)

__name

d)

_name

54.

How can you define a variable visible to all instances?

a)

self.variable

b)

_variable

c)

ClassName.variable

d)

var = 0 in init

55.

Which function returns a readable string of an object?

a)

describe

b)

string

c)

str

d)

text

56.

How can child classes access superclass protected members?

a)

Through imports

b)

Using super ()

c)

Via private access

d)

With decorators

57.

Which function allows invocation of a superclass's method?

a)

parent.method()

b)

base ()

c)

super ()

d)

root.method()

58.

If a class inherits from more than one class, it's called:

a)

Interface merging

b)

Multilevel hierarchy

c)

Multiple inheritance

d)

Deep linking

59.

What does MRO help determine?

a)

Object creation speed

b)

Method call sequence

c)

Memory reference optimization

d)

Recursive class calls

60.

What issue arises from complex multiple inheritance?

a)

Shadowing

b)

Method overuse

c)

Diamond problem

d)

Recursive constructors

61.

Which method is used first when instantiating subclasses?

a)

parent's call

b)

init of child

c)

setup

d)

build()

62.

Is it possible to restrict multiple inheritance in Python?

a)

No, it's always allowed

b)

Only via metaclasses

c)

Yes, with private inheritance

d)

Yes, using single base class policy

63.

Changing behavior of a method in a child class is called:

a)

Hiding

b)

Overriding

c)

Inverting

d)

Swapping

64.

To enforce method implementation in subclasses, use:

a)

@enforce

b)

@abstractmethod

c)

@virtual

d)

override()

65.

Which decorator makes a method abstract inside a class?

a)

@abstractmethod

b)

@pure

c)

@virtualmethod

d)

@interface

66.

Can instances of abstract classes be created directly?

a)

Only with init override

b)

No

c)

Yes, if it has no methods

d)

Yes, with metaclass tricks

67.

What makes polymorphism useful?

a)

Reduces subclassing

b)

Enables uniform interfaces

c)

Limits method visibility

d)

Prevents overrides

68.

When different classes implement the same interface, it's called:

a)

Polymorphism

b)

Abstraction

c)

Composition

d)

Overloading

69.

What output will this code produce?

a)

A

b)

B

c)

Error

d)

Nothing

70.

In which case is an attribute considered private?

a)

data

b)

_temp

c)

__secret

d)

All of them

71.

Which method influences print(obj) output?

a)

__str__

b)

__format__

c)

__show__

d)

display()

72.

Interfaces in Python are implemented via:

a)

Abstract base classes

b)

Decorators

c)

Class templates

d)

Metaclasses only

73.

To avoid method replacement in child classes, use:

a)

@final (Python 3.8+)

b)

@readonly

c)

No such protection exists

d)

Use underscore prefixes

74.

What causes an error here?

a)

Works fine

b)

x is undefined due to missing super ()

c)

y hides x

d)

Class syntax is wrong

75.

Purpose of @staticmethod: 

a)

A)   Prevent inheritance 

b)

A)   Allow function without object reference 

c)

A)   Create shared state 

d)

A)   Track method calls 

76.

What does "duck typing" mean?

a)

Interface-based variable hints

b)

Behavior determines object compatibility

c)

Compiler-style typing

d)

Static interface checks

77.

Which principle is violated here?

class Account: 

def init(self): 

self.__balance = 100 

print(Account().__balance) 

a)

Polymorphism

b)

Abstraction

c)

Encapsulation

d)

Composition

78.

Which method runs upon object deletion?

a)

__exit__

b)

__destroy__

c)

__del__

d)

__remove__

79.

What will be printed?

a)

From object

b)

From class

c)

Nothing

d)

Error

80.

Which decorator allows access to the method from class and not just instances?

a)

@classmethod

b)

@staticmethod

c)

@abstractmethod

d)

@getmethod

81.

What is the main purpose of encapsulation in OOP?

a)

To make all class members publicly accessible

b)

To hide implementation details from outside access

c)

To prevent class inheritance

d)

To improve code execution speed

82.

Which symbol prefix indicates a private attribute in Python?

a)

No prefix

b)

Single underscore (_)

c)

Double underscore (__)

d)

Asterisk (*)

83.

What does the @property decorator do?

a)

Makes a method act like a class method

b)

Converts a method into a read-only attribute

c)

Prevents method overriding

d)

Marks a method as deprecated

84.

How do you define a setter for a property named 'price'?

a)

@price.setter

b)

@setter(price)

c)

@property.setter

d)

@set_price

85.

Which of these is a protected attribute in Python?

a)

self.name

b)

self._name

c)

self.__name

d)

self.name*

86.

Which magic method is called by the print() function?

a)

__repr__

b)

__str__

c)

__print__

d)

__display__

87.

What is the purpose of the __len__ method?

a)

To compare object sizes

b)

To return the length of an object

c)

To initialize object counters

d)

To enable string conversion

88.

Which magic method is called when using the + operator?

a)

__plus__

b)

__add__

c)

__sum__

d)

__concat__

89.

What does the __call__ method enable?

a)

Object comparison

b)

Object initialization

c)

Calling objects like functions

d)

Object serialization

90.

Which method implements the == comparison?

a)

__eq__

b)

__cmp__

c)

__same__

d)

__compare__

91.

What is the correct structure for exception handling?

a)

try -> catch -> finally

b)

try -> except -> finally

c)

attempt -> handle -> ensure

d)

begin -> rescue -> end

92.

Which block always executes, even if an exception occurs?

a)

try

b)

except

c)

finally

d)

else

93.

How do you create a custom exception class?

a)

A)   class MyError(Exception) 

b)

A)   def MyError() 

c)

A)   exception MyError: 

d)

A)   new Exception("MyError") 

94.

Which exception occurs when a key isn't found in a dictionary?

a)

ValueError

b)

KeyError

c)

IndexError

d)

AttributeError

95.

What does the raise keyword do?

a)

Catches an exception

b)

Creates a new exception type

c)

Triggers an exception manually

d)

Silences an exception

96.

Which file mode opens for writing without overwriting?

a)

"w"

b)

"r"

c)

"a"

d)

"x"

97.

What does json.loads() do?

a)

Converts JSON to Python object

b)

Converts Python object to JSON

c)

Reads JSON from a file

d)

Validates JSON syntax

98.

Which module handles binary serialization?

a)

json

b)

pickle

c)

yaml

d)

marshal

99.

What is the correct way to read all lines from a file?

a)

file.read()

b)

file.readlines()

c)

file.line()

d)

file.get_lines()

100.

What does serialization mean?

a)

Sorting data alphabetically

b)

Converting objects to storable formats

c)

Encrypting sensitive data

d)

Compressing file sizes

101.

What is the default metaclass in Python?

a)

object

b)

type

c)

class

d)

meta

102.

Which method is called first when creating an instance?

a)

__init__

b)

__new__

c)

__call__

d)

__create__

103.

What does type ('MyClass', (), {}) do?

a)

Checks variable type

b)

Creates a new class dynamically

c)

Converts values to strings

d)

Compares two classes

104.

When is __new__ executed?

a)

Before __init__

b)

After __init__

c)

Instead of __init__

d)

During class destruction

105.

What best describes a metaclass?

a)

A superclass for all exceptions

b)

A class that creates other classes

c)

A decorated method

d)

An abstract base class

106.

What does the Singleton pattern ensure?

a)

Only one instance exists

b)

Multiple interchangeable algorithms

c)

One-to-many notifications

d)

Dynamic behavior addition

107.

Which pattern delegates object creation to subclasses?

a)

Observer

b)

Factory Method

c)

Decorator

d)

Strategy

108.

In the Observer pattern, who receives updates?

a)

The main subject

b)

Registered observers

c)

Factory objects

d)

Decorator classes

109.

What does the Decorator pattern do?

a)

Adds responsibilities dynamically

b)

Ensures single instance

c)

Creates object families

d)

Encapsulates algorithms

110.

Which pattern selects algorithms at runtime?

a)

Singleton

b)

Strategy

c)

Observer

d)

Factory

111.

What is the purpose of getter methods?

a)

To modify private attributes directly

b)

To return attribute values safely

c)

To initialize class instances

d)

To delete object properties

112.

Which magic method provides an "official" string representation?

a)

__str__

b)

__repr__

c)

__print__

d)

__info__

113.

What happens if you don't handle an exception?

a)

Program continues silently

b)

Program terminates with error

c)

Exception is logged automatically

d)

Default values are substituted

114.

What is the main advantage of using JSON over pickle?

a)

Better performance

b)

Language independence

c)

Supports binary data

d)

Smaller file sizes

115.

Which pattern notifies dependents of state changes?

a)

Singleton

b)

Strategy

c)

Observer

d)

Factory

116.

What is the primary purpose of the Singleton pattern?

a)

Allow creation of multiple instances of a class

b)

Ensure only one instance of a class exists

c)

Define interchangeable algorithms

d)

Construct complex objects

117.

Which pattern allows dynamically adding new responsibilities to an object at runtime?

a)

Strategy

b)

Observer

c)

Decorator

d)

Factory Method

118.

The Factory Method pattern encapsulates object creation by using:

a)

A global function

b)

A static factory class

c)

A method that returns instances of various subclasses

d)

A class decorator

119.

In the Observer pattern, the subject notifies observers of state changes by invoking:

a)

Direct attribute access

b)

Observer callbacks

c)

Raising exceptions

d)

Polling in a loop

120.

The Strategy pattern is useful when you need to:

a)

Create singleton instances

b)

Select an algorithm at runtime

c)

Enforce a strict class hierarchy

d)

Track state changes of an object

121.

Which technique in Python is often used to implement the Singleton pattern?

a)

A metaclass that caches instances

b)

Overriding __call__ in each subclass

c)

Decorating methods with @staticmethod

d)

Declaring all methods as @classmethod

122.

What role does the metaclass abc.ABCMeta play?

a)

Enables dynamic code generation

b)

Forces subclasses to implement abstract methods

c)

Implements the Singleton pattern

d)

Manages thread synchronization

123.

To avoid subclass explosion when combining different behaviors, which pattern is suitable?

a)

Singleton

b)

Decorator

c)

Adapter

d)

Observer

124.

In the threading module, which method starts a new thread?

a)

run()

b)

start()

c)

init()

d)

spawn()

125.

Which lock allows the same thread to acquire it multiple times without blocking?

a)

Lock

b)

RLock

c)

Semaphore

d)

Event

126.

What does GIL stand for in Python?

a)

Global Interpreter Lock

b)

General Instance Level

c)

Global Instance Loader

d)

Generic Integration Layer

127.

To bypass the GIL and achieve true parallelism, you should use:

a)

threading

b)

asyncio

c)

multiprocessing

d)

concurrent.futures

128.

To protect shared data from race conditions, you should wrap access in:

a)

with open block

b)

with lock block

c)

try/except block

d)

for loop

129.

If two threads modify the same variable without synchronization, this leads to a:

a)

Deadlock

b)

Race condition

c)

Memory leak

d)

Type error

130.

Which call waits for a thread to complete?

a)

thread.join()

b)

thread.wait()

c)

thread.stop()

d)

thread.sync()

131.

On Windows, the default start method for multiprocessing is:

a)

fork

b)

spawn

c)

forkserver

d)

thread

132.

In Flask, if unspecified, a route accepts which HTTP method by default?

a)

GET

b)

POST

c)

PUT

d)

DELETE

133.

Which Django CBV class renders a list of model instances?

a)

DetailView

b)

ListView

c)

FormView

d)

TemplateView

134.

To create a new Django project, you run:

a)

django-admin startapp

b)

django-admin startproject

c)

python manage.py initproject

d)

flask new project

135.

In SQLAlchemy (Flask), sessions are created via:

a)

Session()

b)

sessionmaker()

c)

engine.connect()

d)

create_session()

136.

In which Django file are URL routes defined?

a)

models.py

b)

views.py

c)

urls.py

d)

settings.py

137.

In Flask, what is a Blueprint used for?

a)

Grouping related routes and templates

b)

Managing static files

c)

Configuring the database

d)

Running background tasks

138.

What is the default template engine in Django called?

a)

Jinja2

b)

Mustache

c)

Django Template Language

d)

Mako

139.

To create and apply migrations in Django, you run:

a)

makemigrations then migrate

b)

migrate then makemigrations

c)

db init

d)

syncdb

140.

Which UML diagram shows classes, their attributes, methods, and relationships?

a)

Use case diagram

b)

Activity diagram

c)

Class diagram

d)

Sequence diagram

141.

Which phase usually comes first in software development?

a)

Testing

b)

Requirements analysis

c)

Implementation

d)

Deployment

142.

Which Sphinx extension automatically documents Python modules?

a)

sphinx.ext.autodoc

b)

sphinx.ext.viewcode

c)

sphinx.ext.napoleon

d)

sphinx.ext.todo

143.

What does Continuous Integration (CI) primarily provide?

a)

Automated build and test

b)

Manual code review

c)

Real-time chat

d)

Deployment scripts only

144.

To inspect the Method Resolution Order (MRO) in Python, you can check:

a)

Class.__mro__

b)

class.mro()

c)

inspect.mro(class)

d)

All of the above

145.

Which function serializes a Python object to a JSON-formatted string?

a)

json.dumps()

b)

pickle.dumps()

c)

marshal.dumps()

d)

yaml.dump()

146.

What will be printed? class X: def run(self): print("X") class Y(X): def run(self): print("Y") super().run() class Z(X): def run(self): super().run() print("Z") class W(Y, Z): def run(self): super().run() print("W") w = W() w.run()

a)

Y X Z W

b)

X Y Z W

c)

Y Z X W

d)

X Z Y W

147.

What will be printed? class A: def __init__(self): print("A", end="") class B(A): def __init__(self): print("B", end="") super().__init__() class C(A): def __init__(self): super().__init__() print("C", end="") class D(B, C): def __init__(self): print("D", end="") super().__init__() d = D()

a)

DBAC

b)

DBCA

c)

DABC

d)

DCBA

148.

What will be printed? class A: x = "A" class B(A): x = "B" class C(A): pass class D(C, B): pass print(D.x)

a)

A

b)

B

c)

C

d)

Error

149.

What will be printed? class A: def __init__(self): self.value = 1 class B(A): def __init__(self): self.value = 2 super().__init__() b = B() print(b.value)

a)

1

b)

2

c)

3

d)

Error

150.

Which Django management command runs unit tests?

a)

manage.py runserver

b)

manage.py test

c)

manage.py migrate

d)

manage.py makemigrations

151.

Which tool can you use to check Python code style before an exam?

a)

pytest

b)

pylint

c)

unittest

d)

coverage