ProgrammingVery hardHCL · Tech Mahindra · Python placement
What does the following Python 3 code print? print(id([1, 2]) == id([1, 2]))
- A. Error
- B. True
- C. False
- D. None
Correct answer: B. True
In CPython, the first temporary list is garbage-collected (refcount drops to 0) before the second is created, so the allocator can reuse the same memory address and id.
More Python Programming questions
- What does the following Python 3 code print? try: a = (1, 2, 3) a[0] = 9 except TypeError: print("TypeError")
- What does the following Python 3 code print? try: d = {} print(d["missing"]) except KeyError: print("KeyError")
- What does the following Python 3 code print? try: s = "abc" print(s[10]) except IndexError: print("IndexError")
- What does the following Python 3 code print? try: x = int("12a") except ValueError: print("ValueError")
- What does the following Python 3 code print? try: def f(a, b): return a + b f(1) except TypeError: print("TypeError")
- What does the following Python 3 code print? try: x = [1, 2] + (3, 4) except TypeError: print("TypeError")