Computational Thinking and Programming-I Quiz 13

πŸ“– 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πŸ”‘ Accessing ItemsπŸ”“ MutabilityπŸ” Traversal🧰 Built-in Functions⭐ Suggested Programs
Q1
πŸ“– Dictionary Basics

What is a dictionary in Python?

  1. A sequence of numbers
  2. A sequence of characters
  3. A mathematical equation
  4. 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}.

Q2
πŸ”‘ Accessing Items

How can you access the value associated with a specific key in a dictionary?

  1. By using parentheses ()
  2. By using curly braces {}
  3. By using square brackets []
  4. 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 { }.

Q3
πŸ”“ Mutability

Can you modify the values in a dictionary after it’s created?

  1. Yes, values can be modified
  2. No, values cannot be modified
  3. Only numeric values can be modified
  4. 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.

Q4
πŸ”“ Mutability

Which of the following is the most direct way to add a new key-value pair to a dictionary?

  1. Direct assignment: d[new_key] = value
  2. Using the keys() method
  3. Using the values() method
  4. 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.)

Q5
πŸ” Traversal

How can you traverse a dictionary in Python, visiting each key (or key-value pair)?

  1. Using a for loop
  2. Using the sort() method
  3. Using an if statement alone
  4. 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.

Q6
🧰 Built-in Functions

Which built-in function determines the number of key-value pairs in a dictionary?

  1. len()
  2. dict()
  3. keys()
  4. values()
Show Answer & Explanation

βœ… Answer: (a) len()

len() returns how many key-value pairs the dictionary contains.

Q7
🧰 Built-in Functions

Which built-in method returns a view of all the keys in a dictionary?

  1. len()
  2. dict()
  3. keys()
  4. 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()).

Q8
🧰 Built-in Functions

Which built-in method returns a view of all the values in a dictionary?

  1. len()
  2. dict()
  3. keys()
  4. values()
Show Answer & Explanation

βœ… Answer: (d) values()

values() returns a dict_values view object containing every value stored in the dictionary.

Q9
🧰 Built-in Functions

Which built-in method returns a view of all the key-value pairs in a dictionary?

  1. len()
  2. dict()
  3. keys()
  4. 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.

Q10
🧰 Built-in Functions

Which built-in method retrieves the value associated with a key, without raising an error if the key doesn’t exist?

  1. get()
  2. update()
  3. del()
  4. 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.

Q11
🧰 Built-in Functions

Which built-in method updates a dictionary with the key-value pairs from another dictionary?

  1. get()
  2. update()
  3. del()
  4. 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.

Q12
🧰 Built-in Functions

Which built-in method removes a key-value pair from a dictionary given its key?

  1. pop()
  2. popitem()
  3. setdefault()
  4. 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.)

Q13
🧰 Built-in Functions

Which built-in method removes and returns an entire key-value pair (as a tuple) from a dictionary?

  1. pop()
  2. popitem()
  3. setdefault()
  4. 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.

Q14
🧰 Built-in Functions

Which built-in function returns the maximum key in a dictionary?

  1. max()
  2. min()
  3. sorted()
  4. 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.

Q15
⭐ Suggested Programs

Which suggested program involves counting the number of times a character appears in a given string using a dictionary?

  1. Creating a dictionary with employee names and salaries
  2. Accessing values from a dictionary using get()
  3. Deleting a key-value pair from a dictionary
  4. 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.

Q16
🧰 Built-in Functions

Which statement is used to completely delete a specific key-value pair from a dictionary?

  1. del d[key]
  2. d.remove(key)
  3. d.discard(key)
  4. 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.)

Q17
🧰 Built-in Functions

What does the clear() method do to a dictionary?

  1. Deletes the dictionary variable entirely
  2. Removes all key-value pairs, leaving an empty dictionary
  3. Removes only the keys, keeping the values
  4. 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 {}.

Q18
🧰 Built-in Functions

What does the setdefault() method do?

  1. Deletes a key from the dictionary
  2. Returns a key’s value if it exists; otherwise inserts the key with a default value and returns that
  3. Sorts the dictionary
  4. 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.

Q19
⭐ 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)
  1. {‘b’: 1, ‘a’: 3, ‘n’: 2}
  2. {‘b’: 1, ‘a’: 2, ‘n’: 3}
  3. {‘b’: 2, ‘a’: 3, ‘n’: 1}
  4. 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.

Q20
⭐ Suggested Programs

What does the following program print? (Employee names and salaries)

salaries = {"Riya": 45000, "Aman": 52000}
print(salaries["Aman"])
  1. 45000
  2. 52000
  3. “Aman”
  4. 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.

Jitendra Singh
βœ” Verified Educator

Jitendra Singh

Founder of CBSEPython.in

I help CBSE Class 9–12 students learn Python, Information Technology, Artificial Intelligence and Computer Science through easy notes, quizzes, MCQs and sample papers.

Read More About Me β†’