PCEP-30-02 · Question #271
Consider the following python code: ``python x1 = '23' y1 = 7 z1 = x1 * y1 x2 = 42 y2 = 7 z2 = x2 / y2 x3 = 4.7 y3 = 1 z3 = x3 / y3 `` What are the data types of the variables z1, z2 and z3?
The correct answer is B. str, oat, oat. Option B is correct because Python's behavior with these three operators follows specific type rules. Multiplying a string (x1 = '23') by an integer repeats the string, producing '23232323232323' - a str. In Python 3, the / operator always returns a float, even when dividing…
Question
x1 = '23'
y1 = 7
z1 = x1 * y1
x2 = 42
y2 = 7
z2 = x2 / y2
x3 = 4.7
y3 = 1
z3 = x3 / y3
What are the data types of the variables z1, z2 and z3?Options
- Astr, str, str
- Bstr, oat, oat
- Cstr, int, int
- Dstr, int, oat
How the community answered
(44 responses)- A2% (1)
- B75% (33)
- C16% (7)
- D7% (3)
Explanation
Option B is correct because Python's behavior with these three operators follows specific type rules. Multiplying a string (x1 = '23') by an integer repeats the string, producing '23232323232323' - a str. In Python 3, the / operator always returns a float, even when dividing two integers (42 / 7 = 6.0, not 6), making z2 a float; z3 is also a float because dividing a float by an integer yields a float.
Why the distractors fail: A is wrong because / never returns a string. C and D are wrong for the same key reason: C claims both z2 and z3 are int, and D claims z2 is int - but in Python 3, / always produces a float regardless of the operand types (use // for integer division).
Memory tip: Think of Python 3's / as "always floating" - it never gives you a whole number type, even if the math works out evenly. And remember that str * int doesn't multiply - it repeats.
Community Discussion
No community discussion yet for this question.