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.2Medium
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.3Medium
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.4Medium
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.5Medium
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.
Advertisement
Q.6Medium
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.7Medium
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.8Medium
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.10Medium
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)`.