1. Which of the statements about dictionary values if false?
A. More than one key can have the same value
B. The values of the dictionary can be accessed as dict[key]
C. Values of a dictionary must be unique
D. Values of a dictionary can be a mixture of letters and numbers
Ans : Values of a dictionary must be unique
2. If a is a dictionary with some key-value pairs, what does a.popitem() do?
A. Removes an arbitrary element
B. Removes all the key-value pairs
C. Removes the key-value pair for the key given as an argument
D. Invalid method for dictionary
Ans : Removes an arbitrary element.
3. What will be the output of the following Python code snippet?
test = {1:'A', 2:'B', 3:'C'}
del test[1]
test[1] = 'D'
del test[2]
print(len(test))
A. 0
B. 2
C. Error as the key-value pair of 1:’A’ is already deleted
D. 1
Ans : 2
4. What will be the output of the following Python code snippet?
>>>import collections
>>> b=collections.Counter([2,2,3,4,4,4])
>>> b.most_common(1)
A. Counter({4: 3, 2: 2, 3: 1})
B. {3:1}
C. {4:3}
D. [(4, 3)]
Ans : [(4,3)]
5. What will be the output of the following Python code snippet?
>>> import collections
>>> a=collections.Counter([3,3,4,5])
>>> b=collections.Counter([3,4,4,5,5,5])
>>> a&b
A. Counter({3: 12, 4: 1, 5: 1})
B. Counter({3: 1, 4: 1, 5: 1})
C. Counter({4: 2})
D. Counter({5: 1})
Ans : Counter({3: 1, 4: 1, 5: 1})
6. What will be the output of the following Python code?
>>> import collections
>>> b=dict()
>>> b=collections.defaultdict(lambda: 7)
>>> b[4]
A. 4
B. 0
C. An exception is thrown
D. 7
Ans : 7
7. If b is a dictionary, what does any(b) do?
A. Returns True if any key of the dictionary is true
B. Returns False if dictionary is empty
C. Returns True if all keys of the dictionary are true
D. Method any() doesn’t exist for dictionary
Ans : Returns True if any key of the dictionary is true
8. What will be the output of the following Python code?
a={}
a[2]=1
a[1]=[2,3,4]
print(a[1][1])
A. [2,3,4]
B. 3
C. 2
D. An exception is thrown
Ans : 3
9. What will be the output of the following Python code?
>>> b={}
>>> all(b)
A. { }
B. False
C. True
D. An exception is thrown
Ans : True
10. What will be the output of the following Python code?
>>> a=dict()
>>> a[1]
A. An exception is thrown since the dictionary is empty
B. ‘ ‘
C. 1
D. 0
Ans : An exception is thrown since the dictionary is empty
11. What will be the output of the following Python code?
>>> import collections
>>> a=dict()
>>> a=collections.defaultdict(str)
>>> a['A']
A. An exception is thrown since the dictionary is empty
B. ‘ ‘
C. ‘A’
D. 0
Ans : ' '
12. Which of these about a dictionary is false?
A. The values of a dictionary can be accessed using keys
B. The keys of a dictionary can be accessed using values
C. Dictionaries aren’t ordered
D. Dictionaries are mutable
Ans : The keys of a dictinary can be accessed using values
13. Which of the following is not a declaration of the dictionary?
A. {1: ‘A’, 2: ‘B’}
B. dict([[1,”A”],[2,”B”]])
C. {1,”A”,2”B”}
D. { }
Ans : {1,”A”,2”B”}
14. Which of the following isn’t true about dictionary keys?
A. More than one key isn’t allowed
B. Keys must be immutable
C. Keys must be integers
D. When duplicate keys encountered, the last assignment wins
Ans : Keys must be integers
15. What will be the output of the following Python code?
a={1:5,2:3,3:4}
print(a.pop(4,9))
A. 9
B. 3
C. Too many arguments for pop() method
D. 4
Ans : 9
16. What will be the output of the following Python code snippet?
a={1:"A",2:"B",3:"C"}
print(a.get(1,4))
A. 1
B. A
C. 4
D. Invalid syntax for get method
Ans : A
17. Which of the following statements create a dictionary?
A. d = {}
B. d = {“john”:40, “peter”:45}
C. d = {40:”john”, 45:”peter”}
D. All of the mentioned
Ans : All of the mentioned
18. What will be the output of the following Python code snippet?
d = {"john":40, "peter":45}
d["john"]
A. 40
B. 45
C. “john”
D. “peter”
Ans : 40
19. Suppose d = {“john”:40, “peter”:45}. To obtain the number of entries in dictionary which command do we use?
A. d.size()
B. len(d)
C. size(d)
D. d.len()
Ans : len(d)
20. What will be the output of the following Python code snippet?
d = {"john":40, "peter":45}
"john" in d
A. True
B. False
C. None
D. Error
Ans : True
0 Comments