Python Variable Casting Essentials Quiz — Questions & Answers

Test your knowledge of Python variable casting with these practical questions about type conversion, implicit and explicit casting, and best practices. Strengthen your understanding of converting between data types such as integers, floats, strings, and booleans in Python.

This quiz contains 10 questions. Below is a complete reference of all questions, answer choices, and correct answers. You can use this section to review after taking the interactive quiz above.

  1. Question 1: Implicit Casting with Arithmetic

    In the expression result = 7 + 3.5, what will be the type of 'result' after this calculation in Python?

    • string
    • int
    • boolean
    • float
    Show correct answer

    Correct answer: float

    Explanation: When adding an integer and a float in Python, the integer is implicitly converted to a float, making the result a float. The 'int' option is incorrect because the presence of a float in the operation causes Python to cast the result as a float. 'string' and 'boolean' are unrelated to this arithmetic operation and are incorrect.

  2. Question 2: Explicit Casting to Integer

    What is the value of x after executing x = int(8.9) in Python?

    • '8.9'
    • 8
    • 9
    • 8.9
    Show correct answer

    Correct answer: 8

    Explanation: The int() function in Python truncates the decimal part and returns the whole number, so int(8.9) yields 8. Option '9' is incorrect because int() does not round; it always truncates. '8.9' and the string representation '8.9' are both wrong because the result is an integer, not a float or a string.

  3. Question 3: Type Conversion to String

    Which of the following is the correct output of str(123)?

    • one two three
    • '123'
    • "123.0"
    • 123
    Show correct answer

    Correct answer: '123'

    Explanation: The str() function converts the integer 123 to the string '123'. The option 123 represents an integer, not a string. 'one two three' is not a built-in conversion result. '"123.0"' would only be obtained if converting a float, not an integer, and still wrapped as a string.

  4. Question 4: Boolean Casting of Integers

    What is the output of print(bool(0)) in Python?

    • Zero
    • True
    • False
    • None
    Show correct answer

    Correct answer: False

    Explanation: In Python, the integer 0 is considered False when cast to a boolean. Option 'True' is incorrect because any non-zero number would be True, but 0 is a special case. 'None' and 'Zero' are not valid boolean outputs in Python, making them incorrect.

  5. Question 5: Casting a String to Float

    If num = '15.3', what is the result of float(num)?

    • 15
    • 15.3
    • Error
    • '15.3'
    Show correct answer

    Correct answer: 15.3

    Explanation: The float() function successfully converts a correctly formatted numeric string to a floating-point number, so float('15.3') yields 15.3. Option ''15.3'' is a string, not a float. '15' is an integer, not the result of this conversion. 'Error' would only result if the string could not be interpreted as a numeric value.

  6. Question 6: Invalid Conversion Scenario

    What happens if you try to convert the string 'abc' to int using int('abc') in Python?

    • 'abc'
    • Error
    • None
    • 0
    Show correct answer

    Correct answer: Error

    Explanation: Attempting to convert a non-numeric string like 'abc' to int raises a ValueError in Python. The option '0' is not returned in this case unless the string was '0'. ''abc'' is just the original string, not the conversion result. 'None' would only be returned if explicitly coded, not by the int() function.

  7. Question 7: Implicit vs Explicit Casting

    Which statement best describes implicit casting in Python?

    • It always requires user input.
    • It can only be done with strings.
    • Python automatically changes a smaller type to a larger type where needed.
    • It happens when you manually use a built-in function.
    Show correct answer

    Correct answer: Python automatically changes a smaller type to a larger type where needed.

    Explanation: Implicit casting refers to Python's automatic type conversion, such as turning an int into a float during mixed operations. The option about manually using built-in functions describes explicit casting, not implicit. Implicit casting does not require user input, and it is not limited to strings.

  8. Question 8: Data Loss Warning

    Which type of variable casting in Python can result in the loss of information when converting between data types?

    • Casting from int to float
    • Casting from string to bool
    • Casting from float to int
    • Casting from bool to float
    Show correct answer

    Correct answer: Casting from float to int

    Explanation: Casting from float to int in Python truncates the decimal part, leading to potential loss of precision. Casting from int to float doesn't lose information as int values have exact float equivalents. Converting from string to bool or bool to float doesn't usually cause data loss, as they follow specific logical rules.

  9. Question 9: Best Practices in Casting

    Why is it important to validate user input before casting in Python?

    • To prevent runtime casting errors
    • To avoid variable assignment
    • To make every input True
    • To improve code speed
    Show correct answer

    Correct answer: To prevent runtime casting errors

    Explanation: Validating user input helps ensure that the data can be safely cast to a specific type, preventing runtime errors like ValueError. Improving code speed isn't the main concern here. Making every input True relates to boolean logic, not validation. Avoiding variable assignment is unrelated to the context.

  10. Question 10: Boolean Conversion of Strings

    What is the boolean result of bool('False') in Python?

    • Error
    • True
    • None
    • False
    Show correct answer

    Correct answer: True

    Explanation: In Python, any non-empty string, including 'False', is converted to True when cast with bool(). 'False' would only be returned if the string were empty or explicitly set as False. 'Error' and 'None' are not the result of boolean casting a non-empty string.