Consider: a = 'Hello'; b = 'Hello'. Are a and b the same object?
Answer: C
Due to string interning in Python, small strings may be cached and refer to the same object. However, this is an implementation detail. a == b is True, but a is b may vary.
Q.42Medium
Consider the code: def func(x, y=5): return x + y. What is func(3)?
Answer: C
y has a default value of 5. When func(3) is called, x=3 and y=5 (default). The function returns 3 + 5 = 8.
Q.43Medium
Consider: x = [1, 2, 3, 4, 5]; y = x[1:4]. What is y?
Answer: B
List slicing x[1:4] includes indices 1, 2, 3 (but excludes 4). Elements are 2, 3, 4 from the list [1, 2, 3, 4, 5].
Q.44Medium
Consider: d = {'a': 1, 'b': 2}; d['c'] = 3. What is d?
Answer: B
Dictionaries are mutable. The assignment d['c'] = 3 adds a new key-value pair to the dictionary. d becomes {'a': 1, 'b': 2, 'c': 3}.
Q.45Medium
Consider the code: x = (1, 2, 3); x[0] = 5. What happens?
Answer: B
Tuples are immutable. Attempting to modify a tuple element with x[0] = 5 raises a TypeError: 'tuple' object does not support item assignment.
Advertisement
Q.46Medium
What is the output of: print(None == False)?
Answer: B
None and False are distinct objects. None == False evaluates to False. None is a unique singleton object, not equivalent to False or 0.
Q.47Medium
Consider the following code snippet:
python
x = [1, 2, 3]
y = x
y.append(4)
print(x)
What will be the output?
Answer: A
In Python, when you assign y = x, both variables reference the same list object in memory. When you modify the list using y.append(4), the original list x is also modified because they point to the same object. Therefore, print(x) outputs [1, 2, 3, 4].
Q.48Medium
What is the output of: len([1, [2, 3], 4, [5, [6, 7]]])?
Answer: B
len() counts the number of top-level elements in the list: 1, [2,3], 4, and [5,[6,7]] = 4 elements total.
Q.49Medium
Which statement correctly creates a list of numbers from 1 to 5?
Answer: D
range(1, 6) generates 1 to 5 (exclusive end), and explicit list [1,2,3,4,5] both work. Both statements are correct.
Q.50Medium
What will be printed?
my_list = [10, 20, 30]
my_list[1] = 25
print(my_list)
Answer: B
Lists are mutable. Assignment my_list[1] = 25 changes the element at index 1 from 20 to 25.
Q.51Medium
Which of the following will return True?
my_list = [1, 2, 3]
result = 2 in my_list
Answer: A
The 'in' operator checks membership. Since 2 exists in my_list, the expression evaluates to True.
Q.52Medium
What is the difference between list and tuple in Python?
Answer: A
Lists (created with []) can be modified after creation, while tuples (created with ()) cannot be changed after initialization.
Q.53Medium
What will be the output?
my_list = [1, 2, 3]
my_list.extend([4, 5])
print(my_list)
Answer: B
extend() unpacks the iterable and adds individual elements. append() would add the list as a single element.
Q.54Medium
How many times will the value 2 appear in the output?
my_list = [1, 2, 2, 3, 2, 4]
print(my_list.count(2))
Answer: C
count() returns the number of occurrences of a value. The value 2 appears 3 times in the list.
Q.55Medium
What will be the output?
my_list = [[1, 2], [3, 4], [5, 6]]
print(my_list[1][0])
Answer: B
my_list[1] returns [3, 4], and [3, 4][0] returns 3 (first element of the nested list).
Q.56Medium
What will this code produce?
my_list = [3, 1, 4, 1, 5]
my_list.sort()
print(my_list)
Answer: B
sort() arranges list elements in ascending order by default. It modifies the list in-place.
Q.57Medium
What will be the output of the following code?
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return 'Some sound'
class Dog(Animal):
def speak(self):
return 'Woof'
d = Dog('Rex')
print(d.speak())
Answer: B
Dog overrides the speak() method of Animal. When speak() is called on a Dog instance, Python uses method resolution order (MRO) and finds speak() in Dog first, so it returns 'Woof'. This is an example of method overriding in inheritance.
Q.58Medium
Which of the following best describes the purpose of the __str__ method in a Python class?
Answer: B
__str__ is a dunder (magic) method that defines the human-readable string representation of an object. When you call print(obj) or str(obj), Python internally calls obj.__str__(). It is meant to be informative and readable for end users.
Q.59Medium
What will be the output of the following code?
class Counter:
count = 0
def __init__(self):
Counter.count += 1
a = Counter()
b = Counter()
c = Counter()
print(Counter.count)
Answer: C
count is a class variable shared across all instances. Each time __init__ is called (once per object creation), Counter.count is incremented by 1. Since three objects (a, b, c) are created, Counter.count becomes 3.
Q.60Medium
What will be the output of the following code?
class MyClass:
def __init__(self, x):
self.__x = x
obj = MyClass(10)
print(obj.__x)
Answer: C
__x is a name-mangled private attribute. Python internally renames it to _MyClass__x to prevent direct external access. Accessing obj.__x from outside the class raises an AttributeError. The correct way to access it would be obj._MyClass__x.