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