Python Dictionary Questions: Dictionary MCQs in Python

Python Dictionary Questions. MCQs on Dictionary in Python Programming Language.

 

Python Dictionary Questions

 

 

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’)])

 

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

 

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():

 

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)

 

Python Dictionary Questions

 

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

 

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

 

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}

 

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)

 

Python Dictionary Questions

 

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

 

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”

 

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

 

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

 

Python Dictionary Questions

 

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

 

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

 

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

Leave a Comment

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

error: Content is protected !!