What is the time complexity of accessing an element by index in a Python list?
Answer: C
Python lists are implemented as dynamic arrays. Accessing an element by index is a constant-time operation O(1) because the memory address of any element can be computed directly using the base address and the index.
Q.2Medium
What will be the output of the following code?
```python
d = {'a': 1, 'b': 2, 'c': 3}
print(list(d.keys()))
print(list(d.values()))
```
Answer: A
From Python 3.7 onwards, dictionaries maintain insertion order. The keys() method returns a view of keys and values() returns a view of values. Wrapping them in list() converts them to lists, producing ['a', 'b', 'c'] and [1, 2, 3] respectively.
Q.3Medium
Which of the following Python data structures does NOT allow duplicate elements?
Answer: C
A set in Python is an unordered collection that automatically discards duplicate values. Lists, tuples, and deques all allow duplicate elements. For example, {1, 2, 2, 3} evaluates to {1, 2, 3}.
Q.4Medium
What will be the output of the following code?
```python
stack = []
stack.append(10)
stack.append(20)
stack.append(30)
stack.pop()
print(stack)
```
Answer: B
A Python list used as a stack follows the LIFO (Last In, First Out) principle. After appending 10, 20, and 30, the list is [10, 20, 30]. Calling pop() removes the last element (30), leaving [10, 20].
Q.5Medium
What is the key difference between a Python list and a tuple?
Answer: C
The primary difference is mutability. Lists are mutable, meaning elements can be added, removed, or changed after creation. Tuples are immutable; once created, their content cannot be modified. Both support heterogeneous data and indexing.
Advertisement
Q.6Medium
What will be the output of the following code?
```python
from collections import deque
dq = deque([1, 2, 3])
dq.appendleft(0)
dq.append(4)
print(list(dq))
```
Answer: B
A deque (double-ended queue) allows appending to both ends. appendleft(0) inserts 0 at the front, making it [0, 1, 2, 3]. Then append(4) adds 4 at the rear, giving [0, 1, 2, 3, 4].
Q.7Medium
Which of the following is the correct way to merge two dictionaries d1 and d2 in Python 3.9 and above, where d2's values take priority for common keys?
Answer: B
Python 3.9 introduced the | operator for merging dictionaries. d1 | d2 creates a new dictionary with d2's values overwriting d1's for common keys. While d1.update(d2) modifies d1 in place (not creating a new dict), and dict(d1, **d2) works but is less clean, the | operator is the idiomatic Python 3.9+ approach that also returns a new dictionary.
Q.8Medium
What will be the output of the following code?
```python
my_set = {3, 1, 4, 1, 5, 9, 2, 6, 5}
print(len(my_set))
```
Answer: C
Sets do not allow duplicate elements. The set {3, 1, 4, 1, 5, 9, 2, 6, 5} contains duplicates: 1 appears twice and 5 appears twice. After removing duplicates, the unique elements are {1, 2, 3, 4, 5, 6, 9}, which gives a length of 7.
Q.9Medium
What will be the output of the following code?
```python
from collections import Counter
words = ['apple', 'banana', 'apple', 'cherry', 'banana', 'apple']
c = Counter(words)
print(c.most_common(2))
```
Answer: A
Counter counts the frequency of each element. Here apple appears 3 times, banana 2 times, and cherry 1 time. The most_common(2) method returns the top 2 elements as a list of (element, count) tuples sorted by frequency in descending order: [('apple', 3), ('banana', 2)].
Q.10Medium
What will be the output of the following code?
```python
my_list = [1, 2, 3, 4, 5]
print(my_list[1:4])
print(my_list[::-1])
```
Answer: A
List slicing my_list[1:4] extracts elements from index 1 up to (but not including) index 4, giving [2, 3, 4]. The slice my_list[::-1] uses a step of -1 to reverse the entire list, producing [5, 4, 3, 2, 1]. Both results together match option A.