PCEP-30-02 · Question #10
What is the expected output of the following code? ``python data = {'name': 'Peter', 'age': 30} person = data.copy() print(id(data) == id(person)) ``
The correct answer is A. False. dict.copy() creates a new dictionary object in memory - a shallow copy with identical contents but a distinct identity. Since data and person occupy different memory addresses, id(data) == id(person) evaluates to False, making A correct. B (1) and C (0) are wrong because…
Question
data = {'name': 'Peter', 'age': 30}
person = data.copy()
print(id(data) == id(person))
Options
- AFalse
- B1
- C0
- DTrue
How the community answered
(32 responses)- A84% (27)
- B9% (3)
- C3% (1)
- D3% (1)
Explanation
dict.copy() creates a new dictionary object in memory - a shallow copy with identical contents but a distinct identity. Since data and person occupy different memory addresses, id(data) == id(person) evaluates to False, making A correct.
B (1) and C (0) are wrong because id(data) == id(person) returns a Python boolean (True/False), not an integer; even though False == 0 is true in Python, print(False) outputs the string False, not 0 or 1. D (True) is wrong because True would only print if both variables were aliases to the same object (e.g., person = data without .copy()).
Memory tip: Think of .copy() as a photocopier - it produces an identical document, but on a different sheet of paper (a new object at a new address). Assignment (=) just hands you the same sheet; copy() hands you a duplicate.
Community Discussion
No community discussion yet for this question.