Question Detail

COS 102 lecture 4 Objective Question

Question

Consider the Python code: x = 5, y = "John", print(x + y). What will be the outcome when this code is executed?
Options
A The output will be '5John'.
B The output will be 10.
C It will result in a syntax error.
D It will result in a runtime error.
Correct Answer
Correct Answer

Option D is the correct answer.

Detailed Explanation

Page 34 explains, 'If you try to combine a string and a number, Python will give you an error.' This is a runtime error because the operation + is defined differently for numbers (addition) and strings (concatenation), and cannot implicitly handle a mix.

Hint

Look at the examples on page 34 regarding combining different data types with the '+' operator for output.

Question Info