Unit 13: NumPy and Pandas - Practice Quiz
1 Which symbol begins a single-line comment in Python?
/*
#
//
<!--
2 Which Python function displays output on the screen?
print()
type()
input()
len()
3
What is the Python data type of the value 25?
str
float
int
bool
4
Which Python data type stores either True or False?
str
bool
list
float
5 Which built-in function returns the data type of a Python object?
type()
range()
input()
print()
6 What is the conventional alias used when importing NumPy?
np
numpy_library_alias
ny
pd
7 Which NumPy function creates an array from a Python list?
np.array()
np.series()
np.create_an_array_from_list()
np.frame()
8 Which attribute gives the dimensions of a NumPy array?
dtype
index
shape
size
9
What does np.zeros(3) create?
10 What is the conventional alias used when importing Pandas?
pd
pn
np
ps
11 Which Pandas object represents a one-dimensional labeled sequence?
12 Which Pandas object stores data in labeled rows and columns?
13 What is the index position of the first element in a Python sequence?
-1
1
0
2
14
In Python slicing, what does data[1:4] select?
15 Which Pandas accessor selects Series values by index label?
.shape
.loc
.iloc
.dtype
16 Which Pandas accessor selects a Series value by integer position?
.select_the_value_at_an_integer_position
.loc
.drop
.iloc
17
If df is a DataFrame, which expression selects the column named age?
df.select_the_column_named_age()
df["age"]
df.locate("age")
df("age")
18
Which expression selects the first row of a DataFrame named df by position?
df[0]
df.loc[1]
df.select_the_first_row_by_its_zero_based_position()
df.iloc[0]
19 Which value commonly represents missing numerical data in Pandas?
NaN
False
0
20 Which Pandas method replaces missing values with a specified value?
.dropna()
.replace_all_rows_containing_missing_values()
.isna()
.fillna()
21
What is the value of result after executing values = [1, 2, 3, 4] and result = [x * 2 if x % 2 == 0 else x for x in values]?
[1, 2, 3, 4]
[1, 4, 3, 8]
[2, 4, 6, 8]
[2, 2, 6, 4]
22
Given a = [1, 2], b = a, and b.append(3), what are the final values of a and b?
a = [1, 2], b = [1, 2, 3]
a = [1, 2], b = [3]
a = [1, 2, 3], b = [1, 2]
a = [1, 2, 3], b = [1, 2, 3]
23
If NumPy arrays a and b have shapes (3, 1) and (1, 4), respectively, what is the shape of a + b?
(3, 1)
(3, 4)
(4, 3)
(1, 4)
24
Consider a = np.arange(5), b = a[1:4], and b[0] = 99. What is the resulting value of a?
[0, 1, 2, 3, 4]
[0, 99, 2, 3, 4]
[99, 1, 2, 3, 4]
[0, 1, 99, 3, 4]
25
Given arr = np.array([[1, 4], [3, 2]]), what does arr[arr > 2] return?
array([[4], [3]])
array([4, 3])
array([3, 4])
array([1, 2])
26
For x = np.array([[1, 2, 3], [4, 5, 6]]), what is the result of x.sum(axis=0)?
array([5, 7, 9])
array([6, 15])
array([3, 7, 11])
array([21])
27
After executing d = {True: "yes", 1: "one"}, what are len(d) and d[True]?
2 and "one"
2 and "yes"
1 and "yes"
1 and "one"
28
What is the typical dtype of np.array([1, 2, np.nan])?
object
bool
float64
int64
29
Let s1 = pd.Series([1, 2], index=["a", "b"]) and s2 = pd.Series([10, 20], index=["b", "c"]). What does s1 + s2 produce?
a: 11, b: 22, c: NaN
a: NaN, b: 12, c: NaN
a: 11, b: 22
a: 1, b: 12, c: 20
30
A DataFrame has team = ["A", "A", "B"] and score = [10, 20, 30]. What does df.groupby("team")["score"].mean() return?
A: 20, B: 30
A: 30, B: 30
A: 15, B: 30
A: 10, B: 25
31
Given s = pd.Series([10, 20, 30], index=["a", "b", "b"]), what does s.loc["b"] return?
10 and 20
30
20
20 and 30
32
Given s = pd.Series([10, 20, 30, 40], index=["a", "b", "c", "d"]), what does s.iloc[1:3] select?
30 and 40
20 and 30
10 and 20
20, 30, and 40
33
A DataFrame indexed by r1 and r2 has row r2 equal to A=5, B=6, and C=7. What does df.loc["r2", ["A", "C"]] select?
B=6 and C=7
5
A=5 and C=7
A=5 and B=6
34
If a DataFrame has columns ordered as A, B, and C, what is the column order in df[["C", "A"]]?
C, B, then A
A, then C
C, then A
A, B, then C
35
For df = pd.DataFrame({"A": [1, 3, 2], "B": [10, 20, 30]}), what does df.loc[(df["A"] > 1) & (df["B"] < 30), "B"].tolist() return?
[10]
[20]
[10, 20]
[20, 30]
36
What is the result of pd.Series([0, None, np.nan, ""]).isna().tolist()?
[False, True, True, False]
[False, False, True, True]
[False, True, False, True]
[True, True, True, False]
37
Rows r1, r2, and r3 contain respectively 2, 1, and 3 non-missing values. Which rows remain after df.dropna(thresh=2)?
r1
r3
r1 and r3
r2 and r3
38
A DataFrame has team = ["A", "A", "B", "B"] and score = [10, np.nan, np.nan, 30]. What are the scores after df["score"].fillna(df.groupby("team")["score"].transform("median"))?
[10, NaN, NaN, 30]
[10, 30, 10, 30]
[10, 20, 20, 30]
[10, 10, 30, 30]
39
What is the result of pd.Series([1, np.nan, np.nan, 4]).ffill(limit=1).tolist()?
[1, 1, 1, 4]
[1, NaN, 4, 4]
[1, 1, NaN, 4]
[1, 4, 4, 4]
40
What values result from pd.Series([2, np.nan, np.nan, 8]).interpolate(method="linear")?
[2, 4, 6, 8]
[2, 5, 5, 8]
[2, 2, 2, 8]
[2, 3, 4, 8]
41
What does the following code print?
def collect(x, bucket=[]):
bucket.append(x)
return bucket
a = collect(1)
b = collect(2)
a.append(3)
print(b)
[1, 2]
[1, 2, 3]
[2, 3]
[2, 3], because each invocation creates a fresh default list before returning it
42
Given the following code, what is the final value of a[0]?
import numpy as np
a = np.arange(12).reshape(3, 4)
b = a[:, [1, 3]]
c = a[:, 1:3]
b[0, 0] = 99
c[0, 0] = 88
array([0, 99, 88, 3])
array([0, 99, 2, 3])
array([0, 88, 2, 3])
array([0, 88, 2, 99])
43
What does np.result_type(np.int32, np.float32) return under NumPy's standard type-promotion rules?
dtype('float32')
dtype('int32')
dtype('float64')
dtype('object')
44
What is produced by the final expression?
import numpy as np
a = np.array(['1', '22'], dtype='<U2')
a[0] = '333'
a.tolist()
['333', '22']
['33', '22']
['3', '22']
ValueError is raised because the assigned string exceeds the fixed-width Unicode data type
45
What is the result of the final expression?
import pandas as pd
s1 = pd.Series({'a': 1, 'b': 2})
s2 = pd.Series({'b': 10, 'c': 20})
s1.add(s2, fill_value=0).sort_index().tolist()
[NaN, 12.0, NaN]
[0.0, 12.0, 0.0]
[1.0, 12.0, 20.0]
[1.0, 10.0, 20.0]
46
For the following Series, which pair of index lists is returned?
import pandas as pd
s = pd.Series(['a', 'b', 'c', 'd'], index=[2, 4, 6, 8])
list(s.loc[4:8].index), list(s.iloc[1:3].index)
([4, 6], [4, 6])
([4, 6, 8], [4, 6])
([4, 6, 8], [4, 6, 8])
([6, 8], [2, 4, 6])
47
What is the result of the final expression?
import pandas as pd
s = pd.Series([10, 20, 30], index=[1, 3, 5])
(s[1], s.iloc[1])
(10, 10)
(20, 20)
KeyError is raised because integer keys can only be interpreted as positional selectors
(10, 20)
48
What is the final value of s.tolist()?
import pandas as pd
s = pd.Series([1, 2, 3], index=['a', 'a', 'b'])
s.loc['a'] = 0
[0, 0, 3]
[1, 0, 3]
[0, 2, 3]
[0, 3]
49
What does the final expression return?
import pandas as pd
df = pd.DataFrame({'x': [1, 2, 3]}, index=['a', 'b', 'c'])
mask = pd.Series([True, False, True], index=['c', 'a', 'b'])
df.loc[mask, 'x'].tolist()
[1, 2]
[1, 3]
[2, 3]
[3, 1]
50
What is returned by the final expression?
import pandas as pd
idx = pd.MultiIndex.from_product([['A', 'B'], [1, 2]])
df = pd.DataFrame({'v': [0, 1, 2, 3]}, index=idx)
df.loc[('A', slice(None)), 'v'].tolist()
[0, 1]
[0, 2]
[1, 2]
[0, 1, 2, 3]
51
Given duplicate column labels, what does the final expression produce?
import pandas as pd
df = pd.DataFrame([[1, 2, 3]], columns=['x', 'x', 'y'])
(type(df['x']).__name__, df['x'].shape)
('Series', (2,))
('Series', (1,))
('DataFrame', (1, 2))
('DataFrame', (2, 1))
52
Under Pandas' nullable Boolean logic, what is the result?
import pandas as pd
s = pd.Series([True, pd.NA, False], dtype='boolean')
(s & False).tolist()
[True, <NA>, False]
[False, False, False]
TypeError is raised because pd.NA cannot participate in any Boolean operation
[False, <NA>, False]
53
What does the final expression return under the default Pandas missing-value rules?
import numpy as np
import pandas as pd
s = pd.Series([np.nan, None, pd.NA, np.inf], dtype='object')
pd.isna(s).tolist()
[True, True, True, False]
[False, True, True, False]
[True, True, True, True]
[True, False, True, False]
54
Which index remains after the final operation?
import numpy as np
import pandas as pd
df = pd.DataFrame({
'a': [1, np.nan, 3],
'b': [np.nan, 2, 4],
'c': [np.nan, 3, np.nan]
}, index=['r0', 'r1', 'r2'])
df.dropna(subset=['a', 'b'], thresh=2).index.tolist()
['r1', 'r2']
['r0']
['r2']
['r1']
55
Which description matches the result?
import numpy as np
import pandas as pd
df = pd.DataFrame({
'k': ['a', 'a', None, 'b'],
'v': [1.0, np.nan, 3.0, np.nan]
})
r = df.groupby('k')['v'].sum(min_count=1)
'a' maps to 1.0, 'b' maps to NaN, and the missing key maps to 3.0 because all keys are retained by default
'a' maps to 1.0, 'b' maps to NaN, and the missing key is omitted
'a' maps to NaN, 'b' maps to NaN, and the missing key is included
'a' maps to 1.0, 'b' maps to 0.0, and the missing key is omitted
56
What is the result of s.ffill(limit=1).tolist()?
import numpy as np
import pandas as pd
s = pd.Series([1.0, np.nan, np.nan, 4.0, np.nan])
[1.0, 1.0, 1.0, 4.0, 4.0]
[1.0, 1.0, NaN, 4.0, 4.0]
[1.0, 1.0, NaN, 4.0, NaN]
[1.0, NaN, 1.0, 4.0, 4.0]
57
What does the interpolation produce?
import numpy as np
import pandas as pd
s = pd.Series([np.nan, 1.0, np.nan, np.nan, 4.0, np.nan])
s.interpolate(limit_area='inside').tolist()
[1.0, 1.0, 2.0, 3.0, 4.0, 4.0]
[NaN, 1.0, 2.5, 2.5, 4.0, NaN]
[NaN, 1.0, 2.0, 3.0, 4.0, NaN]
[NaN, 1.0, NaN, NaN, 4.0, NaN]
58
Let a.shape == (2, 3) and b.shape == (2,). Which expression adds b[i] to every element of row i without explicitly constructing a full (2, 3) array?
a + b[:, None]
a + b[None, :]
a + np.resize(b, a.shape), which repeatedly tiles the values in flattened element order
a + b
59
What is the result of the final expression?
import numpy as np
a = np.arange(6).reshape(2, 3)
b = a.T.reshape(6)
b[0] = -1
(np.shares_memory(a, b), a[0, 0])
(False, -1)
(True, -1)
(False, 0)
(True, 0)
60
What is df['x'].tolist() after the aligned assignment?
import numpy as np
import pandas as pd
df = pd.DataFrame({'x': [0.0, 0.0, 0.0]}, index=['a', 'b', 'c'])
rhs = pd.Series([10.0, 20.0], index=['c', 'a'])
df.loc[:, 'x'] = rhs
[20.0, 10.0, 0.0]
[20.0, NaN, 10.0]
[10.0, 20.0, NaN]
[10.0, NaN, 20.0]
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →