Question 56
What is the output of slicing expression 'Python'[-3:] in Python?
Option E is correct (None of the above). The true answer is ''hon''. Negative index -3 starts 3 characters from the end ('h'), and slicing to the end produces 'hon'.
What is the output of slicing expression 'Python'[-3:] in Python?
Option E is correct (None of the above). The true answer is ''hon''. Negative index -3 starts 3 characters from the end ('h'), and slicing to the end produces 'hon'.
In Python OOP, what special method serves as the class constructor to initialize instance attributes?
Option C is correct. __init__() is the initializer method automatically called when a new object instance is created.
Which dictionary method safely returns a value for a given key, returning a default value if the key is NOT present in the dictionary?
Option B is correct. dict.get(key, default) avoids KeyErrors by returning default (or None) if key does not exist.
What does list comprehension [x**2 for x in range(5) if x % 2 == 0] evaluate to?
Option A is correct. range(5) gives 0, 1, 2, 3, 4. Even numbers are 0, 2, 4. Squaring them gives 0, 4, 16.
What does the @staticmethod decorator indicate when applied to a method inside a Python class?
Option A is correct. Static methods in Python behave like normal functions bound to a class namespace, receiving neither self nor cls implicitly.