1Which keyword is used to define a class in Python?
A.def
B.class
C.struct
D.object
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 function defined inside a class
B.An instance of a class
C.A reserved keyword
D.A built-in data type
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.class
B.object
C.init
D.create
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 current class definition
B.The instance of the class calling the method
C.The parent class
D.Global variables
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 = Car()
B.my_car = new 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.
B.1
C.None
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.Encapsulation
C.Inheritance
D.Data Hiding
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 inherits Parent:
B.class Child(Parent):
C.class Child extends 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.The parent method is called
B.The child method overrides the parent method
C.A syntax error occurs
D.Both methods are merged
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.Prefix the name with a single underscore (_)
C.Prefix the name with double underscores (__)
D.Suffix the name with double underscores
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, exactly like Java or C++
B.No, the latest definition overwrites previous ones
C.Yes, but only for constructors
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.isinstance()
B.typeof()
C.issubclass()
D.inherits()
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 call the superclass (parent) methods
B.To make a variable global
C.To initialize a class
D.To delete an object
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 variable defined inside a method using 'self'
B.A variable defined inside the class but outside any methods
C.A global variable defined outside the class
D.A private variable
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 the value
B.It returns None
C.It raises an AttributeError
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.init
B.del
C.destroy
D.end
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.Nothing
B.Hello
C.Test
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 class name
B.Returns True if obj is an instance of ClassName or its subclass
C.Creates a new instance
D.Returns the memory address
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.Inheritance
B.Polymorphism
C.Encapsulation
D.Overloading
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.Defining multiple functions with same name
B.Using default argument values or *args
C.Using the 'overload' keyword
D.Importing the overloading module
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 inherits from multiple parents
B.A class has multiple children
C.A class has multiple methods
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 3
C.3 4
D.4 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.(A,)
B.(object,)
C.()
D.None
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 super().method_name()
D.Use parent.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.str
B.repr
C.string
D.print
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.Python provides a default constructor
D.The program crashes
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.Double underscore prefix (__)
B.Single underscore prefix (_)
C.No 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.Yes, anytime
B.No, only inside init
C.Only if the class is empty
D.Only using setter methods
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 Return Object
B.Method Resolution Order
C.Main Runtime Object
D.Memory Read Operation
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 'object'>
B.<class 'type'>
C.<class 'class'>
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.C
C.PC
D.Error
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.__hidden
B.Test._Test__hidden
C.instance._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 last argument
B.The first argument (self)
C.Keyword arguments
D.None
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.Error
C.5 then Empty
D.Empty 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 shared by all instances
B.They can be accessed via the Class name
C.Changing them via an instance changes the class variable for everyone
D.They are defined outside init
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 variable
B.The base class for all classes in Python 3
C.A keyword for instantiation
D.A module
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.remove
B.delete
C.del
D.free
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 first string literal inside the class definition
B.The doc assignment
C.Comments starting with #
D.The help() method
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.
B.None
C.AttributeError
D.False
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.Same method name performing different tasks based on the object
C.Hiding data
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.Class A
B.Class B
C.Both
D.Neither
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.Only the first one is available
C.Only the second one is available
D.Syntax Error
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.dir()
B.list()
C.help()
D.info()
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.Yes, using self.method_name()
B.No, never
C.Yes, using method_name() directly
D.Only static methods
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.None
D.It cannot return anything
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.Composition
B.Inheritance
C.Association
D.Aggregation
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.Encrypted in memory
B.Completely inaccessible
C.Protected from accidental access
D.Deleted
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.1
B.2
C.12
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.
Give Feedback
Help us improve by sharing your thoughts or reporting issues.