30+ MCQs on Python Particular Strategies(Dunder Strategies)

0
32


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 sorts or operations. These questions will check your understanding of varied 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 best choice. Let’s discover the world of Python particular strategies collectively!

Python Special Methods

30+ MCQs on Python Particular Strategies

Q1. What do double underscore (__) prefixed strategies in Python signify?

a) They’re reserved for inside 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

Rationalization: Double underscore (__) prefixed strategies are particular strategies in Python generally known as “dunder” strategies or magic strategies. They’re reserved for inside use and deal with particular operations.

Q2. What’s the function of the __init__ technique 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

Rationalization: The __init__ technique 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 technique is used to implement the habits of the + operator in Python?

a) __add__

b) __plus__

c) __sum__

d) __concat__

Reply: a

Rationalization: The __add__ technique is used to outline the habits 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

Rationalization: The __str__ technique 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__ technique do in Python?

a) It returns the size of the item.

b) It defines the habits 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

Rationalization: The __len__ technique is used to outline the habits of the size operator (len()) for customized objects, returning the size of the item.

Q6. Which dunder technique is used to implement the habits of the * operator in Python?

a) __mult__

b) __mul__

c) __product__

d) __times__

Reply: b

Rationalization: The __mul__ technique is used to outline the habits of the * operator in Python courses.

Q7. What’s the function of the __getitem__ technique in Python?

a) It’s used to get the worth of an merchandise from a dictionary.

b) It’s used to outline the habits for accessing gadgets utilizing sq. brackets ([]).

c) It’s used to retrieve an merchandise from an inventory.

d) It’s used to get the merchandise at a particular index in a sequence.

Reply: b

Rationalization: The __getitem__ technique is used to outline the habits for accessing gadgets utilizing sq. brackets ([]) for customized objects.

Q8. Which dunder technique is used to implement the habits of the in operator in Python?

a) __exists__

b) __contains__

c) __includes__

d) __in__

Reply: b

Rationalization: The __contains__ technique is used to outline the habits of the in operator in Python courses.

Q9. What’s the function of the __setattr__ technique in Python?

a) It’s used to set the worth of an attribute.

b) It’s used to outline the habits of attribute task.

c) It’s used to test if an attribute exists.

d) It’s used to delete an attribute.

Reply: b

Rationalization: The __setattr__ technique is used to outline the habits when an attribute is ready in a Python class.

Q10. Which dunder technique is used to implement the habits of the - operator in Python?

a) __sub__

b) __minus__

c) __subtract__

d) __difference__

Reply: a

Rationalization: The __sub__ technique is used to outline the habits of the - operator in Python courses.

Q11. What does the __iter__ technique do in Python?

a) It returns an iterator object for the category.

b) It checks if an object is iterable.

c) It defines the habits of the for loop for the category.

d) It returns the subsequent merchandise within the iteration.

Reply: a

Rationalization: The __iter__ technique is used to return an iterator object for the category, permitting it for use in a for loop.

Q12. Which dunder technique is used to implement the habits of the // operator in Python?

a) __floor_div__

b) __floordiv__

c) __divide__

d) __intdiv__

Reply: b

Rationalization: The __floordiv__ technique is used to outline the habits of the // operator in Python courses.

Q13. What’s the function of the __contains__ technique in Python?

a) It checks if a component is contained in an object.

b) It checks if an object comprises 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

Rationalization: The __contains__ technique is used to outline the habits of the in operator for customized objects, checking if a component is contained in a sequence.

Q14. Which dunder technique is used to implement the habits of the == operator in Python?

a) __equal__

b) __eq__

c) __equals__

d) __is__

Reply: b

Rationalization: The __eq__ technique is used to outline the habits of the == operator in Python courses.

Q15. What does the __add__ technique 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

Rationalization: The __add__ technique is used to outline the habits of the + operator for customized objects, permitting them to be added collectively.

Q16. Which dunder technique is used to implement the habits of the [] operator for setting values in Python?

a) __setitem__

b) __set__

c) __assign__

d) __update__

Reply: a

Rationalization: The __setitem__ technique is used to outline the habits of the [] operator for setting values in Python courses.

Q17. What’s the function of the __delattr__ technique in Python?

a) It’s used to delete an attribute from an object.

