Unit 13: NumPy and Pandas - Practice Quiz

ECAP792 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Which symbol begins a single-line comment in Python?

Introduction to Python Easy
A. /*
B. #
C. //
D. <!--

2 Which Python function displays output on the screen?

Introduction to Python Easy
A. print()
B. input()
C. type()
D. len()

3 What is the Python data type of the value 25?

Understanding data types in Python Easy
A. str
B. float
C. bool
D. int

4 Which Python data type stores either True or False?

Understanding data types in Python Easy
A. bool
B. str
C. list
D. float

5 Which built-in function returns the data type of a Python object?

Understanding data types in Python Easy
A. range()
B. input()
C. type()
D. print()

6 What is the conventional alias used when importing NumPy?

NumPy Easy
A. np
B. numpy_library_alias
C. pd
D. ny

7 Which NumPy function creates an array from a Python list?

NumPy Easy
A. np.frame()
B. np.create_an_array_from_list()
C. np.series()
D. np.array()

8 Which attribute gives the dimensions of a NumPy array?

NumPy Easy
A. shape
B. index
C. dtype
D. size

9 What does np.zeros(3) create?

NumPy Easy
A. An array containing three zeros
B. A table containing three labeled columns of missing values
C. An array containing three ones
D. An array containing numbers from zero to three inclusive

10 What is the conventional alias used when importing Pandas?

Pandas for data analysis Easy
A. np
B. pn
C. pd
D. ps

11 Which Pandas object represents a one-dimensional labeled sequence?

Pandas for data analysis Easy
A. Workbook
B. DataFrame
C. Series
D. Array

12 Which Pandas object stores data in labeled rows and columns?

Pandas for data analysis Easy
A. A multidimensional collection that cannot have row or column labels
B. Tuple
C. DataFrame
D. Series

13 What is the index position of the first element in a Python sequence?

Data indexing and selection Easy
A. 2
B. 0
C. 1
D. -1

14 In Python slicing, what does data[1:4] select?

Data indexing and selection Easy
A. Every fourth value beginning at position 1 and continuing to the end
B. Positions 1 through 4
C. Positions 1 through 3
D. Positions 0 through 4

15 Which Pandas accessor selects Series values by index label?

Data selection in Series Easy
A. .iloc
B. .loc
C. .shape
D. .dtype

16 Which Pandas accessor selects a Series value by integer position?

Data selection in Series Easy
A. .select_the_value_at_an_integer_position
B. .loc
C. .iloc
D. .drop

17 If df is a DataFrame, which expression selects the column named age?

Data selection in DataFrame Easy
A. df.locate("age")
B. df("age")
C. df["age"]
D. df.select_the_column_named_age()

18 Which expression selects the first row of a DataFrame named df by position?

Data selection in DataFrame Easy
A. df.select_the_first_row_by_its_zero_based_position()
B. df[0]
C. df.iloc[0]
D. df.loc[1]

19 Which value commonly represents missing numerical data in Pandas?

Missing data in Pandas Easy
A. NaN
B. 0
C. False
D. An automatically generated average of the surrounding numerical values

20 Which Pandas method replaces missing values with a specified value?

Handling missing data Easy
A. .fillna()
B. .replace_all_rows_containing_missing_values()
C. .dropna()
D. .isna()

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]?

Introduction to Python Medium
A. [2, 4, 6, 8]
B. [1, 4, 3, 8]
C. [1, 2, 3, 4]
D. [2, 2, 6, 4]

22 Given a = [1, 2], b = a, and b.append(3), what are the final values of a and b?

Introduction to Python Medium
A. a = [1, 2], b = [3]
B. a = [1, 2], b = [1, 2, 3]
C. a = [1, 2, 3], b = [1, 2]
D. 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?

NumPy Medium
A. (4, 3)
B. (3, 4)
C. (3, 1)
D. (1, 4)

24 Consider a = np.arange(5), b = a[1:4], and b[0] = 99. What is the resulting value of a?

NumPy Medium
A. [99, 1, 2, 3, 4]
B. [0, 1, 99, 3, 4]
C. [0, 1, 2, 3, 4]
D. [0, 99, 2, 3, 4]

25 Given arr = np.array([[1, 4], [3, 2]]), what does arr[arr > 2] return?

NumPy Medium
A. array([1, 2])
B. array([[4], [3]])
C. array([3, 4])
D. array([4, 3])

26 For x = np.array([[1, 2, 3], [4, 5, 6]]), what is the result of x.sum(axis=0)?

NumPy Medium
A. array([3, 7, 11])
B. array([21])
C. array([5, 7, 9])
D. array([6, 15])

27 After executing d = {True: "yes", 1: "one"}, what are len(d) and d[True]?

Understanding data types in Python Medium
A. 1 and "yes"
B. 2 and "yes"
C. 1 and "one"
D. 2 and "one"

28 What is the typical dtype of np.array([1, 2, np.nan])?

Understanding data types in Python Medium
A. int64
B. float64
C. object
D. bool

29 Let s1 = pd.Series([1, 2], index=["a", "b"]) and s2 = pd.Series([10, 20], index=["b", "c"]). What does s1 + s2 produce?

Pandas for data analysis Medium
A. a: 1, b: 12, c: 20
B. a: 11, b: 22, c: NaN
C. a: NaN, b: 12, c: NaN
D. a: 11, b: 22

30 A DataFrame has team = ["A", "A", "B"] and score = [10, 20, 30]. What does df.groupby("team")["score"].mean() return?

Pandas for data analysis Medium
A. A: 30, B: 30
B. A: 20, B: 30
C. A: 10, B: 25
D. A: 15, B: 30

31 Given s = pd.Series([10, 20, 30], index=["a", "b", "b"]), what does s.loc["b"] return?

Data selection in Series Medium
A. A Series containing 20 and 30
B. The scalar value 20
C. The scalar value 30
D. A Series containing 10 and 20

32 Given s = pd.Series([10, 20, 30, 40], index=["a", "b", "c", "d"]), what does s.iloc[1:3] select?

Data selection in Series Medium
A. Values 20, 30, and 40
B. Values 10 and 20
C. Values 20 and 30
D. Values 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?

Data selection in DataFrame Medium
A. A scalar containing the value 5
B. A DataFrame with B=6 and C=7
C. A Series with A=5 and B=6
D. A Series with A=5 and C=7

34 If a DataFrame has columns ordered as A, B, and C, what is the column order in df[["C", "A"]]?

Data selection in DataFrame Medium
A. A, B, then C
B. A, then C
C. C, then A
D. C, B, then A

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?

Data indexing and selection Medium
A. [10, 20]
B. [20, 30]
C. [10]
D. [20]

36 What is the result of pd.Series([0, None, np.nan, ""]).isna().tolist()?

Missing data in Pandas Medium
A. [True, True, True, False]
B. [False, True, True, False]
C. [False, True, False, True]
D. [False, False, True, True]

37 Rows r1, r2, and r3 contain respectively 2, 1, and 3 non-missing values. Which rows remain after df.dropna(thresh=2)?

Missing data in Pandas Medium
A. r1 and r3
B. Only r3
C. r2 and r3
D. Only r1

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"))?

Handling missing data Medium
A. [10, 10, 30, 30]
B. [10, 20, 20, 30]
C. [10, 30, 10, 30]
D. [10, NaN, NaN, 30]

39 What is the result of pd.Series([1, np.nan, np.nan, 4]).ffill(limit=1).tolist()?

Handling missing data Medium
A. [1, 1, 1, 4]
B. [1, NaN, 4, 4]
C. [1, 4, 4, 4]
D. [1, 1, NaN, 4]

40 What values result from pd.Series([2, np.nan, np.nan, 8]).interpolate(method="linear")?

Handling missing data Medium
A. [2, 2, 2, 8]
B. [2, 5, 5, 8]
C. [2, 4, 6, 8]
D. [2, 3, 4, 8]

41 What does the following code print?

PYTHON
def collect(x, bucket=[]):
    bucket.append(x)
    return bucket

a = collect(1)
b = collect(2)
a.append(3)
print(b)

Introduction to Python Hard
A. [1, 2, 3]
B. [1, 2]
C. [2, 3], because each invocation creates a fresh default list before returning it
D. [2, 3]

42 Given the following code, what is the final value of a[0]?

PYTHON
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

NumPy Hard
A. array([0, 88, 2, 3])
B. array([0, 99, 88, 3])
C. array([0, 88, 2, 99])
D. array([0, 99, 2, 3])

43 What does np.result_type(np.int32, np.float32) return under NumPy's standard type-promotion rules?

Understanding data types in Python Hard
A. dtype('int32')
B. dtype('object')
C. dtype('float64')
D. dtype('float32')

44 What is produced by the final expression?

PYTHON
import numpy as np

a = np.array(['1', '22'], dtype='<U2')
a[0] = '333'
a.tolist()

Understanding data types in Python Hard
A. ['33', '22']
B. ['333', '22']
C. A ValueError is raised because the assigned string exceeds the fixed-width Unicode data type
D. ['3', '22']

45 What is the result of the final expression?

PYTHON
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()

Pandas for data analysis Hard
A. [NaN, 12.0, NaN]
B. [1.0, 12.0, 20.0]
C. [0.0, 12.0, 0.0]
D. [1.0, 10.0, 20.0]

46 For the following Series, which pair of index lists is returned?

PYTHON
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)

Data indexing and selection Hard
A. ([4, 6, 8], [4, 6, 8])
B. ([4, 6], [4, 6])
C. ([4, 6, 8], [4, 6])
D. ([6, 8], [2, 4, 6])

47 What is the result of the final expression?

PYTHON
import pandas as pd

s = pd.Series([10, 20, 30], index=[1, 3, 5])
(s[1], s.iloc[1])

Data selection in Series Hard
A. (10, 20)
B. (20, 20)
C. (10, 10)
D. A KeyError is raised because integer keys can only be interpreted as positional selectors

48 What is the final value of s.tolist()?

PYTHON
import pandas as pd

s = pd.Series([1, 2, 3], index=['a', 'a', 'b'])
s.loc['a'] = 0

Data selection in Series Hard
A. [1, 0, 3]
B. [0, 2, 3]
C. [0, 0, 3]
D. [0, 3]

49 What does the final expression return?

PYTHON
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()

Data selection in DataFrame Hard
A. [3, 1]
B. [1, 3]
C. [1, 2]
D. [2, 3]

50 What is returned by the final expression?

PYTHON
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()

Data selection in DataFrame Hard
A. [0, 1]
B. [0, 1, 2, 3]
C. [1, 2]
D. [0, 2]

51 Given duplicate column labels, what does the final expression produce?

PYTHON
import pandas as pd

df = pd.DataFrame([[1, 2, 3]], columns=['x', 'x', 'y'])
(type(df['x']).__name__, df['x'].shape)

Data selection in DataFrame Hard
A. ('Series', (1,))
B. ('DataFrame', (1, 2))
C. ('DataFrame', (2, 1))
D. ('Series', (2,))

52 Under Pandas' nullable Boolean logic, what is the result?

PYTHON
import pandas as pd

s = pd.Series([True, pd.NA, False], dtype='boolean')
(s & False).tolist()

Missing data in Pandas Hard
A. [False, False, False]
B. [False, <NA>, False]
C. [True, <NA>, False]
D. A TypeError is raised because pd.NA cannot participate in any Boolean operation

53 What does the final expression return under the default Pandas missing-value rules?

PYTHON
import numpy as np
import pandas as pd

s = pd.Series([np.nan, None, pd.NA, np.inf], dtype='object')
pd.isna(s).tolist()

Missing data in Pandas Hard
A. [False, True, True, False]
B. [True, True, True, False]
C. [True, False, True, False]
D. [True, True, True, True]

54 Which index remains after the final operation?

PYTHON
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()

Handling missing data Hard
A. ['r1']
B. ['r2']
C. ['r1', 'r2']
D. ['r0']

55 Which description matches the result?

PYTHON
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)

Pandas for data analysis Hard
A. 'a' maps to 1.0, 'b' maps to NaN, and the missing key maps to 3.0 because all keys are retained by default
B. 'a' maps to NaN, 'b' maps to NaN, and the missing key is included
C. 'a' maps to 1.0, 'b' maps to NaN, and the missing key is omitted
D. '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()?

PYTHON
import numpy as np
import pandas as pd

s = pd.Series([1.0, np.nan, np.nan, 4.0, np.nan])

Handling missing data Hard
A. [1.0, 1.0, 1.0, 4.0, 4.0]
B. [1.0, NaN, 1.0, 4.0, 4.0]
C. [1.0, 1.0, NaN, 4.0, 4.0]
D. [1.0, 1.0, NaN, 4.0, NaN]

57 What does the interpolation produce?

PYTHON
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()

Handling missing data Hard
A. [NaN, 1.0, NaN, NaN, 4.0, NaN]
B. [NaN, 1.0, 2.0, 3.0, 4.0, NaN]
C. [1.0, 1.0, 2.0, 3.0, 4.0, 4.0]
D. [NaN, 1.0, 2.5, 2.5, 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?

NumPy Hard
A. a + b
B. a + b[None, :]
C. a + np.resize(b, a.shape), which repeatedly tiles the values in flattened element order
D. a + b[:, None]

59 What is the result of the final expression?

PYTHON
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])

NumPy Hard
A. (False, -1)
B. (False, 0)
C. (True, 0)
D. (True, -1)

60 What is df['x'].tolist() after the aligned assignment?

PYTHON
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

Data indexing and selection Hard
A. [20.0, NaN, 10.0]
B. [10.0, NaN, 20.0]
C. [10.0, 20.0, NaN]
D. [20.0, 10.0, 0.0]