Python full mock exam
15 questions sampled across all 5 modules. Timed like the real thing: answer everything, then review the explanations.
Why can a working notebook fail for someone else?
- Version differences only
- Cells may have been run out of order
- Jupyter is unreliable
- Missing internet
Answer: Cells may have been run out of order — Restart & Run All is the check that catches it.
'10' + '5' returns:
- 15
- '105'
- An error
- 105
Answer: '105' — String concatenation, not addition. Convert first.
In pd.DataFrame({'a': [1,2]}), the key 'a' becomes:
- A row
- A column
- An index
- A data type
Answer: A column — Dictionary keys become column names.
How do you stop 07030 becoming 7030?
- astype(int)
- dtype={'zip': str} when reading
- parse_dates
- encoding="utf-8"
Answer: dtype={'zip': str} when reading — Read identifier-like columns as strings.
An integer column showing float64 usually indicates:
- A bug
- Missing values are present
- Large numbers
- Wrong encoding
Answer: Missing values are present — NaN forces the column to float.
df[df['a'] > 1 and df['b'] < 5] raises an error because:
- a is not numeric
- pandas requires & rather than and
- Brackets are wrong
- It needs .loc
Answer: pandas requires & rather than and — The "truth value is ambiguous" error means and was used instead of &.
mean() on 100 values with 40 missing divides by:
- 100
- 60
- 40
- It errors
Answer: 60 — Missing values are skipped entirely.
To keep the most recent record per email:
- drop_duplicates() alone
- Sort by date, then drop_duplicates(keep="last")
- dropna()
- groupby(email)
Answer: Sort by date, then drop_duplicates(keep="last") — Without sorting, which row survives is arbitrary.
str.split(" ", n=1, expand=True) returns:
- A list
- A DataFrame of the split parts
- A string
- A Series of lists
Answer: A DataFrame of the split parts — expand=True gives columns you can assign directly.
Which keeps the original row count?
- agg
- transform
- sum
- count
Answer: transform — transform aligns the result back onto every row, like a window function.
df['x'].rank() on tied values gives, by default:
- Sequential integers
- The average rank, e.g. 2.5
- The lowest rank
- An error
Answer: The average rank, e.g. 2.5 — method="min" or "dense" match SQL behaviour.
Which argument raises an error if the relationship is not as expected?
- indicator
- validate
- suffixes
- how
Answer: validate — validate='many_to_one' checks the key relationship.
Why call savefig before show?
- It is faster
- show clears the figure, producing a blank file
- savefig needs the window
- Order does not matter
Answer: show clears the figure, producing a blank file — A classic cause of empty PNGs.
Why must a bar chart y-axis start at zero?
- Convention
- Bar length encodes value, so truncation misleads
- It errors otherwise
- For colour scaling
Answer: Bar length encodes value, so truncation misleads — A truncated axis exaggerates small differences.
Which shows median, quartiles and outliers per category?
- Histogram
- Box plot
- Scatter
- Heatmap
Answer: Box plot — Box plots compare distributions across categories.