PCEP-30-02 · Question #86
What is the data type of x, y, z after executing the following snippet? ``python x = 23 + 42 y = '23' + '42' z = '23' * 7 ``
The correct answer is D. int, str, str. Option D is correct because Python's + operator on integers performs arithmetic addition (23 + 42 = 65, an int), while + on strings performs concatenation ('23' + '42' = '2342', a str), and ` on a string repeats it ('23' 7 = '23232323232323', still a str). Why the distractors…
Question
x = 23 + 42
y = '23' + '42'
z = '23' * 7
Options
- Aint, int, int
- Bx is int, y and z are invalid declarations
- Cint, str, int
- Dint, str, str
How the community answered
(30 responses)- A3% (1)
- B7% (2)
- C10% (3)
- D80% (24)
Explanation
Option D is correct because Python's + operator on integers performs arithmetic addition (23 + 42 = 65, an int), while + on strings performs concatenation ('23' + '42' = '2342', a str), and * on a string repeats it ('23' * 7 = '23232323232323', still a str).
Why the distractors fail:
- A is wrong because
yandzinvolve string operands, not integers. - B is wrong because both
yandzare perfectly valid - Python's+and*operators are defined for strings. - C is wrong because
zis astr, not anint; multiplying a string by a number repeats the string, it doesn't do arithmetic.
Memory tip: Think of Python operators as context-sensitive - they behave according to the types of their operands. If even one operand is a string (quoted), + becomes concatenation and * becomes repetition, and the result stays a str.
Community Discussion
No community discussion yet for this question.