CBSE Class 12 Computer Science Question 89 of 105

Python Revision Tour II — Question 16

Back to all questions
16
Question

Question 9

What will be the output of the following code snippet ?

rec = {"Name" : "Python", "Age" : "20", "Addr" : "NJ", "Country" : "USA"}
id1 = id(rec)
del rec
rec = {"Name" : "Python", "Age" : "20", "Addr" : "NJ", "Country" : "USA"}
id2 = id(rec)
print(id1 == id2)
  1. True
  2. False
  3. 1
  4. Exception
Answer
Output
True
Explanation

In the given python code snippet, id1 and id2 will point to two different objects in memory as del rec deleted the original dictionary whose id is stored in id1 and created a new dictionary with the same contents storing its id in id2. However, id1 == id2 will compare the contents of the two dictionaries pointed to by id1 and id2. As contents of both the dictionaries are same hence it returns True. If in this code we add another line print(id1 is id2) then this line will print False as id1 and id2 point to two different dictionary objects in memory.