Guide
Python
Intermediate
8.16 Interview Questions
Frequently asked and scenario-based Python type conversion interview questions covering implicit vs explicit conversion, int(3.9), ValueError vs TypeError, and bool('False').
Conceptual Questions
- What’s the difference between implicit and explicit type conversion?
- Why does
int(3.9)return3instead of4? - What’s the difference between
ValueErrorandTypeErrorduring conversion? - Why is
bool("False")True? - Why does
input()always require explicit conversion for numeric use?
Scenario-Based Questions
- A script reads a config value
"true"from a file and needs it as a realbool— how would you convert it correctly? - An API sometimes returns a numeric field as a JSON string instead of a number — how would you defensively handle both cases?
- A CSV import is crashing on a few malformed rows — how would you make the numeric conversion resilient to bad data?
Coding Questions
- Write a
safe_float()function that returns a default value instead of raising on invalid input. - Write a function that correctly parses common boolean-like strings (
"true"/"false"/"yes"/"no") into actualboolvalues. - Given a list of mixed-type values, write code that converts only the ones that can be converted to
int, skipping the rest.
Quick Interview Answer
“The core conversion questions test whether implicit vs explicit conversion is understood clearly, whether
int()’s truncate-toward-zero behavior is known (not rounding), whetherValueErrorvsTypeErrorcan be distinguished by cause, and whether thebool(\"False\")gotcha is anticipated. The coding questions almost always come down to writing asafe_*wrapper aroundtry/except, or defensively normalizing a value that might arrive as either its real type or a string version of it.”
Common Mistakes
- Answering “
int(3.9)rounds to4” — it truncates toward zero to3; conflating truncation with rounding is one of the most common wrong answers here. - Writing a boolean-string parser using
bool(value)directly instead of comparing lowercased text against"true"— misses the entire point of the question. - Solving the “convert only what’s convertible” coding question with a bare
except:instead of the specific(ValueError, TypeError)pair.
Add More Questions to This Guide
Know a question that should be here? Share it and help the community!
Open Google Form