Question 1
In Python, what is the purpose of a metaclass?
To create class instances
To define class-level attributes
To define the behavior of classes
Metaclasses are not supported in Python
Question 2
What is the purpose of the __str__ and __repr__ methods in a Python class?
__str__ is used for creating string representations of instances, while __repr__ is used for debugging.
__str__ is used for debugging, while __repr__ is used for creating string representations of instances.
Both __str__ and __repr__ serve the same purpose.
__str__ and __repr__ are not valid methods in Python.
Question 3
How can you implement operator overloading for arithmetic operations (+, -, *, /) in a Python class?
By using the @overload decorator.
By defining methods like __add__, __sub__, __mul__, and __truediv__ in the class.
By using the operator module.
Operator overloading for arithmetic operations is not allowed in Python.
Question 4
Explain the purpose of the __call__ method in a class and provide an example use case.
__call__ is used to create a new instance of a class.
__call__ defines a static method in a class.
__call__ allows instances of the class to be called as functions.
__call__ is not a valid method in Python.
Question 5
Consider the following Python code:
class A:
def __init__(self, x):
self.x = x
class B(A):
def __init__(self, x, y):
super().__init__(x)
self.y = y
What is the relationship between the A and B classes?
Composition
Inheritance
Encapsulation
Polymorphism
Question 6
How can you create a custom exception class in Python?
By inheriting from the Exception class
By using the @exception decorator
Custom exceptions must inherit from Exception class.
By creating a class with any name
Question 7
In Python, what is the purpose of the __iter__ method in a class?
To create a new instance of a class
To access the attributes of an instance
To define a static method
To make instances of a class iterable in a loop
Question 8
How can you access a class-level attribute in Python?
Using the self keyword
Using the attribute keyword
By declaring it as a parameter
Directly using the class name
Question 9
Consider the following Python class:
class Book:
def __init__(self, title):
self.title = title
def __str__(self):
return self.title
How can you get the string representation of a Book instance?
str(Book)
Book.__str__()
book = Book("Python"); book.__str__()
book = Book("Python"); str(book)
Question 10
Consider the following Python code:
class MyClass:
pass
obj = MyClass()
What is the data type of obj?
object
MyClass
class
instance
There are 19 questions to complete.