PCEP-30-02 · Question #263
What is the expected output of the following code if the user enters 2 and 4? ``python x = int(input()) y = int(input()) print(x + y) ``
The correct answer is A. 6. Option A (6) is correct because int(input()) converts each entered string to an integer, so x = 2 and y = 4, and print(x + y) performs integer addition: 2 + 4 = 6. Option D (24) is the most tempting wrong answer - it's what you'd get if the inputs were treated as strings and…
Question
x = int(input())
y = int(input())
print(x + y)
Options
- A6
- B2
- C4
- D24
How the community answered
(21 responses)- A86% (18)
- B10% (2)
- C5% (1)
Explanation
Option A (6) is correct because int(input()) converts each entered string to an integer, so x = 2 and y = 4, and print(x + y) performs integer addition: 2 + 4 = 6.
Option D (24) is the most tempting wrong answer - it's what you'd get if the inputs were treated as strings and concatenated ("2" + "4" == "24"), but the int() conversion prevents this. Option B (2) and Option C (4) would only print if the code printed x or y individually, not their sum.
Memory tip: See int(input()) → think "math mode." The int() wrapper is the signal that + will add, not concatenate. If you ever see raw input() without int(), assume string behavior and concatenation.
Community Discussion
No community discussion yet for this question.