250+ TOP MCQs on Python Dictionary and Answers

Python Multiple Choice Questions on “Dictionary – 2”.

1. 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
Answer: b
Clarification: The values of a dictionary can be accessed using keys but the keys of a dictionary can’t be accessed using values.

2. 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) { }
Answer: c
Clarification: Option c is a set, not a dictionary.

3. What will be the output of the following Python code snippet?

a={1:"A",2:"B",3:"C"}
for i,j in a.items():
    print(i,j,end=" ")

a) 1 A 2 B 3 C
b) 1 2 3
c) A B C
d) 1:”A” 2:”B” 3:”C”
Answer: a
Clarification: In the above code, variables i and j iterate over the keys and values of the dictionary respectively.

4. 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
Answer: b
Clarification: The get() method returns the value of the key if the key is present in the dictionary and the default value(second parameter) if the key isn’t present in the dictionary.

5. What will be the output of the following Python code snippet?

a={1:"A",2:"B",3:"C"}
print(a.get(5,4))

a) Error, invalid syntax
b) A
c) 5
d) 4
Answer: d
Clarification: The get() method returns the default value(second parameter) if the key isn’t present in the dictionary.

6. What will be the output of the following Python code snippet?

a={1:"A",2:"B",3:"C"}
print(a.setdefault(3))

a) {1: ‘A’, 2: ‘B’, 3: ‘C’}
b) C
c) {1: 3, 2: 3, 3: 3}
d) No method called setdefault() exists for dictionary
Answer: b
Clarification: setdefault() is similar to get() but will set dict[key]=default if key is not already in the dictionary.

7. What will be the output of the following Python code snippet?

a={1:"A",2:"B",3:"C"}
a.setdefault(4,"D")
print(a)

a) {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’}
b) None
c) Error
d) [1,3,6,10]
Answer: a
Clarification: setdefault() will set dict[key]=default if key is not already in the dictionary.

8. What will be the output of the following Python code?

a={1:"A",2:"B",3:"C"}
b={4:"D",5:"E"}
a.update(b)
print(a)

a) {1: ‘A’, 2: ‘B’, 3: ‘C’}
b) Method update() doesn’t exist for dictionaries
c) {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’, 5: ‘E’}
d) {4: ‘D’, 5: ‘E’}
Answer: c
Clarification: update() method adds dictionary b’s key-value pairs to dictionary a. Execute in python shell to verify.

9. What will be the output of the following Python code?

a={1:"A",2:"B",3:"C"}
b=a.copy()
b[2]="D"
print(a)

a) Error, copy() method doesn’t exist for dictionaries
b) {1: ‘A’, 2: ‘B’, 3: ‘C’}
c) {1: ‘A’, 2: ‘D’, 3: ‘C’}
d) “None” is printed
Answer: b
Clarification: Changes made in the copy of the dictionary isn’t reflected in the original one.

10. What will be the output of the following Python code?

a={1:"A",2:"B",3:"C"}
a.clear()
print(a)

a) None
b) { None:None, None:None, None:None}
c) {1:None, 2:None, 3:None}
d) { }
Answer: d
Clarification: The clear() method clears all the key-value pairs in the dictionary.

11. 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
Answer: c
Clarification: Keys of a dictionary may be any data type that is immutable.

12. What will be the output of the following Python code?

a={1:5,2:3,3:4}
a.pop(3)
print(a)

a) {1: 5}
b) {1: 5, 2: 3}
c) Error, syntax error for pop() method
d) {1: 5, 3: 4}
Answer: b
Clarification: pop() method removes the key-value pair for the key mentioned in the pop() method.

13. 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
Answer: a
Clarification: pop() method returns the value when the key is passed as an argument and otherwise returns the default value(second argument) if the key isn’t present in the dictionary.

14. What will be the output of the following Python code?

a={1:"A",2:"B",3:"C"}
for i in a:
    print(i,end=" ")

a) 1 2 3
b) ‘A’ ‘B’ ‘C’
c) 1 ‘A’ 2 ‘B’ 3 ‘C’
d) Error, it should be: for i in a.items():
Answer: a
Clarification: The variable i iterates over the keys of the dictionary and hence the keys are printed.

15. What will be the output of the following Python code?

>>> a={1:"A",2:"B",3:"C"}
>>> a.items()

a) Syntax error
b) dict_items([(‘A’), (‘B’), (‘C’)])
c) dict_items([(1,2,3)])
d) dict_items([(1, ‘A’), (2, ‘B’), (3, ‘C’)])
Answer: d
Clarification: The method items() returns list of tuples with each tuple having a key-value pair.

Leave a Reply

Your email address will not be published. Required fields are marked *