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.
Q.102Medium
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.103Medium
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.104Medium
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.105Medium
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)].
Advertisement
Q.106Medium
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.
Q.107Medium
What will the following code output?
```python
import numpy as np
a = np.array([1, 2, 3, 4, 5])
print(a[a > 2])
```
Answer: A
Boolean indexing in NumPy: `a > 2` creates a boolean array `[False, False, True, True, True]`. When used as an index, it selects only the elements where the condition is True, resulting in `[3 4 5]`.
Q.108Medium
Which NumPy function is used to compute the dot product of two 1-D arrays `a = np.array([1, 2, 3])` and `b = np.array([4, 5, 6])`?
Answer: B
`np.dot(a, b)` computes the dot product: 1×4+2×5+3×6=4+10+18=32. `np.multiply` returns element-wise product, `np.cross` gives the cross product, and `np.sum(a, b)` is invalid syntax.
Q.109Medium
What will the following code output?
```python
import numpy as np
a = np.arange(12).reshape(3, 4)
print(a.shape)
```
Answer: C
`np.arange(12)` creates a 1-D array of 12 elements `[0, 1, ..., 11]`. `.reshape(3, 4)` reshapes it into a 2-D array with 3 rows and 4 columns. Therefore `a.shape` returns `(3, 4)`.
Q.110Medium
What is the output of the following code?
```python
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
print(df['A'].sum())
```
Answer: B
`df['A']` selects column A which contains `[1, 2, 3]`. The `.sum()` method computes 1+2+3=6. Option A (15) would be the sum of column B (4+5+6=15), and option C (21) would be the total of both columns.
Q.111Medium
What will the following code output?
```python
import pandas as pd
df = pd.DataFrame({'X': [10, 20, 30, 40], 'Y': [1, 2, 3, 4]})
print(df.iloc[1:3, 0])
```
Answer: A
`iloc` uses integer-based indexing. `iloc[1:3, 0]` selects rows at positions 1 and 2 (indices 1 and 2) and the column at position 0 (column 'X'). This gives values 20 and 30 with their row labels 1 and 2.
Q.112Medium
Which of the following correctly creates a NumPy array of shape `(3, 3)` filled with zeros of integer data type?
Answer: A
`np.zeros((3, 3), dtype=int)` is the correct syntax to create a 3×3 array of integer zeros. Option B uses `np.zero` which does not exist. Option C uses `dtype=float` which gives floats, not integers. Option D uses `np.empty` which allocates memory without initializing to zero.
Q.113Medium
What is the output of the following code?
```python
import pandas as pd
df = pd.DataFrame({'A': [1, None, 3], 'B': [4, 5, None]})
print(df.isnull().sum())
```
Answer: A
`df.isnull()` returns a DataFrame of boolean values where `True` indicates a missing value. `.sum()` counts `True` values per column. Column A has one `None` (at index 1), and column B has one `None` (at index 2), so the result is A: 1, B: 1.
Q.114Medium
What will the following NumPy code output?
```python
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
print(np.matmul(a, b))
```
Answer: A
`np.matmul` performs matrix multiplication. For $A =
What does the following Pandas code do?
```python
import pandas as pd
df = pd.DataFrame({'City': ['Delhi', 'Mumbai', 'Delhi', 'Chennai'],
'Sales': [100, 200, 150, 300]})
result = df.groupby('City')['Sales'].mean()
print(result)
```
Answer: A
`groupby('City')` groups rows by the City column. `.mean()` computes the average Sales per city. Delhi appears twice with values 100 and 150: 2100+150=125.0. Mumbai: 200.0. Chennai: 300.0. The result is sorted alphabetically by city name.
Q.116Medium
What will the following code output?
```python
import numpy as np
a = np.array([1, 2, 3])
b = np.array([10, 20, 30])
print(np.concatenate((a, b)))
print(np.stack((a, b)).shape)
```
Answer: A
`np.concatenate((a, b))` joins the two 1-D arrays end-to-end producing `[ 1 2 3 10 20 30]` of shape `(6,)`. `np.stack((a, b))` stacks arrays along a new axis (default axis=0), creating a 2-D array of shape `(2, 3)` — 2 arrays each of length 3. So the shape is `(2, 3)`.
Q.117Medium
What is the time complexity of the binary search algorithm implemented on a sorted list in Python?
Answer: C
Binary search repeatedly divides the search space in half. At each step, the problem size reduces by half, giving a recurrence T(n)=T(n/2)+O(1), which solves to O(logn).
Q.118Medium
What will be the output of the following code implementing bubble sort?
```python
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
return arr
print(bubble_sort([64, 34, 25, 12, 22]))
```
Answer: A
Bubble sort repeatedly compares adjacent elements and swaps them if they are in the wrong order. After all passes over [64, 34, 25, 12, 22], the final sorted result is [12, 22, 25, 34, 64].
Q.119Medium
What is the worst-case time complexity of Quick Sort?
Answer: C
Quick Sort's worst case occurs when the pivot chosen is always the smallest or largest element (e.g., already sorted input with naive pivot selection), leading to n recursive calls each of size n−1. This gives T(n)=T(n−1)+O(n), which solves to O(n2).
Q.120Medium
What will be the output of the following code?
```python
def binary_search(arr, target):
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
print(binary_search([2, 5, 8, 12, 16, 23], 12))
```
Answer: A
The function performs binary search on [2, 5, 8, 12, 16, 23] looking for 12. The index of 12 in the list is 3 (0-based). The midpoints traced are index 2 (value 8), then index 4 (value 16), then index 3 (value 12) — match found at index 3.