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

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

Question 1 of 53 0 correct so far

๐Ÿ“– Dictionaries Quiz

Q1.

A dictionary in Python stores data as:

Explanation

Each item of a dictionary is a key and its value, like "Riya": 92.

Q2.

Which brackets are used to create a dictionary?

Explanation

Example: d = {"Riya": 92, "Aman": 78}.

Q3.

Which symbol separates a key from its value in a dictionary?

Explanation

Pairs are written as key: value, separated by commas.

Q4.

Which statement about dictionary KEYS is TRUE?

Explanation

Keys can be strings, numbers or tuples, but never repeated or mutable.

Q5.

Which of these CANNOT be used as a dictionary key?

Explanation

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

Q6.

What does d = {[1, 2]: "a"} give?

Explanation

A list is unhashable (mutable) and cannot be a key.

Q7.

Can two different keys have the SAME value in a dictionary?

Explanation

Only keys must be unique; values can repeat.

Q8.

Is a dictionary mutable?

Explanation

A dictionary is mutable.

Q9.

What is the output of:

d = {"a": 1, "a": 2}
print(d)
Explanation

A repeated key keeps only the LAST value.

Q10.

Which statement creates an EMPTY dictionary?

Explanation

{} or dict() makes an empty dictionary.

Q11.

What is the output of:

print(dict(a=1, b=2))
Explanation

dict() with keyword arguments makes string keys.

Q12.

What is the output of:

print(dict([("x", 10), ("y", 20)]))
Explanation

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

Q13.

d = {"Riya": 92, "Aman": 78}. What is d["Aman"]?

Explanation

A value is accessed using its key.

Q14.

d = {"Riya": 92}. What does d["Zoya"] give?

Explanation

Accessing a key that doesn't exist gives KeyError.

Q15.

d = {"Riya": 92}. What does d[0] give?

Explanation

Dictionaries don't use positions; 0 is treated as a key, and there is no such key.

Q16.

d = {"Riya": 92}. What is the output of print(d.get("Zoya"))?

Explanation

get() returns None instead of an error when the key is missing.

Q17.

d = {"Riya": 92}. What is the output of print(d.get("Zoya", 0))?

Explanation

The second argument of get() is the default value.

Q18.

What is the output of:

d = {"a": 1}
d["b"] = 2
print(d)
Explanation

Assigning to a new key ADDS a new pair.

Q19.

What is the output of:

d = {"a": 1}
d["a"] = 5
print(d)
Explanation

Assigning to an existing key CHANGES its value.

Q20.

What is the output of:

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

update() changes existing keys and adds new ones.

Q21.

What is the output of:

d = {"a": 1, "b": 2}
del d["a"]
print(d)
Explanation

del d[key] removes that pair.

Q22.

What is the output of:

d = {"a": 1, "b": 2}
x = d.pop("b")
print(x, d)
Explanation

pop(key) removes the pair and returns its value.

Q23.

What is the output of:

d = {"a": 1, "b": 2}
print(d.popitem())
Explanation

popitem() removes and returns the LAST inserted pair as a tuple.

Q24.

What is the output of:

d = {"a": 1}
d.clear()
print(d)
Explanation

clear() removes all pairs but the dictionary still exists.

Q25.

After del d (without brackets), what happens when you print(d)?

Explanation

del d removes the dictionary name itself.

Q26.

What is the output of:

d = {"Riya": 92, "Aman": 78}
print("Riya" in d)
Explanation

in checks the KEYS of a dictionary.

Q27.

What is the output of:

d = {"Riya": 92, "Aman": 78}
print(92 in d)
Explanation

in checks only keys, not values.

Q28.

How can you check whether 92 is one of the VALUES of d?

Explanation

d.values() gives all values, so in works on them.

Q29.

What is the output of:

d = {"x": 1, "y": 2}
for k in d:
    print(k, end=" ")
Explanation

Looping over a dictionary gives its keys.

Q30.

What is the output of:

d = {"x": 1, "y": 2}
for k in d:
    print(d[k], end=" ")
Explanation

d[k] gives the value for each key.

Q31.

What is the output of:

d = {"x": 1, "y": 2}
for k, v in d.items():
    print(k, v, end=" ")
Explanation

items() gives each (key, value) pair.

Q32.

What is the output of:

print(list({"a": 1, "b": 2}.keys()))
Explanation

keys() gives all the keys.

Q33.

What is the output of:

print(list({"a": 1, "b": 2}.values()))
Explanation

values() gives all the values.

Q34.

What is the output of:

print(list({"a": 1, "b": 2}.items()))
Explanation

items() gives (key, value) tuples.

Q35.

What is the output of:

print(len({"a": 1, "b": 2, "c": 3}))
Explanation

len() gives the number of key-value pairs.

Q36.

What is the output of:

print(max({"Riya": 92, "Zoya": 85, "Aman": 78}))
Explanation

max() on a dictionary works on the KEYS (alphabetical order).

Q37.

What is the output of:

print(sorted({"b": 2, "c": 1, "a": 3}))
Explanation

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

Q38.

What is the output of:

freq = {}
for ch in "aba":
    freq[ch] = freq.get(ch, 0) + 1
print(freq)
Explanation

get(ch, 0) starts a new character at 0, then adds 1 each time.

Q39.

Why does freq[ch] = freq[ch] + 1 give an error for a NEW character?

Explanation

Use freq.get(ch, 0) + 1 to handle new keys.

Q40.

What is the output of:

words = "to be or not to be".split()
d = {}
for w in words:
    d[w] = d.get(w, 0) + 1
print(d["to"], d["or"])
Explanation

"to" appears twice and "or" appears once.

Q41.

What is the output of:

m = {"Riya": 92, "Aman": 78, "Zoya": 85}
top = ""
best = 0
for n in m:
    if m[n] > best:
        best = m[n]
        top = n
print(top)
Explanation

Riya has the highest marks (92).

Q42.

What is the output of:

m = {"A": 85, "B": 60, "C": 90}
for k in m:
    if m[k] >= 80:
        print(k, end=" ")
Explanation

Only A (85) and C (90) are 80 or more.

Q43.

A list vs a dictionary โ€” which is TRUE?

Explanation

Lists use positions starting at 0; dictionaries use keys.

Q44.

Which is the best choice to store the phone numbers of friends by their names?

Explanation

A dictionary lets you look up the number directly with the name as the key.

Q45.

What is the output of:

d = {1: "one", 2: "two"}
print(d[2])
Explanation

Here 2 is a KEY, not a position.

Q46.

What is the output of:

d = {}
d["x"] = d.get("x", 10) + 5
print(d)
Explanation

"x" is missing, so get() returns 10, and 10 + 5 = 15.

Q47.

What is the output of:

d = {"a": 1, "b": 2}
print(d.pop("z", "none"))
Explanation

pop() with a default returns the default when the key is missing.

Q48.

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

Explanation

items() gives (key, value) pairs.

Q49.

Which method adds the pairs of one dictionary into another?

Explanation

d1.update(d2) adds/changes pairs from d2 in d1.

Q50.

What is the output of:

d = {"a": [1, 2]}
d["a"].append(3)
print(d)
Explanation

Values can be any type, including lists, and the list can be changed.

Q51.

What is the output of:

d = {(1, 2): "point"}
print(d[(1, 2)])
Explanation

A tuple is immutable, so it can be used as a key.

Q52.

What is the output of:

print(type({}))
Explanation

Empty curly brackets create an empty dictionary.

Q53.

Which statement adds a new pair with key "city" and value "Pune" to dictionary s?

Explanation

Assigning to a new key adds the pair.