WorksheetsMidtermExam-OOP-MakScie
Total questions: 93
Worksheet time: 2hrs 33mins
What is the main purpose of encapsulation in Python OOP?
To increase execution speed
To hide implementation details and protect data
To allow multiple inheritance
To avoid using classes
In Python, how do you define a private variable inside a class?
By prefixing with #
By prefixing with _ (single underscore)
By prefixing with __ (double underscore)
By declaring it as private
Which of the following demonstrates encapsulation properly?
self.__name is accessible only inside the class
self.__name can be modified directly
self.__name is a class method
Encapsulation is not applied here
Which method is typically used to access a private variable?
Global variable
Getter method
Direct assignment
Operator overloading
Which of the following is NOT an advantage of encapsulation?
Data protection
Code reusability
Data hiding
Preventing inheritance
Which principle does encapsulation directly support?
Polymorphism
Abstraction
Overloading
Composition
What is true about public variables in Python?
Can only be accessed inside class
Accessible anywhere
Automatically private
Not allowed in classes
Which keyword is used in Python to enforce encapsulation strictly?
private
protected
No strict keyword exists
secure
What happens if you try to access a private variable directly?
Error
Value is shown normally
Python performs name mangling
It becomes read-only
Which of these is a good reason to use encapsulation?
Faster loops
Cleaner UI
Prevent unauthorized access
Debugging only
Which is NOT part of encapsulation?
Getters
Setters
Encapsulation allows:
Multiple return values
Secure modification of variables
Faster compilation
Dynamic typing
What is the recommended way to modify a private variable?
Access directly with object.__var
Use a setter method
Use reflection
No way exists
Which principle is violated if encapsulation is broken?
DRY principle
Information hiding
Loosely coupled design
Polymorphism
In Python, which keyword is used to define inheritance?
extends
implements
inherit
class ParentClass
What type of inheritance is this?
Multiple inheritance
Single inheritance
Multilevel inheritance
Hybrid inheritance
If class C inherits from both A and B, what is this called?
Multiple inheritance
Single inheritance
Hybrid inheritance
Hierarchical inheritance
Which Python feature helps to resolve diamond problem in inheritance?
super()
Polymorphism
Decorators
Abstract classes
Which function checks if a class is a subclass of another?
isinstance()
issubclass()
mro()
id()
Which type of inheritance is shown?
Single
Multilevel
Multiple
Hybrid
Which inheritance type is a combination of two or more?
Multilevel
Multiple
Hybrid
Single
Which keyword is used to call a parent class constructor?
parent()
base()
super()
root()
What happens if both parent and child have the same method name?
Error
Child method overrides parent method
Parent method overrides child method
Both methods run together
Which of the following supports method overriding?
Encapsulation
Inheritance
Polymorphism
Abstraction
What does the super() function return?
Parent class object
Child class object
Global object
None
Which inheritance structure is generally discouraged?
Single
Multiple with diamond shape
Multilevel
Hybrid
Which operator is used for floor division in Python?
/
//
%
**
What will be the output of: x, y = 5, 2 print(x ** y)
2.5
25
7
10
Which operator is used for string repetition?
*
+
,
**
Which operator checks identity in Python?
==
is
!=
in
What will be output? x = [1,2,3] y = x print(x is y)
False
True
Error
None
Which operator is overloaded by __add__() method?
+
-
*
/
Which operator is used for membership testing?
in
is
==
!=
Which operator gives remainder?
/
%
//
**
Which operator is overloaded by __str__() method?
print()
String conversion
Concatenation
Multiplication
Which of the following is NOT a Python control structure?
if-else
for loop
switch-case
while loop
What is the output of: for i in range(2, 6, 2): print(i, end=" ")
A. 2 3 4 5
B. 2 4
C. 2 6
D. Error
Which keyword is used to exit a loop immediately?
continue
break
pass
exit
Which keyword is used to skip the current iteration in a loop?
continue
break
skip
pass
What will be the output?
The code will print '0 1 2'
The code will print '0 0 0 ...'
The code will result in an error
The code will print nothing
x += 1
0 1
0 1 2
1 2 3
Infinite loop
Which of the following is valid Python code?
for(i=0;j<5;i++)
for i in range(5):
loop i=0 to 5
foreach i in range(5)
Which loop runs at least once in Python?
while
do-while
for
None
What is the purpose of the else in a loop?
Runs if loop breaks
Runs if loop completes normally without break
Runs before loop starts
Cannot be used in Python
What will this code print? for i in range(3): pass print("Done")
Done
0 1 2 Done
Nothing
Error
Which keyword is used for an empty block?
empty
none
pass
skip
Which loop is more suitable for iterating through lists?
for
while
do-while
switch
Which statement stops execution of the current function?
break
continue
return
pass
Which construct can replace multiple if-elif chains more efficiently?
while
dictionary mapping
pass
recursion
Which loop is used when the number of iterations is unknown?
for
while
foreach
switch
Which of the following is public in Python by default?
Variables without underscore
Variables with one underscore
What does a single underscore (_var) indicate?
Strong private
Weak private (internal use)
Protected and cannot be accessed
Global variable
Which is stronger in Python?
_var
__var
Both are same
None
Which variable type can still be accessed with name mangling?
Public
Protected
Private (__var)
Constant
What is the best practice to access protected members?
Directly outside class
Through child classes
Using global variable
Not possible
What happens if you declare a variable as _x?
Cannot be accessed outside
Still accessible but discouraged
Becomes private
Raises error
Which member has the highest restriction?
Public
Protected
Private
Which of these is a valid Python convention?
varPublic → public
_var → protected
__var → private
All of the above
Which variables are accessible everywhere?
Private
Protected
Public
Encapsulated
Access modifiers in Python are enforced by:
Compiler
Interpreter rules
Naming convention and name mangling
Operating system
Which Python decorator is used for getters?
@staticmethod
@getter
@property
@classmethod
How do you define a setter in Python?
@setter directly
@
Assigning variable directly
Using global keyword
self.__x = 10 @property def x(self): return self.__x a = A() print(a.x)
10
Error
None
__x
Which of these allows attribute validation before setting?
Getter
Setter
Constructor
Destructor
What is true about getters and setters?
They replace encapsulation
They implement encapsulation
They remove need for private variables
They prevent constructors
Which decorator is used to delete a property?
@deleter
@property.deleter
@del
@remove
Given the following code: self.__age = 20 @property def age(self): return self.__age @age.setter def age(self, value): if value > 0: self.__age = value a = A() a.age = 25 print(a.age)
A. 20
B. 25
C. Error
D. None
Which of these is an advantage of @property?
Simpler syntax than getter methods
Faster execution
Supports operator overloading
Avoids constructors
Which function allows checking if an attribute exists?
hasattr()
getattr()
setattr()
delattr()
Which diagram is used to represent class relationships in OOP?
Sequence diagram
Flowchart
Class diagram
State diagram
In UML, a filled diamond represents:
Association
Aggregation
Composition
Inheritance
Which symbol shows inheritance in a class diagram?
Solid arrow with closed head
Dotted arrow
Open arrow
Triangle arrow
Which of these represents "uses-a" relationship?
Association
Aggregation
Composition
Generalization
Which of these represents "is-a" relationship?
Aggregation
Inheritance
Composition
Dependency
Which method is the constructor in Python?
__init__()
__main__()
What is the output?
No output
Error
Constructor called
None
How many constructors can a Python class have?
Only one __init__ method
Multiple with different signatures
None
Unlimited
What is a default constructor?
Constructor with no parameters
Constructor with parameters
Abstract constructor
Constructor from parent class
What happens if a class has no constructor?
Error
Python creates a default one
No object can be created
Destructor is used
Which keyword is used inside constructor to refer to object?
this
self
object
What will be the output?
0 20
10 20
None 20
Error
What will happen if parent and child both have constructors?
Both run automatically
Only parent runs
Only child runs unless super() is used
Error
Which of the following is used for multiple ways of constructing objects?
Constructor chaining
Overloading
Default constructor
Encapsulation
Which method is called before __init__()?
__new__()
__start__()
__create__()
__build__()
Does Python support traditional method overloading?
Yes, fully
No, only latest defined method is used
Yes, using @overload decorator only
Yes, with multiple constructors
What is the output?
Error
Hello Alice
Hello Hello Alice
None
Which of these allows pseudo-overloading?
Method overriding
Default arguments
Decorators only
Abstract methods
Which operator is overloaded by __eq__()?
==
=
!=
Which operator is overloaded by __lt__()?
<=
<
>
>=
Which operator is overloaded by __mul__()?
*
**
//
%
Which method is called when str(obj) is executed?
__repr__()
__str__()
__doc__()
__name__()
Which method is called when len(obj) is executed?
__count__()
__size__()
__len__()
__iter__()
Which method is called for obj1 + obj2?
__sum__()
__add__()
__plus__()
__concat__()
