Test Index

Python Revision Tour Get it on Google PlayDownload on the App Store CBSE Board Videos

© examsnet.com
Question : 84 of 102
Marks: +1, -0
Which of the following statement(s) would give an error after executing the following code ?
Stud={"Murugan":100, "Mithu":95}  # Statement 1
print (Stud[95]) # Statement 2
Stud ["Murugan"]=99 # Statement 3
print(Stud.pop()) # Statement 4
print(Stud) # Statement 5
[CBSE 2023 Set 4]
Solution:  
Concept:
A dictionary is accessed by its keys, not by its values. pop() on a dictionary needs the key to remove.
Explanation:
Statement 1: creates the dictionary, no error.
Statement 2: Stud[95] looks for the key 95; 95 is a value, not a key, so KeyError: 95.
Statement 3: changes the value of the existing key "Murugan", no error.
Statement 4: Stud.pop() gives TypeError: pop expected at least 1 argument, got 0.
Statement 5: prints the dictionary, no error.
Key point: Statements 2 and 4 are both wrong on their own; each one raises an error when it is executed.
Answer:
Option D (Statements 2 and 4)
© examsnet.com
Advertisements
Go to Question: