PCEP-30-02 · Question #198
What is the expected output of the following code? def func(message, num=1): print(message * num) func('Hello') func('Welcome', 3)
The correct answer is D. 1 | Hello 2 | Welcome Welcome Welcome. Option D is correct because Python's string multiplication operator (`) repeats the string without any separator - 'Welcome' 3 produces 'WelcomeWelcomeWelcome', and 'Hello' 1 (the default) produces 'Hello'. Why distractors fail: A is wrong on the count - it shows Welcome only…
Question
Options
- A1 | Hello 2 | Welcome Welcome
- B1 | Hello 2 | Welcome Welcome Welcome
- C1 | Hello 2 | Welcome, Welcome, Welcome
- D1 | Hello 2 | Welcome Welcome Welcome
- E1 | Hello
How the community answered
(46 responses)- B11% (5)
- C4% (2)
- D83% (38)
- E2% (1)
Explanation
Option D is correct because Python's string multiplication operator (*) repeats the string without any separator - 'Welcome' * 3 produces 'WelcomeWelcomeWelcome', and 'Hello' * 1 (the default) produces 'Hello'.
Why distractors fail:
- A is wrong on the count - it shows
Welcomeonly twice, not three times. - B looks close but inserts spaces between repetitions (
Welcome Welcome Welcome), which*never does. - C incorrectly adds commas, as if using
', '.join(...)- that's a completely different operation. - E omits the second line of output entirely, ignoring
func('Welcome', 3).
Memory tip: Think of string multiplication as "paste n copies side by side with no glue" - 'ab' * 3 → 'ababab', never 'ab ab ab'. If you want separators, you need join(), not *.
Community Discussion
No community discussion yet for this question.