PCEP-30-02 · Question #236
QUESTION 264 What is the expected output of the following code? num = '7' * '7' print(num)
The correct answer is B. The code is erroneous. Option B is correct because Python does not support multiplying two string objects together - '7' '7' raises a TypeError: can't multiply sequence by non-int of type 'str', making the code erroneous. Why the distractors fail: A (7777777) would be correct if the right operand…
Question
Options
- A7777777
- BThe code is erroneous.
- C49
- D77
How the community answered
(39 responses)- A13% (5)
- B77% (30)
- C3% (1)
- D8% (3)
Explanation
Option B is correct because Python does not support multiplying two string objects together - '7' * '7' raises a TypeError: can't multiply sequence by non-int of type 'str', making the code erroneous.
Why the distractors fail:
- A (7777777) would be correct if the right operand were an integer:
'7' * 7repeats the string seven times. But'7'is a string, not an integer, so repetition is impossible. - C (49) assumes both operands are treated as numbers (
7 × 7 = 49), but Python does not silently coerce strings to integers in arithmetic operations. - D (77) might seem like simple string concatenation, but
*is not the concatenation operator -+is ('7' + '7'gives'77').
Memory tip: Think of Python's * with strings as "string × integer = repetition." The rule is exactly one side must be an int - two strings, two ints with * on strings, or any other combo is a TypeError.
Community Discussion
No community discussion yet for this question.