PCEP-30-02 · Question #82
What is the expected output of the following code? print(3 * "abc" + 'xyz')
The correct answer is C. abcabcabcxyz. Option C is correct because Python's ` operator on a string repeats it - 3 "abc" produces "abcabcabc" - and then + concatenates "xyz" to the end, giving "abcabcabcxyz". Option A (3abcxyz) is wrong because 3 does not prepend a numeric digit; it repeats the string. Option B…
Question
Options
- A3abcxyz
- Babcabcxyzxyz
- Cabcabcabcxyz
- Dabcxyzabcxyzabcxyz
How the community answered
(33 responses)- A6% (2)
- B9% (3)
- C82% (27)
- D3% (1)
Explanation
Option C is correct because Python's * operator on a string repeats it - 3 * "abc" produces "abcabcabc" - and then + concatenates "xyz" to the end, giving "abcabcabcxyz". Option A (3abcxyz) is wrong because 3 * does not prepend a numeric digit; it repeats the string. Option B (abcabcxyzxyz) incorrectly assumes "xyz" also gets repeated, which it does not - repetition only applies to "abc". Option D (abcxyzabcxyzabcxyz) mistakes operator precedence: * binds to "abc" alone, not to the entire "abc" + 'xyz' expression.
Memory tip: Think of * as a copy machine - 3 * "abc" makes 3 copies of "abc" first, then + staples "xyz" onto the end exactly once.
Community Discussion
No community discussion yet for this question.