Exit Chapter 12 ยท ๐Ÿ“– Dictionaries โ€” Quiz

Playing as a guest? to earn XP and track your progress.

Question 1 of 52 0 correct so far

๐Ÿ“– Dictionaries Quiz

Q1.

What is a dictionary in Python?

Explanation

A dictionary maps each key to a value, and values are looked up by key rather than by index.

Q2.

For student = {"roll": 101, "name": "Riya", "marks": 92.5}, what does student["name"] return?

Explanation

The key "name" is mapped to the value "Riya".

Q3.

In the analogy of an English dictionary, the 'word' you look up corresponds to the:

Explanation

You look up a word (key) to find its meaning (value).

Q4.

For d = {"a": 1, "b": 2}, what does d[0] do?

Explanation

There is no positional index in a dictionary; d[0] looks for the KEY 0.

Q5.

What is type({})?

Explanation

{} creates an empty dictionary.

Q6.

What is emp after emp = dict(name="Aman", age=30)?

Explanation

With keyword arguments, dict() makes the argument names into string keys.

Q7.

What is dict([(1, "one"), (2, "two")])?

Explanation

dict() can build a dictionary from a list of (key, value) pairs.

Q8.

What does dict.fromkeys(["Riya", "Aman"], 0) return?

Explanation

fromkeys() creates a dictionary with the given keys, all set to the same value.

Q9.

What does dict.fromkeys([1, 2, 3]) return?

Explanation

When no value is given, fromkeys() uses None.

Q10.

What is printed after d = {"a": 1, "b": 2, "a": 3} and print(d)?

Explanation

Keys must be unique, so for a repeated key the last value wins.

Q11.

Which of these can be used as a dictionary key?

Explanation

Keys must be immutable; a tuple of immutable values is allowed, but lists and dictionaries are not.

Q12.

What happens with bad = {[1, 2]: "list key"}?

Explanation

A list is mutable, so it cannot be a dictionary key.

Q13.

Which statement about dictionary values is correct?

Explanation

Only keys have restrictions (unique and immutable); values can be anything.

Q14.

For student = {"roll": 101, "name": "Riya"}, what does student["age"] give?

Explanation

Square-bracket access raises a KeyError when the key is missing.

Q15.

For student = {"roll": 101, "name": "Riya"}, what does student.get("age") return?

Explanation

get() returns None for a missing key instead of raising an error.

Q16.

What does student.get("age", "N/A") return if the key "age" is missing?

Explanation

The second argument of get() is the default value returned for a missing key.

Q17.

Which statement comparing d[key] and d.get(key) is correct?

Explanation

Both return the value for an existing key; only d[key] = value can add or update an item.

Q18.

For student = {"roll": 101, "name": "Riya"}, what is "Riya" in student?

Explanation

in checks KEYS only; "Riya" is a value. Use "Riya" in student.values() to check values.

Q19.

What is stock after this code?

stock = {"pen": 50, "pencil": 30} stock["pen"] = 45 stock["eraser"] = 20

Explanation

d[key] = value updates an existing key and adds a new one.

Q20.

For d = {"a": 1, "b": 2, "c": 3}, what is d after del d["b"]?

Explanation

del d[key] removes the item with that key.

Q21.

For d = {"a": 1, "b": 2, "c": 3}, what does x = d.pop("a") store in x?

Explanation

pop(key) removes the item and returns its VALUE.

Q22.

For d = {"a": 1, "b": 2}, what does d.pop("z", 0) return?

Explanation

With a default, pop() returns it instead of raising an error; d is unchanged.

Q23.

For d = {"a": 1, "b": 2, "c": 3}, what does d.popitem() return?

Explanation

popitem() removes and returns the LAST inserted item as a (key, value) tuple.

Q24.

Which statement comparing pop() and popitem() is correct?

Explanation

pop needs a key and returns a value; popitem takes no argument and returns a tuple.

Q25.

When you loop with 'for x in d:' over a dictionary d, x takes:

Explanation

A for loop over a dictionary gives its keys one by one.

Q26.

Which loop gives both the key and the value on each iteration?

Explanation

items() gives (key, value) pairs that can be unpacked in the loop.

Q27.

What is the total after this code?

marks = {"Maths": 95, "Physics": 88, "Chemistry": 91} total = 0 for m in marks.values(): total += m

Explanation

values() gives 95, 88 and 91, which add up to 274.

