Notice: Due to an ongoing migration of our storage and publishing system, material requested since last month will not be uploaded for the time being, and new requests are paused. All existing notes remain available.
You're offline
Unit 13 MCQsNumPy and Pandas
ECAP792 ·
60 questions. Pick an option to check it.
Which symbol begins a single-line comment in Python?
Not quite. Try another option.
Answer: #
Python uses the # symbol to begin a single-line comment.
Question 2 of 60 · Introduction to Python · Easy
Which Python function displays output on the screen?
Not quite. Try another option.
Answer: print()
The print() function displays text and other values on the screen.
Question 3 of 60 · Understanding data types in Python · Easy
What is the Python data type of the value 25?
Not quite. Try another option.
Answer: int
25 is a whole number, so its data type is int.
Question 4 of 60 · Understanding data types in Python · Easy
Which Python data type stores either True or False?
Not quite. Try another option.
Answer: bool
The bool data type represents the logical values True and False.
Question 5 of 60 · Understanding data types in Python · Easy
Which built-in function returns the data type of a Python object?
Not quite. Try another option.
Answer: type()
The type() function returns the data type or class of an object.
Question 6 of 60 · NumPy · Easy
What is the conventional alias used when importing NumPy?
Not quite. Try another option.
Answer: np
NumPy is conventionally imported with import numpy as np.
Question 7 of 60 · NumPy · Easy
Which NumPy function creates an array from a Python list?
Not quite. Try another option.
Answer: np.array()
np.array() converts a Python list or similar sequence into a NumPy array.
Question 8 of 60 · NumPy · Easy
Which attribute gives the dimensions of a NumPy array?
Not quite. Try another option.
Answer: shape
The shape attribute reports the size of the array along each dimension.
Question 9 of 60 · NumPy · Easy
What does np.zeros(3) create?
Not quite. Try another option.
Answer: An array containing three zeros
np.zeros(3) creates a one-dimensional array with three zero values.
Question 10 of 60 · Pandas for data analysis · Easy
What is the conventional alias used when importing Pandas?
Not quite. Try another option.
Answer: pd
Pandas is conventionally imported with import pandas as pd.
Question 11 of 60 · Pandas for data analysis · Easy
Which Pandas object represents a one-dimensional labeled sequence?
Not quite. Try another option.
Answer: Series
A Pandas Series is a one-dimensional sequence with index labels.
Question 12 of 60 · Pandas for data analysis · Easy
Which Pandas object stores data in labeled rows and columns?
Not quite. Try another option.
Answer: DataFrame
A DataFrame is a two-dimensional labeled table with rows and columns.
Question 13 of 60 · Data indexing and selection · Easy
What is the index position of the first element in a Python sequence?
Not quite. Try another option.
Answer: 0
Python uses zero-based indexing, so the first element is at position 0.
Question 14 of 60 · Data indexing and selection · Easy
In Python slicing, what does data[1:4] select?
Not quite. Try another option.
Answer: Positions 1 through 3
A slice includes its starting position but excludes its stopping position.
Question 15 of 60 · Data selection in Series · Easy
Which Pandas accessor selects Series values by index label?
Not quite. Try another option.
Answer: .loc
The .loc accessor selects values using index labels.
Question 16 of 60 · Data selection in Series · Easy
Which Pandas accessor selects a Series value by integer position?
Not quite. Try another option.
Answer: .iloc
The .iloc accessor selects values using integer positions.
Question 17 of 60 · Data selection in DataFrame · Easy
If df is a DataFrame, which expression selects the column named age?
Not quite. Try another option.
Answer: df["age"]
Using df["age"] selects the DataFrame column whose label is age.
Question 18 of 60 · Data selection in DataFrame · Easy
Which expression selects the first row of a DataFrame named df by position?
Not quite. Try another option.
Answer: df.iloc[0]
Because positions begin at zero, df.iloc[0] selects the first row.
Question 19 of 60 · Missing data in Pandas · Easy
Which value commonly represents missing numerical data in Pandas?
Not quite. Try another option.
Answer: NaN
NaN, meaning "Not a Number," commonly represents missing numerical data.
Question 20 of 60 · Handling missing data · Easy
Which Pandas method replaces missing values with a specified value?
Not quite. Try another option.
Answer: .fillna()
The .fillna() method replaces missing entries with a chosen value.
Question 21 of 60 · Introduction to Python · Medium
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]?
Not quite. Try another option.
Answer: [1, 4, 3, 8]
The conditional expression doubles even values while leaving odd values unchanged.
Question 22 of 60 · Introduction to Python · Medium
Given a = [1, 2], b = a, and b.append(3), what are the final values of a and b?
Not quite. Try another option.
Answer: a = [1, 2, 3], b = [1, 2, 3]
a and b refer to the same mutable list, so modifying it through b also affects a.
Question 23 of 60 · NumPy · Medium
If NumPy arrays a and b have shapes (3, 1) and (1, 4), respectively, what is the shape of a + b?
Not quite. Try another option.
Answer: (3, 4)
Broadcasting expands the singleton dimensions so both arrays behave as if they had shape (3, 4).
Question 24 of 60 · NumPy · Medium
Consider a = np.arange(5), b = a[1:4], and b[0] = 99. What is the resulting value of a?
Not quite. Try another option.
Answer: [0, 99, 2, 3, 4]
A basic NumPy slice is usually a view, so changing b[0] changes the corresponding element of a.
Question 25 of 60 · NumPy · Medium
Given arr = np.array([[1, 4], [3, 2]]), what does arr[arr > 2] return?
Not quite. Try another option.
Answer: array([4, 3])
Boolean indexing returns matching elements as a one-dimensional array in row-major order.
Question 26 of 60 · NumPy · Medium
For x = np.array([[1, 2, 3], [4, 5, 6]]), what is the result of x.sum(axis=0)?
Not quite. Try another option.
Answer: array([5, 7, 9])
Using axis=0 collapses the rows and computes the sum of each column.
Question 27 of 60 · Understanding data types in Python · Medium
After executing d = {True: "yes", 1: "one"}, what are len(d) and d[True]?
Not quite. Try another option.
Answer: 1 and "one"
In Python, True compares equal to 1 and has the same hash, so the second assignment replaces the first value.
Question 28 of 60 · Understanding data types in Python · Medium
What is the typical dtype of np.array([1, 2, np.nan])?
Not quite. Try another option.
Answer: float64
np.nan is a floating-point value, so NumPy promotes the integer elements to a floating-point type.
Question 29 of 60 · Pandas for data analysis · Medium
Let s1 = pd.Series([1, 2], index=["a", "b"]) and s2 = pd.Series([10, 20], index=["b", "c"]). What does s1 + s2 produce?
Not quite. Try another option.
Answer: a: NaN, b: 12, c: NaN
Pandas aligns Series by label before adding. Only label b exists in both Series.
Question 30 of 60 · Pandas for data analysis · Medium
A DataFrame has team = ["A", "A", "B"] and score = [10, 20, 30]. What does df.groupby("team")["score"].mean() return?
Not quite. Try another option.
Answer: A: 15, B: 30
Team A's mean is , while team B has one score of .
Question 31 of 60 · Data selection in Series · Medium
Given s = pd.Series([10, 20, 30], index=["a", "b", "b"]), what does s.loc["b"] return?
Not quite. Try another option.
Answer: A Series containing 20 and 30
Because the label b occurs twice, label-based selection returns both matching entries as a Series.
Question 32 of 60 · Data selection in Series · Medium
Given s = pd.Series([10, 20, 30, 40], index=["a", "b", "c", "d"]), what does s.iloc[1:3] select?
Not quite. Try another option.
Answer: Values 20 and 30
iloc uses positional slicing, where position 1 is included and position 3 is excluded.
Question 33 of 60 · Data selection in DataFrame · Medium
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?
Not quite. Try another option.
Answer: A Series with A=5 and C=7
The first argument selects row r2, while the column list selects only columns A and C.
Question 34 of 60 · Data selection in DataFrame · Medium
If a DataFrame has columns ordered as A, B, and C, what is the column order in df[["C", "A"]]?
Not quite. Try another option.
Answer: C, then A
Selecting columns with a list preserves the order specified in that list.
Question 35 of 60 · Data indexing and selection · Medium
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?
Not quite. Try another option.
Answer: [20]
Only the second row satisfies both conditions: its A value exceeds 1 and its B value is below 30.
Question 36 of 60 · Missing data in Pandas · Medium
What is the result of pd.Series([0, None, np.nan, ""]).isna().tolist()?
Not quite. Try another option.
Answer: [False, True, True, False]
Pandas treats None and np.nan as missing, but it does not treat zero or an empty string as missing by default.
Question 37 of 60 · Missing data in Pandas · Medium
Rows r1, r2, and r3 contain respectively 2, 1, and 3 non-missing values. Which rows remain after df.dropna(thresh=2)?
Not quite. Try another option.
Answer: r1 and r3
thresh=2 keeps rows containing at least two non-missing values.
Question 38 of 60 · Handling missing data · Medium
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"))?
Not quite. Try another option.
Answer: [10, 10, 30, 30]
Each missing score is filled with its team's median: for team A and for team B.
Question 39 of 60 · Handling missing data · Medium
What is the result of pd.Series([1, np.nan, np.nan, 4]).ffill(limit=1).tolist()?
Not quite. Try another option.
Answer: [1, 1, NaN, 4]
Forward filling with limit=1 fills only the first consecutive missing value after 1.
Question 40 of 60 · Handling missing data · Medium
What values result from pd.Series([2, np.nan, np.nan, 8]).interpolate(method="linear")?
Not quite. Try another option.
Answer: [2, 4, 6, 8]
Linear interpolation spaces the two missing values evenly between and , producing and .
Question 41 of 60 · Introduction to Python · Hard
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)
Not quite. Try another option.
Answer: [1, 2, 3]
The default list is created once when the function is defined. Both a and b therefore reference the same mutable list.
Question 42 of 60 · NumPy · Hard
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
Not quite. Try another option.
Answer: array([0, 88, 2, 3])
Advanced indexing in b creates a copy, so its mutation does not affect a. Basic slicing in c creates a view, so assigning 88 changes a[0, 1].
Question 43 of 60 · Understanding data types in Python · Hard
What does np.result_type(np.int32, np.float32) return under NumPy's standard type-promotion rules?
Not quite. Try another option.
Answer: dtype('float64')
A float32 cannot represent the full range of int32 values with adequate precision, so NumPy promotes the combination to float64.
Question 44 of 60 · Understanding data types in Python · Hard
What is produced by the final expression?
Python
import numpy as np
a = np.array(['1', '22'], dtype='<U2')
a[0] = '333'
a.tolist()
Not quite. Try another option.
Answer: ['33', '22']
The <U2 dtype stores at most two Unicode characters per element. Assigning a longer string silently truncates it to '33'.
Question 45 of 60 · Pandas for data analysis · Hard
Because 'x' matches multiple columns, selecting it returns a DataFrame containing both matches rather than a Series.
Question 52 of 60 · Missing data in Pandas · Hard
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()
Not quite. Try another option.
Answer: [False, False, False]
Pandas uses three-valued Boolean logic. Since any Boolean value combined with False using AND is definitively False, even the missing element becomes False.
Question 53 of 60 · Missing data in Pandas · Hard
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()
Not quite. Try another option.
Answer: [True, True, True, False]
np.nan, None, and pd.NA are recognized as missing. Positive infinity is not considered missing by default.
thresh=2 requires two nonmissing values among only columns 'a' and 'b'. Only row 'r2' satisfies that requirement.
Question 55 of 60 · Pandas for data analysis · Hard
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)
Not quite. Try another option.
Answer: 'a' maps to 1.0, 'b' maps to NaN, and the missing key is omitted
groupby drops missing keys by default. min_count=1 makes an all-missing group such as 'b' sum to NaN instead of zero.
Question 56 of 60 · Handling missing data · Hard
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])
Not quite. Try another option.
Answer: [1.0, 1.0, NaN, 4.0, 4.0]
limit=1 fills at most one consecutive missing value after each observed value. The second missing value in the first gap therefore remains missing.
Question 57 of 60 · Handling missing data · Hard
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()
Not quite. Try another option.
Answer: [NaN, 1.0, 2.0, 3.0, 4.0, NaN]
Linear interpolation fills only the internal gap, yielding evenly spaced values 2.0 and 3.0. limit_area='inside' leaves both exterior missing values unchanged.
Question 58 of 60 · NumPy · Hard
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?
Not quite. Try another option.
Answer: a + b[:, None]
b[:, None] has shape (2, 1), which broadcasts across the three columns. Shapes (2, 3) and (2,) are not directly broadcast-compatible.
Question 59 of 60 · NumPy · Hard
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])
Not quite. Try another option.
Answer: (False, 0)
The transposed array is noncontiguous for this C-order reshape, so reshaping it to one dimension requires a copy. Mutating b therefore leaves a unchanged.
Question 60 of 60 · Data indexing and selection · Hard
What is df['x'].tolist() after the aligned assignment?
Nearly through. Did unit 13 of ECAP792 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 13 of ECAP792 save you a night before the exam?
Then you can help keep it free for the next batch. 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.