Welcome to the Python Particular Strategies MCQs! Particular strategies, often known as “magic” or “dunder” strategies, are prefixed and suffixed with double underscores in Python. These strategies present performance to courses that allow them to emulate built-in varieties or operations. These questions will take a look at your understanding of assorted particular strategies in Python, together with strategies for object illustration, arithmetic operations, comparability, and extra. Every query is multiple-choice, with just one right reply. Take your time to rigorously learn every query and select the most suitable choice. Let’s discover the world of Python particular strategies collectively!
30+ MCQs on Python Particular Strategies
Q1. What do double underscore (__) prefixed strategies in Python signify?
a) They’re reserved for inner use and shouldn’t be known as straight.
b) They’re particular strategies generally known as “dunder” strategies.
c) They’re known as magic strategies and deal with particular operations.
d) The entire above
Reply: d
Clarification: Double underscore (__) prefixed strategies are particular strategies in Python generally known as “dunder” strategies or magic strategies. They’re reserved for inner use and deal with particular operations.
Q2. What’s the objective of the __init__
methodology in a Python class?
a) To initialize the category object with default attributes.
b) To create a brand new occasion of the category.
c) To outline the constructor of the category.
d) To destroy the category object.
Reply: c
Clarification: The __init__
methodology is the constructor in Python courses. It’s used to initialize the item’s attributes when a brand new occasion of the category is created.
Q3. Which dunder methodology is used to implement the conduct of the +
operator in Python?
a) __add__
b) __plus__
c) __sum__
d) __concat__
Reply: a
Clarification: The __add__
methodology is used to outline the conduct of the +
operator in Python courses.
This autumn. Which of the next strategies is used to symbolize an object as a string in Python?
a) __repr__
b) __str__
c) __display__
d) __format__
Reply: b
Clarification: The __str__
methodology is used to outline how an object must be represented as a string when utilizing str(object)
or print(object)
.
Q5. What does the __len__
methodology do in Python?
a) It returns the size of the item.
b) It defines the conduct of the size operator (len()
).
c) It checks if an object is empty.
d) It returns the variety of parts within the object.
Reply: a
Clarification: The __len__
methodology is used to outline the conduct of the size operator (len()
) for customized objects, returning the size of the item.
Q6. Which dunder methodology is used to implement the conduct of the *
operator in Python?
a) __mult__
b) __mul__
c) __product__
d) __times__
Reply: b
Clarification: The __mul__
methodology is used to outline the conduct of the *
operator in Python courses.
Q7. What’s the objective of the __getitem__
methodology in Python?
a) It’s used to get the worth of an merchandise from a dictionary.
b) It’s used to outline the conduct for accessing gadgets utilizing sq. brackets ([]
).
c) It’s used to retrieve an merchandise from a listing.
d) It’s used to get the merchandise at a particular index in a sequence.
Reply: b
Clarification: The __getitem__
methodology is used to outline the conduct for accessing gadgets utilizing sq. brackets ([]
) for customized objects.
Q8. Which dunder methodology is used to implement the conduct of the in
operator in Python?
a) __exists__
b) __contains__
c) __includes__
d) __in__
Reply: b
Clarification: The __contains__
methodology is used to outline the conduct of the in
operator in Python courses.
Q9. What’s the objective of the __setattr__
methodology in Python?
a) It’s used to set the worth of an attribute.
b) It’s used to outline the conduct of attribute project.
c) It’s used to test if an attribute exists.
d) It’s used to delete an attribute.
Reply: b
Clarification: The __setattr__
methodology is used to outline the conduct when an attribute is ready in a Python class.
Q10. Which dunder methodology is used to implement the conduct of the -
operator in Python?
a) __sub__
b) __minus__
c) __subtract__
d) __difference__
Reply: a
Clarification: The __sub__
methodology is used to outline the conduct of the -
operator in Python courses.
Q11. What does the __iter__
methodology do in Python?
a) It returns an iterator object for the category.
b) It checks if an object is iterable.
c) It defines the conduct of the for
loop for the category.
d) It returns the subsequent merchandise within the iteration.
Reply: a
Clarification: The __iter__
methodology is used to return an iterator object for the category, permitting it for use in a for
loop.
Q12. Which dunder methodology is used to implement the conduct of the //
operator in Python?
a) __floor_div__
b) __floordiv__
c) __divide__
d) __intdiv__
Reply: b
Clarification: The __floordiv__
methodology is used to outline the conduct of the //
operator in Python courses.
Q13. What’s the objective of the __contains__
methodology in Python?
a) It checks if a component is contained in an object.
b) It checks if an object accommodates one other object.
c) It checks if an attribute is contained in a category.
d) It checks if a component is contained in a sequence.
Reply: d
Clarification: The __contains__
methodology is used to outline the conduct of the in
operator for customized objects, checking if a component is contained in a sequence.
Q14. Which dunder methodology is used to implement the conduct of the ==
operator in Python?
a) __equal__
b) __eq__
c) __equals__
d) __is__
Reply: b
Clarification: The __eq__
methodology is used to outline the conduct of the ==
operator in Python courses.
Q15. What does the __add__
methodology do in Python?
a) It provides two objects collectively.
b) It concatenates two objects.
c) It performs element-wise addition for 2 objects.
d) It performs matrix multiplication for 2 objects.
Reply: a
Clarification: The __add__
methodology is used to outline the conduct of the +
operator for customized objects, permitting them to be added collectively.
Q16. Which dunder methodology is used to implement the conduct of the []
operator for setting values in Python?
a) __setitem__
b) __set__
c) __assign__
d) __update__
Reply: a
Clarification: The __setitem__
methodology is used to outline the conduct of the []
operator for setting values in Python courses.
Q17. What’s the objective of the __delattr__
methodology in Python?
a) It’s used to delete an attribute from an object.
b) It’s used to outline the conduct when an attribute is deleted.
c) It’s used to test if an attribute will be deleted.
d) It’s used to return the worth of a deleted attribute.
Reply: a
Clarification: The __delattr__
methodology is used to outline the conduct when an attribute is deleted from a Python object.
Q18. What does the __next__
methodology do in Python?
a) It returns the subsequent aspect in an iteration.
b) It checks if there are extra parts in an iteration.
c) It defines the conduct of the subsequent()
perform.
d) It raises a StopIteration exception.
Reply: a
Clarification: The __next__
methodology is used to outline the conduct of retrieving the subsequent aspect in an iteration.
Q19. Which dunder methodology is used to implement the conduct of the **
operator in Python?
a) __exp__
b) __pow__
c) __power__
d) __exponent__
Reply: b
Clarification: The __pow__
methodology is used to outline the conduct of the **
operator for exponentiation in Python courses.
Q24. Which dunder methodology is used to implement the conduct of the !=
operator in Python?
a) __not_equal__
b) __ne__
c) __noteq__
d) __not_equals__
Reply: b
Clarification: The __ne__
methodology is used to outline the conduct of the !=
operator in Python courses.
Q25. What’s the objective of the __call__
methodology in Python?
a) It calls one other methodology within the class.
b) It defines the conduct of calling an occasion of the category as a perform.
c) It checks if the item is callable.
d) It returns the item’s sort.
Reply: b
Clarification: The __call__
methodology is used to outline the conduct of calling an occasion of the category as if it had been a perform.
Q26. Which dunder methodology is used to implement the conduct of the -=
operator in Python?
a) __decr__
b) __minus_equal__
c) __isub__
d) __sub_assign__
Reply: c
Clarification: The __isub__
methodology is used to outline the conduct of the -=
operator in Python courses.
Q27. Which dunder methodology is used to implement the conduct of the +=
operator in Python?
a) __increase__
b) __add_assign__
c) __iadd__
d) __plus_equal__
Reply: c
Clarification: The __iadd__
methodology is used to outline the conduct of the +=
operator in Python courses.
Q28. Which dunder methodology is used to implement the conduct of the >
operator in Python?
a) __gt__
b) __greater__
c) __greater_than__
d) __isgreater__
Reply: a
Clarification: The __gt__
methodology is used to outline the conduct of the >
operator in Python courses.
Q29. Which dunder methodology is used to implement the conduct of the <=
operator in Python?
a) __le__
b) __less__
c) __lessthan__
d) __islessthan__
Reply: a
Clarification: The __le__
methodology is used to outline the conduct of the <=
operator in Python courses.
Q30. Which dunder methodology is used to implement the conduct of the >=
operator in Python?
a) __ge__
b) __greater__
c) __greaterthan__
d) __isgreater__
Reply: a
Clarification: The __ge__
methodology is used to outline the conduct of the >=
operator in Python courses.
Q31. Which dunder methodology is used to implement the conduct of the <
operator in Python?
a) __lt__
b) __lesser__
c) __lessthan__
d) __islessthan__
Reply: a
Clarification: The __lt__
methodology is used to outline the conduct of the <
operator in Python courses.
Q32. Which particular methodology is used to overload the addition operator +
in Python?
class MyClass:
def __init__(self, worth):
self.worth = worth
def __add__(self, different):
return MyClass(self.worth + different.worth)
def __str__(self):
return f"MyClass object with worth: {self.worth}"
obj1 = MyClass(5)
obj2 = MyClass(10)
end result = obj1 + obj2
print(end result)
a) __add__
b) __plus__
c) __sum__
d) __concat__
Reply: a
Clarification: The __add__
methodology overloads the addition operator +
in Python courses.
Q33. Which particular methodology is used to overload the division operator /
in Python for division?
class Fraction:
def __init__(self, numerator, denominator):
self.numerator = numerator
self.denominator = denominator
def __truediv__(self, different):
return Fraction(self.numerator * different.denominator, self.denominator * different.numerator)
def __str__(self):
return f"{self.numerator}/{self.denominator}"
f1 = Fraction(1, 2)
f2 = Fraction(3, 4)
end result = f1 / f2
print(end result)
a) __divide__
b) __truediv__
c) __fraction__
d) __div__
Reply: b
Clarification: The __truediv__
methodology overloads the division operator /
in Python courses.
Q34. Which particular methodology is used to overload the lower than operator <
in Python?
class Level:
def __init__(self, x, y):
self.x = x
self.y = y
def __lt__(self, different):
return self.x < different.x and self.y < different.y
def __str__(self):
return f"Level({self.x}, {self.y})"
point1 = Level(5, 10)
point2 = Level(8, 12)
end result = point1 < point2
print(end result)
a) __less__
b) __lt__
c) __lessthan__
d) __islessthan__
Reply: b
Clarification: The __lt__
methodology overloads the lower than operator <
in Python courses.
Congratulations on finishing the Python Particular Strategies MCQs! Particular strategies in Python present a robust approach to customise the conduct of objects and courses, making them extra versatile and expressive. By mastering these particular strategies, you acquire the flexibility to outline how objects work together with operators, implement customized representations, deal with comparisons, and extra. Maintain working towards and experimenting with Python’s particular strategies to turn out to be proficient in constructing versatile and highly effective Python courses. When you have any questions or need to delve deeper into any subject, don’t hesitate to proceed your studying journey. Pleased coding!
You can even enroll in our free Python Course As we speak!
Learn our extra articles associated to MCQs in Python: