Skip to main content

Module 2 Test: Python for Data Science

This is your progress tracker . 20 questions: 5 Easy, 10 Moderate, 5 Hard . First solve all the questions yourself (before running the code in Colab, work out the output in your head), then check with the Answer Key.

ScoreMeaning
18 to 20Excellent! Your grip on Module 2 is strong.
12 to 17Good. Re-read the chapters of the questions you got wrong.
Below 12Do not worry. Re-read Chapters 5 to 8 and run the code yourself, then take the test again.
EASY​
Q1. Worldwide, what short name is given to NumPy when importing it?​

A) num B) np C) numpy D) nm

​
EASY​
Q2. In Pandas, what is the whole table (both rows and columns) called?​

A) Series B) Array C) DataFrame D) List

​
EASY​
Q3. What is a histogram used for?​

A) To see change over time B) To see the distribution of a numeric column C) To show parts of a whole D) To join two text columns

​
EASY​
Q4. Which library is Seaborn built on?​

A) Pandas B) TensorFlow C) Matplotlib D) Scikit-learn

​
EASY​
Q5. How many rows does df.head() show when you write nothing inside it?​

A) 1 B) 3 C) 10 D) 5

​
MODERATE​
Q6. What will be the output of this code?​
import numpy as np
print(np.arange(0, 10, 3))

A) [0 3 6 9] B) [0 3 6] C) [3 6 9]

D) [0 1 2 3]

​
MODERATE​
Q7. What will be the output of this code?​
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a.sum(axis=0))

A) [6 15] B) [5 7 9] C) 21 D) [1 2 3 4 5 6]

MODERATE​
Q8. What will be the output of this code?​
import numpy as np
a = np.array([1, 2, 3, 4, 5, 6])
print(a[a % 2 == 0])

A) [1 3 5] B) [2 4 6] C) [False True False True False True] D) [0 2 4]

​
MODERATE​
Q9. What will be the output of this code?​
import pandas as pd
s = pd.Series([10, 20, 30], index=["a", "b", "c"])
print(s["b"])

A) 10 B) 20 C) 30 D) b

​
MODERATE​
Q10. What is the difference between df.loc[0:2] and df.iloc[0:2]? What will be the output of this code?​
import pandas as pd
df = pd.DataFrame({"x": [1, 2, 3, 4, 5]})
print(len(df.loc[0:2]), len(df.iloc[0:2]))

A) 2 2 B) 3 3 C) 2 3 D) 3 2

MODERATE​
Q11. What will be the output of this code?​
import pandas as pd
df = pd.DataFrame({"x": [1, None, 3]})
print(df["x"].fillna(df["x"].mean()).tolist())

A) [1.0, 0.0, 3.0] B) [1.0, 2.0, 3.0] C) [1.0, 3.0, 3.0] D) [1.0, nan, 3.0]

​
MODERATE​
Q12. What will be the output of this code?​
import pandas as pd
df = pd.DataFrame({"c": ["A", "B", "A", "B"], "v": [10, 20, 30, 40]})
print(df.groupby("c")["v"].sum().to_dict())

A) {'A': 40, 'B': 60} B) {'A': 20, 'B': 30} C) {'A': 10, 'B': 20} D) {'A': 30, 'B': 40}

MODERATE​
Q13. What will be the output of this code (rows of the outer and inner merge)?​
import pandas as pd
l = pd.DataFrame({"k": [1, 2, 3], "a": ["x", "y", "z"]})
r = pd.DataFrame({"k": [2, 3, 4], "b": [20, 30, 40]})
print(len(pd.merge(l, r, on="k", how="outer")), len(pd.merge(l, r, on="k", how="inner")))

A) 3 3 B) 4 2 C) 2 4 D) 4 4

​
MODERATE​
Q14. a has shape (3, 4). Which of the following operations will give an error because of broadcasting?​

A) a + np.ones(4) (shape 4) B) a + np.ones((3, 1)) (shape 3, 1) C) a + np.ones(3) (shape 3) D) a + 10

​
MODERATE​
Q15. The correlation of two columns comes out as 0.9. What is its correct meaning?​

A) The first column is the cause of the second

B) There is a strong direct relationship between the two columns, but causation is not proven C) There is no relationship between the two columns

D) The two columns move in opposite directions

​
HARD​
Q16. What will be the output of this code?​
import numpy as np
arr = np.array([10, 20, 30, 40])
print(arr[[3, 0]] + arr[1:3].sum())

A) [90 60] B) [40 10] C) [50 50] D) [90 90]

