KnowledgeBoat Logo

Class - 12 CBSE Computer Science Important Output Questions 2025

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

Python

Python Dictionaries

4 Likes

Answer

True

Working

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.

Answered By

2 Likes