Question Detail

COS 102 Python strings Objective Question

Question

You try to execute the Python code: result = "Age: " + 20. What kind of error would you expect, and why?
Options
A A SyntaxError, because you cannot use '+' with strings.
B A ValueError, because 20 is not a valid string.
C A TypeError, because you cannot directly concatenate a string and a number.
Correct Answer
D No error, the output would be 'Age: 20'.
Correct Answer

Option C is the correct answer.

Detailed Explanation

The 'Avoid the TypeError' section (page 10) explicitly states: 'Mixing Types: You cannot add a string and a number directly. "Age: " + 20 → Error!'. This results in a TypeError.

Hint

The document has a specific section about avoiding a particular type of error when mixing strings and numbers.

Question Info