🎩 Master Quiz: Type Casting
20 Questions. Prove you know the difference between implicit and explicit!
Green = Correct, Red = Incorrect.
What is the core definition of “Type Casting”?
💡 Explanation
Changing one data type into another.
Type Casting is the process of converting a variable from one type (like a String) to another (like an Integer).
In the “Travel Adapter” analogy, what represents Explicit Conversion?
💡 Explanation
The Physical Adapter.
Explicit conversion is manual. Just like you must buy and plug in an adapter to fit a US socket, you must use functions like int() to force the change.
What is the result of 10 + 2.5 in Python?
💡 Explanation
12.5 (Float).
This is Implicit Conversion. Python automatically promotes the Integer to a Float to prevent data loss.
Which function converts the string "500" to a number?
💡 Explanation
int("500").
int() is the constructor function used to manually convert a compatible string into an integer.
Why is type casting crucial for API responses?
💡 Explanation
Data arrives as Strings.
Even if a value is a number (like port 8080), APIs usually send it as a string "8080". You must cast it to use it in logic.
What happens when you run int(9.9)?
💡 Explanation
9 (Truncates).
Converting a float to an int results in Truncation. The decimal part is removed permanently, not rounded.
What is the result of int("10.5")?
💡 Explanation
ValueError (Crash).
Python cannot directly convert a string with a decimal point into an integer. You must use float() first, or it will crash.
How do you correctly convert "10.5" to the integer 10?
💡 Explanation
int(float("10.5"))
First convert the string to a float: float("10.5") -> 10.5. Then convert that float to an int: int(10.5) -> 10.
What is the result of bool("False")?
💡 Explanation
True.
Any non-empty string is evaluated as True in Python. This is the #1 bug in config scripts.
What is the correct way to check if a config string is “True”?
💡 Explanation
Comparison.
Do not cast. Compare the string value directly (and handle case sensitivity) to get a real boolean result.
What happens if you run int("Hello")?
💡 Explanation
ValueError.
Python is strict. You cannot cast “garbage” text that doesn’t look like a number into an integer.
Which library is recommended for secure type casting in modern stacks?
💡 Explanation
Pydantic.
Pydantic handles type casting and validation automatically using Python type hints, preventing common errors.
What happens if you run list("Hi")?
💡 Explanation
['H', 'i']
Casting a string to a list splits it into individual characters. It does not wrap the whole string in a list.
What is the result of float(5)?
💡 Explanation
5.0
The float() constructor adds the decimal precision, turning the integer 5 into 5.0.
Why must you use str() when writing numbers to a text file?
💡 Explanation
Text files expect strings.
Functions like file.write() expect string data. You must explicitly cast numbers to strings before writing.
Is type casting reversible without data loss? (e.g., float -> int -> float)
💡 Explanation
No.
Once you cast 9.9 to int (9), the .9 is gone forever. Casting it back to float gives 9.0.
Can you simply use list() to convert the string "[1, 2]" into a list?
💡 Explanation
No.
list() iterates over the string characters. To parse a string representation of a list, you need json.loads().
What is the result of bool("") (Empty String)?
💡 Explanation
False.
In Python, “empty” values like "", 0, and [] evaluate to False.
What is a key benefit of Type Casting?
💡 Explanation
Interoperability.
Type casting is the bridge that allows text from a file to be used as numbers in a calculation.
What is the result of str(100)?
💡 Explanation
“100” (String).
This explicitly converts the integer 100 into the text format “100”.