Python MCQs on Dictionary: SET 1

Python MCQs on Dictionary

In this section, we are going to introduce you to the most frequently asked Python MCQs on Dictionary. These Python MCQs on Dictionary will be going to help you in Interviews and Exams.

 

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

Check Answer

Answer: d
Dictionaries are created by specifying keys and values.

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

d = {“john”:40, “peter”:45}

a) “john”, 40, 45, and “peter”

b) “john” and “peter”

c) 40 and 45

d) d = (40:”john”, 45:”peter”)

Check Answer

Answer: b
Dictionaries appear in the form of keys and values.

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

Check Answer

Answer: a
In can be used to check if the key is int dictionary.

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

d1 = {“john”:40, “peter”:45}

d2 = {“john”:466, “peter”:45}

d1 == d2

a) True

b) False

c) None

d) Error

Check Answer

Answer: b
If d2 was initialized as d2 = d1 the answer would be true.

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

d1 = {“john”:40, “peter”:45}

d2 = {“john”:466, “peter”:45}

d1 > d2

a) True

b) False

c) Error

d) None

Check Answer

Answer: c
Arithmetic > operator cannot be used with dictionaries.

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

Check Answer

Answer: a
Execute in the shell to verify.

7. Suppose d = {“john”:40, “peter”:45}, to delete the entry for “john” what command do we use?

a) d.delete(“john”:40)

b) d.delete(“john”)

c) del d[“john”]

d) del d(“john”:40)

Check Answer

Answer: c
Execute in the shell to verify.

8. 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()

Check Answer

Answer: b
Execute in the shell to verify.

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

d = {“john”:40, “peter”:45}

print(list(d.keys()))

a) [“john”, “peter”]

b) [“john”:40, “peter”:45]

c) (“john”, “peter”)

d) (“john”:40, “peter”:45)

Check Answer

Answer: a
The output of the code shown above is a list containing only keys of the dictionary d, in the form of a list.

10. Suppose d = {“john”:40, “peter”:45}, what happens when we try to retrieve a value using the expression

d[“susan”]?

a) Since “susan” is not a value in the set, Python raises a KeyError exception

b) It is executed fine and no exception is raised, and it returns None

c) Since “susan” is not a key in the set, Python raises a KeyError exception

d) Since “susan” is not a key in the set, Python raises a syntax error

Check Answer

Answer: c
Execute in the shell to verify.

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