Test Index

CBSE Class 12 Computer Science 2025 Paper Compartment Get it on Google PlayDownload on the App Store

© examsnet.com
Question : 7 of 37
Marks: +1, -0
If the dictionary D1 is defined as :
D1={1:'a',2:'b'}
then which of the following statements is incorrect and hence will result in an error ?
Solution:  
Explanation:
(A) D1.get(1) returns 'a', the value of key 1.
(B) D1.get(3) returns None: get() does not raise an error for a missing key (only D1[3] would).
(C) del is a Python statement (keyword), not a dictionary method, so D1.del(1) is an error (a SyntaxError, since a keyword cannot be used as a method name). The correct ways are del D1[1] or D1.pop(1).
(D) D1.clear() empties the dictionary, giving {}.
Key point: get() with a missing key quietly returns None; that is why it is safer than D1[key].
Answer:
Option C (D1.del(1))
© examsnet.com
Advertisements
Go to Question: