Bird
0
0

Find the error in this code snippet:

medium📝 Debug Q14 of 15
Python - Variables and Dynamic Typing
Find the error in this code snippet:
x = 5
x = 'five'
print(x + 2)
ANo error, output is 'five2'
BNameError because x is undefined
CTypeError because you cannot add string and int
DSyntaxError due to reassignment
Step-by-Step Solution
Solution:
  1. Step 1: Analyze variable types and operation

    x is first an int (5), then a string ('five'). The print statement tries to add x + 2, which is string + int.
  2. Step 2: Understand Python's type rules

    Python does not allow adding string and int directly, causing a TypeError.
  3. Final Answer:

    TypeError because you cannot add string and int -> Option C
  4. Quick Check:

    Adding string + int causes TypeError [OK]
Quick Trick: Cannot add different types like string and int directly [OK]
Common Mistakes:
MISTAKES
  • Thinking Python auto-converts types in addition
  • Expecting concatenation without conversion
  • Ignoring type mismatch errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes