Data Science Basics with Python
- 17Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What is the primary advantage of using a NumPy array over a Python list for numerical data?
What is the primary advantage of using a NumPy array over a Python list for numerical data?
17 questions
- 1
What is the primary advantage of using a NumPy array over a Python list for numerical data?
- A
NumPy arrays have a fixed size, while lists can grow dynamically.
- B
NumPy arrays can store mixed data types, while lists cannot.
- C
NumPy arrays support element-wise mathematical operations, while lists do not.
- D
NumPy arrays automatically sort data, while lists require manual sorting.
Correct answer
CNumPy arrays support element-wise mathematical operations, while lists do not.
Explanation
NumPy arrays allow element-wise arithmetic (e.g., multiplying each element by 2), whereas multiplying a Python list duplicates the list. This makes NumPy superior for numerical computations.
- A
- 2
In a NumPy array with zero-based indexing, how do you access the third element?
- A
Use index 1
- B
Use index 4
- C
Use index 3
- D
Use index 2
Correct answer
DUse index 2
Explanation
Zero-based indexing means the first element is at index 0, second at 1, third at 2.
- A
- 3
Which method is used to create a pandas DataFrame that resembles a table?
- A
plt.figure()
- B
pd.DataFrame()
- C
np.array()
- D
sns.load_dataset()
Correct answer
Bpd.DataFrame()
Explanation
The pd.DataFrame() constructor creates a tabular data structure from dictionaries, lists, or arrays. NumPy arrays and Matplotlib figures are not DataFrames.
- A
- 4
How do you access the entire row with label index 5 in a pandas DataFrame?
- A
df.loc[5]
- B
df.iloc[5]
- C
df.get(5)
- D
df[5]
Correct answer
Adf.loc[5]
Explanation
The .loc method accesses rows by label index. .iloc uses integer position. Square brackets alone on a DataFrame select columns, not rows.
- A
- 5
Which method is used to filter rows in a pandas DataFrame where a column equals a specific value?
- A
df[df['column'] == value]
- B
df.filter(column=value)
- C
df.where(column=value)
- D
df.select(column=value)
Correct answer
Adf[df['column'] == value]
Explanation
Boolean indexing with df[condition] is the standard way. .filter() works on column names, .select() is not a pandas method, and .where() preserves shape and masks other rows.
- A
- 6
What does the .query() method in pandas require when filtering string columns?
- A
The string value must be preceded by an @ symbol.
- B
The .query() method cannot filter on string columns.
- C
The string value must be passed as a variable without quotes.
- D
The string value must be enclosed in quotes inside the query string.
Correct answer
DThe string value must be enclosed in quotes inside the query string.
Explanation
In .query(), string literals must be quoted (e.g., df.query("name == 'Vicky'")). Using @ references Python variables.
- A
- 7
Which of the following is a common method to handle missing values (NaN) in a pandas DataFrame?
- A
Replacing NaN with zero by default
- B
Filling missing values with the mean of the column using fillna()
- C
Automatically ignoring rows with missing values during analysis
- D
Using the .dropna() method to remove all rows with any missing value
Correct answer
BFilling missing values with the mean of the column using fillna()
Explanation
fillna() can fill missing values with a specified value like the mean. dropna() removes rows with missing values, but it is not the only method. The column may have NaN after import, not automatically handled.
- A
- 8
When adding a new column to a pandas DataFrame using pd.concat(), what axis parameter indicates adding a column?
- A
axis=0
- B
axis='row'
- C
axis=1
- D
axis='column'
Correct answer
Caxis=1
Explanation
In pandas, axis=0 refers to rows and axis=1 refers to columns. To add a new column (Series) to a DataFrame, you use axis=1.
- A
- 9
What does the following code do? df.groupby('column')['numeric'].mean()
- A
Groups by 'numeric' and computes the mean of 'column' for each group.
- B
Computes the mean of all columns grouped by 'column'.
- C
Groups by 'column' and computes the mean of 'numeric' for each group.
- D
Returns the mean of 'numeric' ignoring groups.
Correct answer
CGroups by 'column' and computes the mean of 'numeric' for each group.
Explanation
The code first groups by 'column', then selects the 'numeric' column and calculates its mean within each group. It does not compute means of all columns.
- A
- 10
In Matplotlib, what does the term 'figure' refer to?
- A
A single plot or chart within the window.
- B
The area inside the axes where data is plotted.
- C
The legend or title of the chart.
- D
The entire window or page that can contain one or more plots.
Correct answer
DThe entire window or page that can contain one or more plots.
Explanation
A figure is the top-level container that holds all plot elements, including multiple axes (subplots). An axes is one plot area.
- A
- 11
How do you create a bar chart with student names on the x-axis and grades on the y-axis using Matplotlib?
- A
plt.hist(x=df['name'], bins=df['grade'])
- B
plt.plot(x=df['name'], y=df['grade'])
- C
plt.scatter(x=df['name'], y=df['grade'])
- D
plt.bar(x=df['name'], height=df['grade'])
Correct answer
Dplt.bar(x=df['name'], height=df['grade'])
Explanation
plt.bar() expects two arguments: x (categories) and height (values). plt.plot() makes a line plot, plt.scatter() makes a scatter plot, and plt.hist() makes a histogram.
- A
- 12
What does a histogram show about a dataset?
- A
The quartiles and outliers using a box.
- B
The distribution of a continuous variable by binning values and counting frequencies.
- C
The proportion of categories as slices of a circle.
- D
The relationship between two variables with points.
Correct answer
BThe distribution of a continuous variable by binning values and counting frequencies.
Explanation
A histogram divides the data range into bins and shows the count of observations in each bin, revealing the distribution shape.
- A
- 13
In a box plot, what does the box represent?
- A
The mode and median of the data.
- B
The entire range from minimum to maximum.
- C
The interquartile range (IQR), containing the middle 50% of the data.
- D
The mean and standard deviation of the data.
Correct answer
CThe interquartile range (IQR), containing the middle 50% of the data.
Explanation
The box spans from the first quartile (Q1) to the third quartile (Q3), covering 50% of the data. The whiskers extend to the min and max (excluding outliers).
- A
- 14
What does a standard deviation of 3.4 signify in the context of a normal distribution?
- A
The mean is 3.4 units from zero.
- B
Approximately 68% of the data falls within 3.4 units above or below the mean.
- C
Approximately 95% of the data falls within 3.4 units of the mean.
- D
The data is uniformly distributed around the mean.
Correct answer
BApproximately 68% of the data falls within 3.4 units above or below the mean.
Explanation
For a normal distribution, about 68% of data lies within one standard deviation of the mean. 95% lies within two standard deviations.
- A
- 15
Which of the following best describes the correlation coefficient?
- A
A value between -1 and 1 indicating the strength and direction of a linear relationship.
- B
The p-value from a hypothesis test.
- C
A value that must be positive to indicate a relationship.
- D
The slope of the regression line.
Correct answer
AA value between -1 and 1 indicating the strength and direction of a linear relationship.
Explanation
Correlation coefficients range from -1 (perfect negative) to 1 (perfect positive). Zero means no linear relationship. It is not the slope, and it can be negative.
- A
- 16
In linear regression, what does the slope (m) represent?
- A
The correlation between X and Y.
- B
The proportion of variance explained by the model.
- C
The value of Y when X is zero.
- D
The change in the predicted Y for a one-unit change in X.
Correct answer
DThe change in the predicted Y for a one-unit change in X.
Explanation
The slope m indicates how much Y changes on average when X increases by one unit. The intercept b is Y when X=0.
- A
- 17
Using the regression model derived from the data (m=6.31, b=-17.92), what is the predicted grade for a student who studied for 14 hours?
- A
Approximately 98
- B
Approximately 84
- C
Approximately 56
- D
Approximately 70
Correct answer
DApproximately 70
Explanation
Prediction = mX + b = 6.3114 + (-17.92) = 88.34 - 17.92 = 70.42, about 70.
- A

Ask anything
We've read this whole study set, so questions about it get the best answers.