Skip to main content

Python Type Casting Quiz

ยท 5 min read

Progress

Score: 0/20

๐ŸŽฉ Master Quiz: Type Casting

20 Questions. Prove you know the difference between implicit and explicit! Green = Correct, Red = Incorrect.

BeginnerDefinition

What is the core definition of "Type Casting"?โ€‹

  • **A) Changing the variable name**
  • **B) Changing one data type into another**
  • **C) Validating user input**
  • **D) Encrypting data**

๐Ÿ’ก 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).

BeginnerAnalogy

In the "Travel Adapter" analogy, what represents Explicit Conversion?โ€‹

  • **A) The Laptop**
  • **B) The Wall Socket**
  • **C) The Physical Plastic Adapter**
  • **D) The Voltage**

๐Ÿ’ก 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.

IntermediateImplicit

What is the result of 10 + 2.5 in Python?โ€‹

  • **A) 12 (Integer)**
  • **B) 12.5 (Float)**
  • **C) TypeError**
  • **D) "102.5" (String)**

๐Ÿ’ก Explanationโ€‹

12.5 (Float).

This is Implicit Conversion. Python automatically promotes the Integer to a Float to prevent data loss.

BeginnerSyntax

Which function converts the string "500" to a number?โ€‹

  • **A) int("500")``**
  • **B) num("500")``**
  • **C) str("500")``**
  • **D) change("500")``**

๐Ÿ’ก Explanationโ€‹

int("500").

int() is the constructor function used to manually convert a compatible string into an integer.

ArchitectAPI Reality

Why is type casting crucial for API responses?โ€‹

  • **A) APIs always send encrypted data**
  • **B) APIs send data as Integers only**
  • **C) Data usually arrives as JSON Strings**
  • **D) To save bandwidth**

๐Ÿ’ก 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.

AdvancedData Loss

What happens when you run int(9.9)?โ€‹

  • **A) 10 (Rounds up)**
  • **B) 9 (Truncates)**
  • **C) 9.9 (No change)**
  • **D) TypeError**

๐Ÿ’ก Explanationโ€‹

9 (Truncates).

Converting a float to an int results in Truncation. The decimal part is removed permanently, not rounded.

TrapFloat String

What is the result of int("10.5")?โ€‹

  • **A) 10**
  • **B) 10.5**
  • **C) ValueError (Crash)**
  • **D) 11**

๐Ÿ’ก 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.

SolutionFixing Errors

How do you correctly convert "10.5" to the integer 10?โ€‹

  • **A) int("10.5")``**
  • **B) int(float("10.5"))``**
  • **C) round("10.5")``**
  • **D) str(10.5)``**

๐Ÿ’ก 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.

TrapBoolean Trap

What is the result of bool("False")?โ€‹

  • **A) True**
  • **B) False**
  • **C) None**
  • **D) TypeError**

๐Ÿ’ก Explanationโ€‹

True.

Any non-empty string is evaluated as True in Python. This is the #1 bug in config scripts.

SolutionBoolean Fix

What is the correct way to check if a config string is "True"?โ€‹

  • **A) bool(config_value)``**
  • **B) int(config_value)``**
  • **C) config_value.lower() == "true"``**
  • **D) str(config_value)``**

๐Ÿ’ก Explanationโ€‹

Comparison.

Do not cast. Compare the string value directly (and handle case sensitivity) to get a real boolean result.

IntermediateStrictness

What happens if you run int("Hello")?โ€‹

  • **A) It returns 0**
  • **B) ValueError (Crash)**
  • **C) It returns None**
  • **D) It converts ASCII codes**

๐Ÿ’ก Explanationโ€‹

ValueError.

Python is strict. You cannot cast "garbage" text that doesn't look like a number into an integer.

ArchitectValidation Tool

  • **A) NumPy**
  • **B) Pandas**
  • **C) PyTest**
  • **D) Pydantic**

๐Ÿ’ก Explanationโ€‹

Pydantic.

Pydantic handles type casting and validation automatically using Python type hints, preventing common errors.

MechanicsList Casting

What happens if you run list("Hi")?โ€‹

  • **A) ['H', 'i']``**
  • **B) ["Hi"]``**
  • **C) Error**
  • **D) [72, 105]``**

๐Ÿ’ก Explanationโ€‹

['H', 'i']

Casting a string to a list splits it into individual characters. It does not wrap the whole string in a list.

MechanicsFloat Constructor

What is the result of float(5)?โ€‹

  • **A) 5**
  • **B) "5.0"**
  • **C) 5.0**
  • **D) 5f**

๐Ÿ’ก Explanationโ€‹

5.0

The float() constructor adds the decimal precision, turning the integer 5 into 5.0.

Use CaseFile Handling

Why must you use str() when writing numbers to a text file?โ€‹

  • **A) Files only accept integers**
  • **B) Text files expect strings**
  • **C) To encrypt the number**
  • **D) It saves disk space**

๐Ÿ’ก Explanationโ€‹

Text files expect strings.

Functions like file.write() expect string data. You must explicitly cast numbers to strings before writing.

CoreIrreversibility

Is type casting reversible without data loss? (e.g., float -> int -> float)โ€‹

  • **A) Yes, always**
  • **B) No, data (like decimals) is lost**
  • **C) Yes, if you use Pydantic**
  • **D) Only for Strings**

๐Ÿ’ก Explanationโ€‹

No.

Once you cast 9.9 to int (9), the .9 is gone forever. Casting it back to float gives 9.0.

TrapComplex Types

Can you simply use list() to convert the string "[1, 2]" into a list?โ€‹

  • **A) Yes, it works automatically**
  • **B) No, it splits it into chars like '['**
  • **C) Yes, but only for integers**
  • **D) It crashes**

๐Ÿ’ก Explanationโ€‹

No.

list() iterates over the string characters. To parse a string representation of a list, you need json.loads().

SyntaxEmpty Bool

What is the result of bool("") (Empty String)?โ€‹

  • **A) True**
  • **B) False**
  • **C) None**
  • **D) Error**

๐Ÿ’ก Explanationโ€‹

False.

In Python, "empty" values like "", 0, and [] evaluate to False.

BenefitInteroperability

What is a key benefit of Type Casting?โ€‹

  • **A) Allows different data sources to talk to each other**
  • **B) It speeds up the CPU**
  • **C) It encrypts the data**
  • **D) It prevents variables from changing**

๐Ÿ’ก Explanationโ€‹

Interoperability.

Type casting is the bridge that allows text from a file to be used as numbers in a calculation.

SyntaxStr Constructor

What is the result of str(100)?โ€‹

  • **A) 100 (Int)**
  • **B) 100.0 (Float)**
  • **C) "100" (String)**
  • **D) True**

๐Ÿ’ก Explanationโ€‹

"100" (String).

This explicitly converts the integer 100 into the text format "100".

๐ŸŽ‰ Quiz Complete!โ€‹

Your Final Score:

0/20 Novice

Keep practicing to reach Architect level!

  • **โ†บ Retake Quiz**