PCAP 31 03 PCAP Certified Associate Python Programmer · Free Practice Question Medium
Question 25
Q213 - Functions
What is the expected output of the following code?
- def func(message, num=1):
- print(message * num)
- func('Hello')
- func('Welcome', 3)
-
A
- Hello
- Viewers
-
B
- Hello
- Welcome,Welcome,Welcome
-
C
- Hello
- Welcome Welcome Welcome
-
D
- Hello
-
E
- Hello
- WelcomeWelcomeWelcome
Reveal correct answer
Correct answer: E
Explanation
Topics: def default parameter multiply operator string concatenation
Try it yourself:
- def func(message, num=1):
- print(message * num)
- func('Hello') # Hello
- func('Welcome', 3) # WelcomeWelcomeWelcome
Explanation:
In the function a string concatenation by multiplication takes place.
Once with the default value of num (1)
and once with the one passed by argument (3)
Q213 (Please refer to this number, if you want to write me about this question.)
A. This choice is incorrect because it includes a comma and the word "Viewers" after 'Hello', which is not part of the expected output. The function calls only print the messages 'Hello' and 'WelcomeWelcomeWelcome'.
B. This choice is incorrect because it includes commas between the words 'Welcome', which is not part of the expected output. The function calls only print the messages 'Hello' and 'WelcomeWelcomeWelcome' without any commas.
C. This choice is incorrect because it includes spaces between the words 'Welcome', which is not part of the expected output. The function calls only print the messages 'Hello' and 'WelcomeWelcomeWelcome' without any spaces between the repeated 'Welcome' words.
D. This choice is incorrect because it only includes the output for the first function call func('Hello'), which prints 'Hello' once. It does not include the output for the second function call func('Welcome', 3), which prints 'Welcome' three times.
E. The function func takes two parameters, message and num, with a default value of 1 for num. When the function is called with func('Hello'), it prints the message 'Hello' once. When the function is called with func('Welcome', 3), it prints the message 'Welcome' three times. Therefore, the expected output is 'Hello' followed by 'WelcomeWelcomeWelcome'.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
