Python Dictionary Methods Quiz
1. What is the output? d = {"a": 1, "b": 2} print(d.get("a"))
A. 2
B. KeyError
C. None
D. 1
2. What is the output? d = {"x": 10, "y": 20} print(list(d.keys()))
A. [10, 20]
B. [('x', 10), ('y', 20)]
C. ['x', 'y']
D. ['x', 10, 'y', 20]
3. What is the output? d = {"a": 5, "b": 7} print(list(d.values()))
A. ['a', 'b']
B. [5, 'a', 7, 'b']
C. [('a', 5), ('b', 7)]
D. [5, 7]
4. What is the output? d = {"a": 1, "b": 2} print(list(d.items()))
A. [('a', 1), ('b', 2)]
B. [1, 2]
C. ['a', 'b']
D. {'a': 1, 'b': 2}
5. What is the output? d = {"a": 1} d.update({"b": 2}) print(d)
A. {'a': 2, 'b': 1}
B. {'b': 2}
C. {'a': 1}
D. {'a': 1, 'b': 2}
6. What is the output? d = {"a": 10, "b": 20} x = d.pop("a") print(x, d)
A. 20 {'a': 10}
B. 10 {'a': 10}
C. 10 {'b': 20}
D. a {'b': 20}
7. What is the output? d = {"a": 1, "b": 2} x = d.popitem() print(x, d)
A. ('b', 2) {'a': 1, 'b': 2}
B. 2 {'a': 1, 'b': 2}
C. ('a', 1) {'b': 2}
D. ('b', 2) {'a': 1}
8. What is the output? d = {"a": 1} x = d.setdefault("b", 2) print(x, d)
A. 1 {'a': 1, 'b': 2}
B. 2 {'a': 1}
C. None {'a': 1, 'b': 2}
D. 2 {'a': 1, 'b': 2}
9. What is the output? d = {"a": 1} x = d.setdefault("a", 99) print(x, d)
A. 99 {'a': 1}
B. None {'a': 1}
C. 99 {'a': 99}
D. 1 {'a': 1}
10. What is the output? d = {"a": 1, "b": 2} d.clear() print(d)
A. {'a': 1}
B. {}
C. None
D. {'b': 2}
11. What is the output? d = {"a": 1} c = d.copy() c["b"] = 2 print(d)
A. {'a': 1}
B. {'b': 2}
C. {'a': 1, 'b': 2}
D. {}
12. What is the output? d = dict.fromkeys(["a", "b"], 0) print(d)
A. {'a': 1, 'b': 1}
B. {0: 'a', 0: 'b'}
C. {'a': None, 'b': None}
D. {'a': 0, 'b': 0}
13. What is the output? d = {"a": 1} print(d.get("b", 99))
A. KeyError
B. 99
C. None
D. 1
14. What is the output? d = {"a": 1, "b": 2} d.update({"a": 10}) print(d)
A. {'a': 10, 'b': 2}
B. {'a': 10}
C. {'a': 1, 'b': 10}
D. {'a': 1, 'b': 2}
15. What is the output? d = {"a": 1, "b": 2} print("a" in d)
A. True
B. False
C. None
D. 1
16. What is the output? d = {"a": 1, "b": 2} print(d.get("c") is None)
A. True
B. 0
C. KeyError
D. False
17. What is the output? d = {"a": 1} d.update(b=2) print(d)
A. {'a': 1, 'b': None}
B. {'a': 2, 'b': 1}
C. {'a': 1, 'b': 2}
D. {'b': 2}
18. What is the output? d = {"a": 1, "b": 2} x = d.pop("b") print(x)
A. None
B. 1
C. 2
D. b
19. What is the output? d = {"x": 1, "y": 2} print(len(d.keys()))
A. 2
B. 1
C. 0
D. 3
20. What is the output? d = {"a": 1} d.setdefault("b") print(d)
A. {'a': 1, 'b': 0}
B. {'b': None}
C. {'a': 1}
D. {'a': 1, 'b': None}
1 comment:
What is the output?
`d = {"a": 1, "b": 2} print(d.get("a"))`
The key a exists and get returns its associated value, 1.
What is the output?
`d = {"x": 10, "y": 20} print(list(d.keys()))`
keys returns a view containing the dictionary's keys, which list converts to \['x', 'y'\].
What is the output?
`d = {"a": 5, "b": 7} print(list(d.values()))`
values returns the dictionary values in insertion order here.
What is the output?
`d = {"a": 1, "b": 2} print(list(d.items()))`
items returns each key-value pair as a tuple.
What is the output?
`d = {"a": 1} d.update({"b": 2}) print(d)`
update adds the new key-value pair to the dictionary.
What is the output?
`d = {"a": 10, "b": 20} x = d.pop("a") print(x, d)`
pop returns the removed value 10 and removes key a from the dictionary.
What is the output?
`d = {"a": 1, "b": 2} x = d.popitem() print(x, d)`
popitem removes and returns the last inserted key-value pair, b and 2.
What is the output?
`d = {"a": 1} x = d.setdefault("b", 2) print(x, d)`
Because b is absent, setdefault inserts b with value 2 and returns 2.
What is the output?
`d = {"a": 1} x = d.setdefault("a", 99) print(x, d)`
For an existing key, setdefault returns its current value and does not replace it.
What is the output?
`d = {"a": 1, "b": 2} d.clear() print(d)`
clear removes all items from the dictionary, leaving an empty dictionary.
What is the output?
`d = {"a": 1} c = d.copy() c["b"] = 2 print(d)`
copy creates a separate shallow dictionary, so adding b to c does not add it to d.
What is the output?
`d = dict.fromkeys(["a", "b"], 0) print(d)`
fromkeys creates entries for each supplied key using 0 as the common value.
What is the output?
`d = {"a": 1} print(d.get("b", 99))`
b is missing, so get returns the explicitly supplied default 99.
What is the output?
`d = {"a": 1, "b": 2} d.update({"a": 10}) print(d)`
update replaces the value for an existing key with the supplied value.
What is the output?
`d = {"a": 1, "b": 2} print("a" in d)`
The membership test checks dictionary keys, and a is present.
What is the output?
`d = {"a": 1, "b": 2} print(d.get("c") is None)`
c is absent and get without a default returns None.
What is the output?
`d = {"a": 1} d.update(b=2) print(d)`
Keyword arguments supplied to update become dictionary entries.
What is the output?
`d = {"a": 1, "b": 2} x = d.pop("b") print(x)`
pop returns the value associated with the removed key b, which is 2.
What is the output?
`d = {"x": 1, "y": 2} print(len(d.keys()))`
The dictionary has two keys, so its keys view contains two elements.
What is the output?
`d = {"a": 1} d.setdefault("b") print(d)`
When the key is missing and no default is supplied, setdefault inserts the key with value None.
Post a Comment