​
HARD​
Q17. What will be the output of this code (rows, columns)?​
import pandas as pd
df = pd.DataFrame({"a": [1, 2, 3, 4], "b": [5, 6, 7, 8]})
print(df[(df["a"] > 1) & (df["b"] < 8)].shape)

A) (3, 2) B) (2, 2) C) (1, 2) D) (4, 2)

​
HARD​
Q18. What will be the output of this code (standardization)?​
import numpy as np
x = np.array([[1, 10], [3, 30]])
print(((x - x.mean(axis=0)) / x.std(axis=0)).tolist())

A) [[0.0, 0.0], [1.0, 1.0]] B) [[-1.0, -1.0], [1.0, 1.0]] C) [[-1.0, 1.0], [1.0, -1.0]] D) [[1.0, 1.0], [3.0, 3.0]]

​
HARD​
Q19. What will be the output of this code?​
import numpy as np
a = np.array([5, 12, 8, 20])
print(np.where(a > 10, a - 10, 0).tolist())

A) [0, 2, 0, 10] B) [5, 2, 8, 10] C) [0, 12, 0, 20] D) [-5, 2, -2, 10]

​
HARD​
Q20. What will be the output of this code?​
import pandas as pd
df = pd.DataFrame({"city": ["K", "L", "K", "L", "K"], "s": [10, 20, 30, 40, 50]})
r = df.groupby("city").agg(n=("s", "count"), m=("s", "mean"))
print(r.loc["K", "n"], r.loc["K", "m"])

A) 2 15.0 B) 3 30.0 C) 3 90 D) 5 30.0

MODULE 2 TEST​

Module 2 Answer Key (Answers and explanations)​

#LevelAnswerExplanation
1EasyB) npThe well-known way is: import numpy as np.
2EasyC) DataFrameA DataFrame is the whole table. A Series is just one column of that table.
3EasyB) To see the distribution of a
numeric column
A histogram divides the values into bins and shows how many values fall
in each range.
4EasyC) MatplotlibSeaborn is built on Matplotlib, so Matplotlib's title, labels and so on work
here too.
5EasyD) 5By default head() shows the first 5 rows. You can change the number
with df.head(n).
6ModerateA) [0 3 6 9]arange(start, stop, step) starts at 0, moves forward in steps of 3 and
stops before 10: 0, 3, 6, 9.
7ModerateB) [5 7 9]axis=0 adds downwards (column-wise): 1+4, 2+5, 3+6 = [5 7 9].
8ModerateB) [2 4 6]a % 2 == 0 creates a boolean mask, and a[mask] gives only the True
elements (the even numbers).
9ModerateB) 20The value of the label "b" in the Series is 20.
10ModerateD) 3 2In loc the end is included (3 rows: 0, 1, 2), whereas in iloc the end is not
included (2 rows: 0, 1).
11ModerateB) [1.0, 2.0, 3.0]The missing value was filled with the mean. The mean of 1 and 3 is 2.0
(NaN is not counted).
12ModerateA) {'A': 40, 'B': 60}Group A has the values 10 and 30 (sum 40), group B has 20 and 40 (sum
60).
13ModerateB) 4 2Outer keeps all keys of both (1, 2, 3, 4, that is 4 rows), inner keeps only
the common keys (2, 3, that is 2 rows).
14ModerateC) a + np.ones(3) (shape 3)Shapes are matched from the end. With (3, 4) and (3,), the last
dimension is 4 versus 3, which is neither equal nor 1, hence the error.
15ModerateB) There is a strong direct
relationship between the two
columns, but causation is not
proven
+0.9 means a strong positive relationship. Correlation does not mean
causation.
16HardA) [90 60]arr[[3, 0]] = [40 10], arr[1:3].sum() = 20 + 30 = 50. By broadcasting, [40
10] + 50 = [90 60].
17HardB) (2, 2)Both conditions a > 1 and b < 8 are true only for the rows (2, 6) and (3,
7). So 2 rows, 2 columns.
18HardB) [[-1.0, -1.0], [1.0, 1.0]]Mean = [2, 20], std = [1, 10]. First row: (1-2)/1 = -1 and (10-20)/10 = -1.
Second row: +1, +1.
19HardA) [0, 2, 0, 10]Where a > 10 is true we get a - 10 (2 from 12, 10 from 20), otherwise 0.
20HardB) 3 30.0City K has 3 rows (10, 30, 50): count = 3, mean = 90 / 3 = 30.0.

Your score: ______ / 20