1Which keyword is used to define a class in Python?
A.def
B.object
C.class
D.struct
Correct Answer: class
Explanation:
The 'class' keyword is used to define a new class in Python.
Incorrect! Try again.
2What is an object in Python?
A.A built-in data type
B.An instance of a class
C.A reserved keyword
D.A function defined inside a class
Correct Answer: An instance of a class
Explanation:
An object is a specific instance of a class, containing the class's methods and properties.
Incorrect! Try again.
3Which method is automatically called when a new object of a class is created?
A.create
B.class
C.init
D.object
Correct Answer: init
Explanation:
The init method is the constructor in Python and is automatically invoked when a new object is instantiated.
Incorrect! Try again.
4What does the 'self' parameter represent in a class method?
A.The parent class
B.The instance of the class calling the method
C.Global variables
D.The current class definition
Correct Answer: The instance of the class calling the method
Explanation:
'self' represents the specific instance of the class that is calling the method, allowing access to the attributes and methods of that instance.
Incorrect! Try again.
5How do you access the attribute 'name' of an object 'obj'?
A.obj.name
B.obj::name
C.obj[name]
D.obj->name
Correct Answer: obj.name
Explanation:
In Python, the dot (.) operator is used to access attributes and methods of an object.
Incorrect! Try again.
6Which of the following correctly creates an instance of a class named 'Car'?
A.my_car = new Car()
B.my_car = Car
C.my_car = create Car()
D.my_car = Car()
Correct Answer: my_car = Car()
Explanation:
To create an instance, you call the class name followed by parentheses, similar to calling a function.
Incorrect! Try again.
7What is the output of the following code?
class A:
def init(self):
self.x = 1
a = A()
print(a.x)
A.None
B.0
C.1
D.Error
Correct Answer: 1
Explanation:
The constructor initializes the instance attribute 'x' to 1, which is then printed.
Incorrect! Try again.
8Which feature of OOP allows a class to derive properties from another class?
A.Polymorphism
B.Data Hiding
C.Encapsulation
D.Inheritance
Correct Answer: Inheritance
Explanation:
Inheritance allows a child class to acquire the properties and methods of a parent class.
Incorrect! Try again.
9What is the correct syntax to create a class 'Child' that inherits from 'Parent'?
A.class Child : Parent:
B.class Child extends Parent:
C.class Child inherits Parent:
D.class Child(Parent):
Correct Answer: class Child(Parent):
Explanation:
Python uses the syntax 'class Child(Parent):' to define inheritance.
Incorrect! Try again.
10What happens when a method in a child class has the same name as a method in the parent class?
A.Both methods are merged
B.A syntax error occurs
C.The parent method is called
D.The child method overrides the parent method
Correct Answer: The child method overrides the parent method
Explanation:
This is called Method Overriding. The version of the method defined in the child class takes precedence.
Incorrect! Try again.
11How do you achieve data hiding in Python to make an attribute private?
A.Declare it with the 'private' keyword
B.Suffix the name with double underscores
C.Prefix the name with double underscores (__)
D.Prefix the name with a single underscore (_)
Correct Answer: Prefix the name with double underscores (__)
Explanation:
Prefixing an attribute with double underscores (e.g., __var) triggers name mangling, making it harder to access from outside the class, effectively making it private.
Incorrect! Try again.
12Does Python support function overloading (multiple methods with the same name but different arguments) natively?
A.Yes, but only for constructors
B.No, the latest definition overwrites previous ones
C.Yes, exactly like Java or C++
D.No, it causes a syntax error immediately
Correct Answer: No, the latest definition overwrites previous ones
Explanation:
Python does not support traditional overloading. If you define two methods with the same name, the second one replaces the first. Overloading is usually simulated using default arguments or variable-length arguments.
Incorrect! Try again.
13Which function is used to determine if a class is a subclass of another class?
A.inherits()
B.issubclass()
C.isinstance()
D.typeof()
Correct Answer: issubclass()
Explanation:
The issubclass(class, classinfo) function returns True if 'class' is a subclass (direct, indirect, or virtual) of 'classinfo'.
Incorrect! Try again.
14What is the purpose of the 'super()' function?
A.To delete an object
B.To make a variable global
C.To initialize a class
D.To call the superclass (parent) methods
Correct Answer: To call the superclass (parent) methods
Explanation:
'super()' returns a temporary object of the superclass that allows you to call its methods, often used in the init method of a child class.
Incorrect! Try again.
15What is a class variable?
A.A private variable
B.A global variable defined outside the class
C.A variable defined inside a method using 'self'
D.A variable defined inside the class but outside any methods
Correct Answer: A variable defined inside the class but outside any methods
Explanation:
Class variables are shared by all instances of the class and are defined within the class construction but outside of any instance methods.
Incorrect! Try again.
16If you try to access a private variable 'secret' from outside the class directly using 'obj.secret', what happens?
A.It returns None
B.It raises an AttributeError
C.It returns the value
D.It raises a SyntaxError
Correct Answer: It raises an AttributeError
Explanation:
Due to name mangling, '__secret' is renamed to '_ClassNamesecret', so accessing it directly as 'secret' fails.
Incorrect! Try again.
17Which special method is known as the destructor?
A.end
B.del
C.destroy
D.init
Correct Answer: del
Explanation:
The del method is called when an object is about to be destroyed (garbage collected).
Incorrect! Try again.
18What is the output?
class Test:
def init(self):
print('Hello')
t = Test()
A.Hello
B.Test
C.Nothing
D.Error
Correct Answer: Hello
Explanation:
Creating an instance 't = Test()' calls the init method, which prints 'Hello'.
Incorrect! Try again.
19In Python, what is the result of 'isinstance(obj, ClassName)'?
A.Returns the memory address
B.Returns the class name
C.Returns True if obj is an instance of ClassName or its subclass
D.Creates a new instance
Correct Answer: Returns True if obj is an instance of ClassName or its subclass
Explanation:
isinstance() checks if an object belongs to a specific class or a class derived from it.
Incorrect! Try again.
20Which concept implies wrapping data and methods into a single unit?
A.Overloading
B.Inheritance
C.Polymorphism
D.Encapsulation
Correct Answer: Encapsulation
Explanation:
Encapsulation is the bundling of data (attributes) and the methods that operate on that data into a single unit (class).
Incorrect! Try again.
21How can you simulate function overloading in Python?
A.Importing the overloading module
B.Using default argument values or *args
C.Using the 'overload' keyword
D.Defining multiple functions with same name
Correct Answer: Using default argument values or *args
Explanation:
Since Python doesn't support static overloading, developers use default parameter values or variable-length argument lists (*args) to handle different call signatures.
Incorrect! Try again.
22What is Multiple Inheritance?
A.A class has multiple children
B.A class has multiple methods
C.A class inherits from multiple parents
D.A method has multiple arguments
Correct Answer: A class inherits from multiple parents
Explanation:
Multiple inheritance occurs when a child class is defined with more than one parent class, e.g., 'class Child(Parent1, Parent2):'.
Incorrect! Try again.
23Which dictionary contains the namespace of a class or object?
A.dict
B.map
C.vars
D.dir
Correct Answer: dict
Explanation:
The dict attribute stores the writable attributes (namespace) of a class or object as a dictionary.
Incorrect! Try again.
24What is the output?
class Dog:
legs = 4
d1 = Dog()
d2 = Dog()
d1.legs = 3
print(d1.legs, d2.legs)
A.4 4
B.3 4
C.4 3
D.3 3
Correct Answer: 3 4
Explanation:
When 'd1.legs = 3' executes, it creates an instance variable 'legs' for d1, shadowing the class variable. d2 still refers to the shared class variable which remains 4.
Incorrect! Try again.
25Consider 'class A: pass' and 'class B(A): pass'. What is B.bases?
A.None
B.(A,)
C.()
D.(object,)
Correct Answer: (A,)
Explanation:
The bases attribute returns a tuple containing the base classes (parents) of the class.
Incorrect! Try again.
26When overriding a method, how can you ensure the parent class's logic for that method is also executed?
A.It happens automatically
B.Copy paste the code
C.Use parent.method_name()
D.Use super().method_name()
Correct Answer: Use super().method_name()
Explanation:
You must explicitly call the parent method using super() or ClassName.method(self) if you want the parent's logic to run alongside the child's override.
Incorrect! Try again.
27What is the built-in string representation method meant for developers/debugging?
A.print
B.string
C.repr
D.str
Correct Answer: repr
Explanation:
repr is intended to return an unambiguous string representation of the object, primarily for debugging/developers, whereas str is for end-users.
Incorrect! Try again.
28If a class does not define an init method, what happens?
A.Syntax Error
B.It cannot be instantiated
C.The program crashes
D.Python provides a default constructor
Correct Answer: Python provides a default constructor
Explanation:
Classes do not require an init method. If omitted, Python uses a default one that doesn't perform any specific property initialization.
Incorrect! Try again.
29What implies that a variable or method is 'protected' (convention only) in Python?
A.Single underscore prefix (_)
B.No prefix
C.Double underscore prefix (__)
D.The keyword 'protected'
Correct Answer: Single underscore prefix (_)
Explanation:
A single underscore prefix is a convention indicating that the member is intended for internal use (protected), though it is not enforced by the interpreter.
Incorrect! Try again.
30Can you add attributes to an instance object after it has been created?
A.No, only inside init
B.Only if the class is empty
C.Only using setter methods
D.Yes, anytime
Correct Answer: Yes, anytime
Explanation:
Python is dynamic; you can add new attributes to an instance object at any point using dot notation (e.g., obj.new_attr = 5).
Incorrect! Try again.
31What is MRO in the context of Python inheritance?
A.Method Resolution Order
B.Memory Read Operation
C.Method Return Object
D.Main Runtime Object
Correct Answer: Method Resolution Order
Explanation:
MRO determines the order in which Python looks for a method in a hierarchy of classes, especially in multiple inheritance.
Incorrect! Try again.
32Given: 'class A: pass'. What is type(A)?
A.<class 'type'>
B.<class 'class'>
C.<class 'object'>
D.String
Correct Answer: <class 'type'>
Explanation:
In Python, classes themselves are objects, and the type of a class definition is 'type'.
Incorrect! Try again.
33What is the output?
class P:
def show(self): print('P')
class C(P):
def show(self): print('C')
c = C()
c.show()
A.P
B.Error
C.C
D.PC
Correct Answer: C
Explanation:
Class C overrides the 'show' method of Class P. Therefore, calling show() on an instance of C prints 'C'.
Incorrect! Try again.
34How do you access a private variable '__hidden' of class 'Test' from outside via name mangling?
A.Test._Test__hidden
B.instance._Test__hidden
C.Test.__hidden
D.instance.hidden
Correct Answer: instance._Test__hidden
Explanation:
Python mangles the name to '_ClassName__variableName'. It can be accessed this way, though it is bad practice.
Incorrect! Try again.
35Which parameter is not required when calling an instance method, even though it is defined in the method signature?
A.The first argument (self)
B.Keyword arguments
C.None
D.The last argument
Correct Answer: The first argument (self)
Explanation:
When calling an instance method (e.g., obj.method()), Python automatically passes the object 'obj' as the first argument ('self'). You do not pass it manually.
Incorrect! Try again.
36What is the output?
class X:
def m(self, a=None):
if a: print(a)
else: print('Empty')
x = X()
x.m()
x.m(5)
A.Empty then 5
B.Empty then Empty
C.Error
D.5 then Empty
Correct Answer: Empty then 5
Explanation:
This demonstrates simulating overloading. The first call uses the default None (prints 'Empty'), the second passes 5 (prints 5).
Incorrect! Try again.
37Which of the following is FALSE about class attributes?
A.They are defined outside init
B.Changing them via an instance changes the class variable for everyone
C.They can be accessed via the Class name
D.They are shared by all instances
Correct Answer: Changing them via an instance changes the class variable for everyone
Explanation:
If you assign a value to a class attribute via an instance (obj.class_var = x), it creates a new instance variable hiding the class variable for that specific object only. It does not change it for others.
Incorrect! Try again.
38In class MyClass(object):, what is 'object'?
A.A module
B.A variable
C.A keyword for instantiation
D.The base class for all classes in Python 3
Correct Answer: The base class for all classes in Python 3
Explanation:
In Python 3, all classes implicitly inherit from 'object'. Explicitly writing it is valid but optional.
Incorrect! Try again.
39What keyword is used to delete a reference to an object?
A.delete
B.free
C.del
D.remove
Correct Answer: del
Explanation:
The 'del' statement is used to delete references to objects.
Incorrect! Try again.
40What defines the documentation string (docstring) of a class?
A.The help() method
B.Comments starting with #
C.The doc assignment
D.The first string literal inside the class definition
Correct Answer: The first string literal inside the class definition
Explanation:
The first string literal appearing directly after the class definition header is assigned to the doc attribute.
Incorrect! Try again.
41What is the output?
class A: count = 0
print(A.count)
A.AttributeError
B.None
C.False
D.0
Correct Answer: AttributeError
Explanation:
The attribute __count is private (data hiding). It cannot be accessed directly using the class name without name mangling.
Incorrect! Try again.
42Polymorphism in Python is best demonstrated by:
A.Using strict type declaration
B.Hiding data
C.Same method name performing different tasks based on the object
D.Creating multiple classes
Correct Answer: Same method name performing different tasks based on the object
Explanation:
Polymorphism allows different classes to be treated as instances of the same general category, usually via methods with the same name behaving differently.
Incorrect! Try again.
43If Class B inherits from Class A, which class is the Superclass?
A.Both
B.Class A
C.Neither
D.Class B
Correct Answer: Class A
Explanation:
The parent class (the one being inherited from) is the superclass.
Incorrect! Try again.
44What happens if you define def __init__(self, a): and def __init__(self, a, b): in the same class?
A.Both constructors are available
B.Syntax Error
C.Only the second one is available
D.Only the first one is available
Correct Answer: Only the second one is available
Explanation:
Python does not support method overloading. The second definition overwrites the first one completely.
Incorrect! Try again.
45Which built-in function returns the attributes and methods of an object?
A.list()
B.info()
C.dir()
D.help()
Correct Answer: dir()
Explanation:
dir(obj) tries to return a list of valid attributes and methods for the object.
Incorrect! Try again.
46Can a class call a method defined in its own class?
A.Only static methods
B.No, never
C.Yes, using method_name() directly
D.Yes, using self.method_name()
Correct Answer: Yes, using self.method_name()
Explanation:
Inside a class method, you can call other methods of the same class using the 'self' reference (e.g., self.other_method()).
Incorrect! Try again.
47What is the default return value of init?
A.The object created
B.True
C.It cannot return anything
D.None
Correct Answer: None
Explanation:
The init method is required to return None. Attempting to return a value creates a TypeError.
Incorrect! Try again.
48Which of the following represents an 'Is-A' relationship?
A.Inheritance
B.Composition
C.Aggregation
D.Association
Correct Answer: Inheritance
Explanation:
Inheritance represents an Is-A relationship (e.g., A Dog Is-A Animal).
Incorrect! Try again.
49When using Data Hiding, the data is:
A.Protected from accidental access
B.Deleted
C.Completely inaccessible
D.Encrypted in memory
Correct Answer: Protected from accidental access
Explanation:
Data hiding in Python protects members from accidental access or modification, but they can still be accessed intentionally (e.g., via name mangling).
Incorrect! Try again.
50What is the output?
class A:
def f(self): return 1
class B(A):
def f(self): return 2
obj = B()
print(obj.f())
A.2
B.12
C.1
D.None
Correct Answer: 2
Explanation:
Class B overrides method f() of Class A. The instance is of Class B, so it returns 2.
Incorrect! Try again.
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill.
The rest comes out of a student's own pocket: the domain, the storage,
and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason.
to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it.
What it pays for →