b) It’s used to outline the habits when an attribute is deleted.

c) It’s used to test if an attribute might be deleted.

d) It’s used to return the worth of a deleted attribute.

Reply: a

Rationalization: The __delattr__ technique is used to outline the habits when an attribute is deleted from a Python object.

Q18. What does the __next__ technique do in Python?

a) It returns the subsequent ingredient in an iteration.

b) It checks if there are extra parts in an iteration.

c) It defines the habits of the subsequent() operate.

d) It raises a StopIteration exception.

Reply: a

Rationalization: The __next__ technique is used to outline the habits of retrieving the subsequent ingredient in an iteration.

Q19. Which dunder technique is used to implement the habits of the ** operator in Python?

a) __exp__

b) __pow__

c) __power__

d) __exponent__

Reply: b

Rationalization: The __pow__ technique is used to outline the habits of the ** operator for exponentiation in Python courses.

Q24. Which dunder technique is used to implement the habits of the != operator in Python?

a) __not_equal__

b) __ne__

c) __noteq__

d) __not_equals__

Reply: b

Rationalization: The __ne__ technique is used to outline the habits of the != operator in Python courses.

Q25. What’s the function of the __call__ technique in Python?

a) It calls one other technique within the class.

b) It defines the habits of calling an occasion of the category as a operate.

c) It checks if the item is callable.

d) It returns the item’s kind.

Reply: b

Rationalization: The __call__ technique is used to outline the habits of calling an occasion of the category as if it have been a operate.

Q26. Which dunder technique is used to implement the habits of the -= operator in Python?

a) __decr__

b) __minus_equal__

c) __isub__

d) __sub_assign__

Reply: c

Rationalization: The __isub__ technique is used to outline the habits of the -= operator in Python courses.

Q27. Which dunder technique is used to implement the habits of the += operator in Python?

a) __increase__

b) __add_assign__

c) __iadd__

d) __plus_equal__

Reply: c

Rationalization: The __iadd__ technique is used to outline the habits of the += operator in Python courses.

Q28. Which dunder technique is used to implement the habits of the > operator in Python?

a) __gt__

b) __greater__

c) __greater_than__

d) __isgreater__

Reply: a

Rationalization: The __gt__ technique is used to outline the habits of the > operator in Python courses.

Q29. Which dunder technique is used to implement the habits of the <= operator in Python?

a) __le__

b) __less__

c) __lessthan__

d) __islessthan__

Reply: a

Rationalization: The __le__ technique is used to outline the habits of the <= operator in Python courses.

Q30. Which dunder technique is used to implement the habits of the >= operator in Python?

a) __ge__

b) __greater__

c) __greaterthan__

d) __isgreater__

Reply: a

Rationalization: The __ge__ technique is used to outline the habits of the >= operator in Python courses.

Q31. Which dunder technique is used to implement the habits of the < operator in Python?

a) __lt__

b) __lesser__

c) __lessthan__

d) __islessthan__

Reply: a

Rationalization: The __lt__ technique is used to outline the habits of the < operator in Python courses.

Q32. Which particular technique 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)
outcome = obj1 + obj2
print(outcome)

a) __add__

b) __plus__

c) __sum__

d) __concat__

Reply: a

Rationalization: The __add__ technique overloads the addition operator + in Python courses.

Q33. Which particular technique 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)
outcome = f1 / f2
print(outcome)

a) __divide__

b) __truediv__

c) __fraction__

d) __div__

Reply: b

Rationalization: The __truediv__ technique overloads the division operator / in Python courses.

Q34. Which particular technique 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)
outcome = point1 < point2
print(outcome)

a) __less__

b) __lt__

c) __lessthan__

d) __islessthan__

Reply: b

Rationalization: The __lt__ technique overloads the lower than operator < in Python courses.

Congratulations on finishing the Python Particular Strategies MCQs! Particular strategies in Python present a robust strategy to customise the habits 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. Hold training and experimenting with Python’s particular strategies to grow to be proficient in constructing versatile and highly effective Python courses. When you have any questions or wish to delve deeper into any matter, don’t hesitate to proceed your studying journey. Completely satisfied coding!

You may also enroll in our free Python Course Right this moment!

Learn our extra articles associated to MCQs in Python: