π Computational Thinking & Programming-I Quiz 13 β Dictionaries in Python (Class 11 CS)
Practice 20 CBSE Class 11 Computer Science (Code 083) MCQs on Python dictionaries β accessing items by key, mutability, traversing a dictionary, built-in functions/methods (len(), dict(), keys(), values(), items(), get(), update(), del, clear(), pop(), popitem(), setdefault(), max(), min()), and suggested programs like counting character frequency and storing employee salaries.
Every method and program result below has been verified by actually running it in Python. Tap any question to reveal the answer with a clear explanation.
π Dictionary Basics
What is a dictionary in Python?
- A sequence of numbers
- A sequence of characters
- A mathematical equation
- A collection of key-value pairs
Show Answer & Explanation
β Answer: (d) A collection of key-value pairs
A dictionary stores data as key-value pairs β each unique key maps to a value, letting you look things up by name instead of by numeric position, e.g. {"name": "Riya", "age": 16}.
π Accessing Items
How can you access the value associated with a specific key in a dictionary?
- By using parentheses ()
- By using curly braces {}
- By using square brackets []
- By using angle brackets <>
Show Answer & Explanation
β Answer: (c) By using square brackets []
You use square brackets with the key inside, e.g. d["name"] β even though the dictionary itself is defined with curly braces { }.
π Mutability
Can you modify the values in a dictionary after it’s created?
- Yes, values can be modified
- No, values cannot be modified
- Only numeric values can be modified
- Only string values can be modified
Show Answer & Explanation
β Answer: (a) Yes, values can be modified
Dictionaries are mutable β you can freely update, add, or remove key-value pairs after creation, regardless of what data type the values are.
π Mutability
Which of the following is the most direct way to add a new key-value pair to a dictionary?
- Direct assignment: d[new_key] = value
- Using the keys() method
- Using the values() method
- Using the items() method
Show Answer & Explanation
β Answer: (a) Direct assignment: d[new_key] = value
The simplest and most common way is direct assignment β d[new_key] = value β which adds the pair if the key doesn’t exist yet, or updates it if it does. (update() can also add pairs, especially several at once from another dictionary, but plain assignment is the standard everyday method. keys(), values(), and items() are read-only view methods and can’t add anything.)
π Traversal
How can you traverse a dictionary in Python, visiting each key (or key-value pair)?
- Using a for loop
- Using the sort() method
- Using an if statement alone
- Dictionaries cannot be traversed
Show Answer & Explanation
β Answer: (a) Using a for loop
A for loop β like for key in d: or for key, value in d.items(): β is the standard way to iterate through a dictionary’s keys or key-value pairs.
π§° Built-in Functions
Which built-in function determines the number of key-value pairs in a dictionary?
- len()
- dict()
- keys()
- values()
Show Answer & Explanation
β Answer: (a) len()
len() returns how many key-value pairs the dictionary contains.
π§° Built-in Functions
Which built-in method returns a view of all the keys in a dictionary?
- len()
- dict()
- keys()
- values()
Show Answer & Explanation
β Answer: (c) keys()
keys() returns a dict_keys view object containing all the keys β it behaves like a list for looping and membership checks, and can be converted to an actual list with list(d.keys()).
π§° Built-in Functions
Which built-in method returns a view of all the values in a dictionary?
- len()
- dict()
- keys()
- values()
Show Answer & Explanation
β Answer: (d) values()
values() returns a dict_values view object containing every value stored in the dictionary.
π§° Built-in Functions
Which built-in method returns a view of all the key-value pairs in a dictionary?
- len()
- dict()
- keys()
- items()
Show Answer & Explanation
β Answer: (d) items()
items() returns a dict_items view of (key, value) tuples β the standard way to loop through both keys and values together.
π§° Built-in Functions
Which built-in method retrieves the value associated with a key, without raising an error if the key doesn’t exist?
- get()
- update()
- del()
- clear()
Show Answer & Explanation
β Answer: (a) get()
get() safely retrieves a value by key β if the key isn’t found, it returns None (or a default you specify) instead of crashing, unlike d[key] which raises a KeyError.
π§° Built-in Functions
Which built-in method updates a dictionary with the key-value pairs from another dictionary?
- get()
- update()
- del()
- clear()
Show Answer & Explanation
β Answer: (b) update()
update() merges another dictionary’s key-value pairs into the original β adding new keys and overwriting values for any keys that already exist.
π§° Built-in Functions
Which built-in method removes a key-value pair from a dictionary given its key?
- pop()
- popitem()
- setdefault()
- copy()
Show Answer & Explanation
β Answer: (a) pop()
pop(key) removes the entry with the specified key from the dictionary and returns its value. (Note: it returns just the value, not the full key-value pair β for that, see popitem() below.)
π§° Built-in Functions
Which built-in method removes and returns an entire key-value pair (as a tuple) from a dictionary?
- pop()
- popitem()
- setdefault()
- copy()
Show Answer & Explanation
β Answer: (b) popitem()
popitem() removes and returns the most recently inserted key-value pair as a (key, value) tuple β unlike pop(), which needs a specific key and only returns the value.
π§° Built-in Functions
Which built-in function returns the maximum key in a dictionary?
- max()
- min()
- sorted()
- keys()
Show Answer & Explanation
β Answer: (a) max()
max() applied directly to a dictionary compares its keys (since iterating a dict by default gives you its keys) and returns the largest one.
β Suggested Programs
Which suggested program involves counting the number of times a character appears in a given string using a dictionary?
- Creating a dictionary with employee names and salaries
- Accessing values from a dictionary using get()
- Deleting a key-value pair from a dictionary
- Using a dictionary to store each character as a key and its count as the value
Show Answer & Explanation
β Answer: (d) Using a dictionary to store each character as a key and its count as the value
This program loops through the string and, for each character, either adds it as a new key with count 1 or increases its existing count β using each character as a key and its frequency as the value, commonly with freq[ch] = freq.get(ch, 0) + 1.
π§° Built-in Functions
Which statement is used to completely delete a specific key-value pair from a dictionary?
- del d[key]
- d.remove(key)
- d.discard(key)
- d.erase(key)
Show Answer & Explanation
β Answer: (a) del d[key]
del d[key] is the standard way to delete a specific entry entirely. (remove(), discard(), and erase() aren’t dictionary methods at all β remove()/discard() exist for sets, not dicts.)
π§° Built-in Functions
What does the clear() method do to a dictionary?
- Deletes the dictionary variable entirely
- Removes all key-value pairs, leaving an empty dictionary
- Removes only the keys, keeping the values
- Sorts the dictionary’s keys
Show Answer & Explanation
β Answer: (b) Removes all key-value pairs, leaving an empty dictionary
clear() empties out every key-value pair but keeps the dictionary object itself alive, now as {}.
π§° Built-in Functions
What does the setdefault() method do?
- Deletes a key from the dictionary
- Returns a key’s value if it exists; otherwise inserts the key with a default value and returns that
- Sorts the dictionary
- Always overwrites an existing key’s value
Show Answer & Explanation
β Answer: (b) Returns a key’s value if it exists; otherwise inserts the key with a default value and returns that
setdefault(key, default) is a convenient combo: if the key already exists, it just returns its current value; if not, it inserts the key with the given default value and then returns that default.
β Suggested Programs
What does the following program print? (Counting character frequency using a dictionary)
s = "banana"
freq = {}
for ch in s:
freq[ch] = freq.get(ch, 0) + 1
print(freq)- {‘b’: 1, ‘a’: 3, ‘n’: 2}
- {‘b’: 1, ‘a’: 2, ‘n’: 3}
- {‘b’: 2, ‘a’: 3, ‘n’: 1}
- Error
Show Answer & Explanation
β Answer: (a) {‘b’: 1, ‘a’: 3, ‘n’: 2}
In βbananaβ, ‘b’ appears once, ‘a’ appears three times, and ‘n’ appears twice β giving {‘b’: 1, ‘a’: 3, ‘n’: 2}. get(ch, 0) returns 0 the first time a character is seen, so the count starts correctly at 1.
β Suggested Programs
What does the following program print? (Employee names and salaries)
salaries = {"Riya": 45000, "Aman": 52000}
print(salaries["Aman"])- 45000
- 52000
- “Aman”
- KeyError
Show Answer & Explanation
β Answer: (b) 52000
Looking up the key "Aman" directly returns its associated value: 52000. This is the classic pattern for the suggested βemployee names and salariesβ dictionary program.
