Python MCQs on Dictionary: SET 2

11. 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

Check Answer

Answer: b
The values of a dictionary can be accessed using keys but the keys of a dictionary can’t be accessed using values.

12. 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) { }

Check Answer

Answer: c
Option c is a set, not a dictionary.

13. 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”

Check Answer

Answer: a
In the above code, variables i and j iterate over the keys and values of the dictionary respectively.

14. 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

Check Answer

Answer: b
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.

15. 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

Check Answer

Answer: d
The get() method returns the default value(second parameter) if the key isn’t present in the dictionary.

16. 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

Check Answer

Answer: b
setdefault() is similar to get() but will set dict[key]=default if key is not already in the dictionary.

17. 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]

Check Answer

Answer: a
setdefault() will set dict[key]=default if key is not already in the dictionary.

18. 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’}

Check Answer

Answer: c
update() method adds dictionary b’s key-value pairs to dictionary a. Execute in python shell to verify.

19. 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

Check Answer

Answer: b
Changes made in the copy of the dictionary isn’t reflected in the original one.

20. 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) { }

Check Answer

Answer: d
The clear() method clears all the key-value pairs in the dictionary.

Page  1  2  3  4  5

Python Interview Questions (MCQs)

We have divided these Python Questions and Answers into various parts based on the topics. Open the Topic of your choice and Practice these MCQs.

Python MCQs on Variables and Operators Python MCQs on Precedence and Associativity
Python MCQs on Data Type Python MCQs on Boolean
Python MCQs on Bitwise Operators Python MCQs on Formatting and Advance Formatting
Python MCQs on Decorators Python MCQs on While and For Loops
Python MCQs on List Python MCQs on List Comprehension
Python MCQs on String Python MCQs on Tuple
Python MCQs on SET Python MCQs on Dictionary
Python MCQs on Functions Python MCQs on Argument Parsing
Python MCQs on Global and Local Variables Python MCQs on Recursion
Python MCQs on Mapping Functions Python MCQs on Modules
Python MCQs on Regular Expressions Python MCQs on Files
Python MCQs on Overloading Python MCQs on Classes and Objects
Python MCQs on Inheritance Python MCQs on Polymorphism and Encapsulation
Python MCQs on Exception Handling

Leave a Comment