Q28.

For d = {"x": 10, "y": 20}, what is list(d.items())?

Explanation

items() gives the (key, value) pairs; list() turns the view object into a list.

Q29.

What do keys(), values() and items() return?

Explanation

They return view objects such as dict_keys(['x', 'y']).

Q30.

What does len({"a": 1, "b": 2}) return?

Explanation

len() counts the items (key-value pairs), not keys and values separately.

Q31.

What is the output of print(len({"a": 1, "b": 2, "a": 3}))?

Explanation

The duplicate key "a" is stored only once.

Q32.

What is d after this code?

d = {"a": 1, "b": 2} d.update({"b": 9, "c": 3})

Explanation

update() adds the new items and overwrites the values of keys that are already present.

Q33.

What does {"a": 1}.setdefault("b", 5) do?

Explanation

If the key is absent, setdefault() adds k : v and returns v; if present, it returns the existing value.

Q34.

For prices = {"apple": 120, "banana": 40, "mango": 90}, what does max(prices) return?

Explanation

max() works on the KEYS by default; 'mango' is the largest alphabetically. Use max(prices.values()) for the largest value.

Q35.

What does sorted({"b": 2, "c": 1, "a": 3}) return?

Explanation

sorted() on a dictionary returns a sorted list of its KEYS.

Q36.

What is printed after this code?

A = {"x": 1} B = A B["y"] = 2 print(A)

Explanation

B = A is an alias, so changes through B also appear in A. Use A.copy() for an independent copy.

Q37.

For students = {101: {"name": "Riya"}, 102: {"name": "Aman"}}, what is students[102]["name"]?

Explanation

students[102] is the inner dictionary, and ['name'] gets 'Aman' from it.

Q38.

Which statement comparing a list and a dictionary is correct?

Explanation

A list stores values in [ ] by position; a dictionary stores key : value pairs in { } and in checks keys.

Q39.

What does the character-frequency program print for 'banana'?

Explanation

Each character's count is stored with the character as the key.

Q40.

In freq[ch] = freq.get(ch, 0) + 1, what does freq.get(ch, 0) return for a character seen for the first time?

Explanation

get() returns 0 for a missing key, so the count starts at 0 + 1 = 1.

Q41.

What does the frequency program (using get()) print for "mississippi"?

Explanation

m occurs once, i four times, s four times and p twice.

Q42.

In the word-frequency program for "the cat and the hat and the bat", what is the count of 'the'?

Explanation

The sentence is split into words, and 'the' appears 3 times.

Q43.

In the topper program, what is printed for {"Riya": 92, "Aman": 85, "Zoya": 97, "Kabir": 88}?

Explanation

The loop keeps track of the highest marks; 97 (Zoya) is the maximum.

Q44.

What does the invert-dictionary program produce from {"IN": "India", "JP": "Japan"}?

Explanation

It swaps keys and values: code_of[country] = code.

Q45.

What is printed after this code?

d = {1: "one", 2: "two", 3: "three"} d[2] = "TWO" d[4] = "four" print(d.get(5, "not found"), len(d))

Explanation

Key 5 is missing, so the default is returned; the dictionary now has 4 items.

Q46.

What is printed after this code?

stock = {"pen": 10, "ink": 5, "pad": 8} x = stock.pop("ink") y = stock.popitem() print(x, y)

Explanation

pop('ink') returns 5; popitem() removes the last inserted item, ('pad', 8).

Q47.

Following on, stock is now {"pen": 10}. What is stock after stock.update({"pen": 12, "clip": 20})?

Explanation

update() overwrites 'pen' with 12 and adds 'clip'.

Q48.

What does d become after this code?

word = "APPLE" d = {} for ch in word: d[ch] = d.get(ch, 0) + 1

Explanation

P appears twice; every other letter appears once.

Q49.

For d = {'A': 1, 'P': 2, 'L': 1, 'E': 1}, what does print("P" in d, 2 in d, 2 in d.values()) display?

Explanation

'P' is a key; 2 is not a key, but it is one of the values.

Q50.

What does {"x": 1, "x": 2} evaluate to?

Explanation

For a duplicate key, the last value overwrites the earlier one.

Q51.

What does len({}) return?

Explanation

An empty dictionary has 0 items.

Q52.

Why can a tuple be a dictionary key but a list cannot?

Explanation

Mutable objects such as lists are unhashable and cannot be keys.