1. Home
  2. Subjects
  3. Data Science

Data Science MCQs · Page 21

1,613 multiple choice questions with answers across 28 topics.

Showing 501–525 of 1,613

df.drop_duplicates(subset=['email'], keep='last') is applied to a frame with repeated emails. What is retained?

  1. The last row of the frame only, because keep applies to the whole frame rather than rows
  2. Both the first and last occurrences, since keep='last' keeps the boundary rows intact
  3. The last occurrence of each email in the current row order, dropping the earlier ones Correct answer
  4. The first occurrence of each email address, because keep names the copy inspected
MCQSEXAM Read explanation

A column holds integers and NaN. Why does astype('int64') fail, and what is the usual fix?

  1. The cast truncates NaN to zero, raising a warning rather than an error
  2. The fix is to cast to float16 first, because float16 supports missing values natively
  3. int64 has no NaN representation, so the nullable Int64 dtype must be used instead Correct answer
  4. int64 holds NaN perfectly well, but the cast is refused because of an unrelated mismatch
MCQSEXAM Read explanation

df.resample('M').sum() is called on a DataFrame whose date lives in an ordinary string column rather than the index. What happens?

  1. The call groups by calendar month names, treating the strings as categorical data
  2. An empty frame is returned, because no rows fall on the month-end timestamps
  3. A TypeError about the index, because resample needs a DatetimeIndex on the axis Correct answer
  4. The string column is parsed automatically and the monthly totals are returned
MCQSEXAM Read explanation

A categorical column with 20 categories is grouped, but only 3 categories occur in the data. Why does observed=True shrink the result?

  1. Because observed=True drops rows whose category does not appear among the group keys chosen
  2. Because grouping a categorical converts it to an object column, losing category metadata
  3. Because observed=True sorts the categories and then truncates the output index
  4. Because the default expands every category combination, producing empty groups too Correct answer
MCQSEXAM Read explanation

A skewed income column must be split into four groups of equal count. Which function does that, and what does the other do instead?

  1. qcut, which uses sample quantiles, whereas cut splits on equal-width value ranges Correct answer
  2. cut, which uses sample quantiles, whereas qcut splits on equal-width value ranges
  3. cut, because both functions are identical except for the naming of their labels
  4. qcut, because it always produces equal-width bins regardless of the data distribution
MCQSEXAM Read explanation

df['city'].isin(['Lahore', 'Karachi']) is used as a row filter. What does it return, and how are NaN rows treated?

  1. A boolean Series aligned to the index, with NaN rows marked False unless NaN is listed Correct answer
  2. A boolean Series of identical length, with NaN rows marked True so every record is kept
  3. An integer array of row positions, because isin returns the matching row numbers
  4. A filtered DataFrame directly, since isin applies the mask as soon as it is called
MCQSEXAM Read explanation

A wide table with one column per month must become long, with month and value columns. Which call does that?

  1. df.transpose, which swaps rows and columns and therefore creates a long form table
  2. df.melt with id_vars, which turns the monthly columns into rows of key-value pairs Correct answer
  3. df.pivot with the month columns as index, which expands the columns into extra rows
  4. df.stack on axis=1, which returns a wide frame with the months kept as columns
MCQSEXAM Read explanation

df.pivot_table(index='dept', columns='month', values='sales') is called without aggfunc. What happens when several rows share a dept and month?

  1. The duplicate cells are averaged, because the default aggregation is the mean Correct answer
  2. A ValueError is raised, because duplicate entries need an explicit aggfunc
  3. The last row for each cell wins, because pivot_table keeps the final value
  4. The rows are concatenated into a list inside the cell, preserving every value
MCQSEXAM Read explanation

Which statement about TRUNCATE compared with DELETE is correct?

  1. TRUNCATE accepts a WHERE clause but is slower than DELETE, because it scans the whole table row by row
  2. Both fire row-level triggers once per removed row, so auditing behaves exactly the same for the two statements here
  3. DELETE cannot be rolled back in any database, while TRUNCATE always can, because it is DDL and not DML
  4. TRUNCATE removes all rows in one operation, cannot take a WHERE clause and fires no row-level delete triggers Correct answer
MCQSEXAM Read explanation

