PCEP-30-02 · Question #84
What would you insert instead of ???, so that the program prints True to the monitor x = 'Peter' y = 'Peter' res = ??? print(res)
The correct answer is C. x is y. Option C (x is y) prints True because Python's CPython implementation interns short string literals - when you assign 'Peter' to both x and y, Python reuses the same object in memory rather than creating two separate ones, so is (which checks object identity, not value…
Question
Options
- Ax < y
- Bx is not y
- Cx is y
- Dx != y
How the community answered
(25 responses)- A12% (3)
- B8% (2)
- C76% (19)
- D4% (1)
Explanation
Option C (x is y) prints True because Python's CPython implementation interns short string literals - when you assign 'Peter' to both x and y, Python reuses the same object in memory rather than creating two separate ones, so is (which checks object identity, not value equality) returns True.
Option A (x < y) returns False because the strings are equal, so neither is "less than" the other. Option B (x is not y) is the direct opposite of C - it returns False precisely because they are the same object. Option D (x != y) returns False because the values are identical, making them equal, not unequal.
Memory tip: Think of is as asking "Are these the same object in memory?" and == as asking "Do these have the same value?" - this question exploits string interning to make is behave like ==, which is an implementation detail you'd normally never rely on in real code.
Community Discussion
No community discussion yet for this question.