What does LAG(sales, 1, 0) OVER (PARTITION BY store ORDER BY month) return?

  1. The store's average sales, with 0 substituted wherever the partition is empty or has one row
  2. The next month's sales within the same store, with 0 substituted for the last row of each partition
  3. The running total of sales within each store, starting at 0 at the very first row of every partition here
  4. The previous month's sales within the same store, with 0 substituted for the first row of each partition Correct answer
MCQSEXAM Read explanation

Why can WHERE x NOT IN (SELECT y FROM t) return no rows when the subquery yields a NULL?

  1. Because NOT IN converts the NULL to zero and then compares numerically, matching every zero value
  2. Because x <> NULL is UNKNOWN, so the NOT IN predicate can never be TRUE; NOT EXISTS is null-safe Correct answer
  3. Because the subquery is executed only when it contains no NULL values, and otherwise the whole subquery is skipped
  4. Because NOT IN is not valid with a subquery at all, so the parser rejects the statement outright
MCQSEXAM Read explanation

Which statement contrasts UNION and UNION ALL correctly?

  1. UNION removes duplicate rows and therefore usually needs a sort or hash, while UNION ALL concatenates and keeps duplicates Correct answer
  2. Both remove duplicate rows, differing only in the column ordering they produce, so the choice between them is purely cosmetic
  3. UNION requires matching column names, while UNION ALL does not, which is why UNION ALL is faster to write
  4. UNION ALL removes duplicate rows, while UNION keeps them, so the two operators are exact opposites in effect
MCQSEXAM Read explanation

When GROUP BY runs on a column containing NULLs, how are those rows treated?

  1. The rows are excluded from the result entirely, because the grouping key is missing
  2. The query fails unless NULLs are filtered out first, since grouping needs a known key
  3. All NULLs form a single group, because GROUP BY treats them as equal to each other Correct answer
  4. Each NULL forms its own separate group, because NULL is never equal to another one
MCQSEXAM Read explanation

Counting customers by joining customers to orders and using COUNT(*) inflates the number. Why, and what fixes it?

  1. Because COUNT(*) ignores NULLs in the customer key, so rows with a missing key are never counted at all by the aggregate
  2. The one-to-many join repeats each customer row per order, so COUNT(DISTINCT customer_id) or a subquery gives the true count Correct answer
  3. Because the join sorts the rows and duplicates them during the sort, so the sort key must therefore be made distinct first of all
  4. Because COUNT(*) counts columns rather than rows, so it returns the width of the joined result instead of its height
MCQSEXAM Read explanation

A LEFT JOIN carries the right-table condition b.status = 'active' in the WHERE clause instead of the ON clause. What is the effect?

  1. Nothing changes, because the optimiser moves the predicate into the ON clause
  2. All inactive matches are kept while unmatched rows are duplicated
  3. The query fails because WHERE cannot reference the right table
  4. Rows with no matching b row are removed, turning the outer join into an effective inner join Correct answer
MCQSEXAM Read explanation

Values 100, 100, 90 are ranked in descending order. What do RANK, DENSE_RANK and ROW_NUMBER produce?

  1. 1,2,3 for all three functions, because every row receives a distinct sequential position
  2. 1,1,2 for RANK and 1,1,3 for DENSE_RANK, with 1,2,3 for ROW_NUMBER in the very same single query
  3. 1,1,3 for RANK; 1,1,2 for DENSE_RANK; and 1,2,3 for ROW_NUMBER with an arbitrary tie order Correct answer
  4. 1,1,3 for all three, since ties always skip a position whatever function is used
MCQSEXAM Read explanation

In SUM(x) OVER (ORDER BY d), the default window frame uses RANGE. What practical difference does that make compared with an explicit ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW?

  1. RANGE requires a PARTITION BY clause, whereas ROWS can be used over the whole table without one
  2. ROWS ignores the ORDER BY, whereas RANGE sorts the partition before summing
  3. Rows that tie on d are all included in the same running total under RANGE, while ROWS admits them one at a time Correct answer
  4. RANGE includes only the current row, whereas ROWS accumulates every preceding row in the partition
MCQSEXAM Read explanation

Which curve would you inspect to decide whether collecting more training data is likely to help?

  1. The precision-recall curve, which plots precision against recall at every threshold
  2. The validation curve, which plots scores against one hyperparameter value at a time
  3. The ROC curve, which plots the true positive rate against the false positive rate of the model
  4. The learning curve, which plots training and validation scores against training-set size Correct answer
MCQSEXAM Read explanation