Vidyalelo
Python · all questions

MCQs for Python Dictionaries: Exam
practice.

Practice every MCQ with options. Use Show answers when you want the correct option and solution.

85

Questions

4/5

Page

Pick an option on a question to see the right answer and solution.

What will be the output of the following Python code?
>>> a={i: i*i for i in range(6)}
>>> a

Select an option to see the answer and solution.

What will be the output of the following Python code snippet?
a = {}
a[1] = 1
a['1'] = 2
a[1.0]=4
count = 0
for i in a:
    count += a[i]
print(count)

Select an option to see the answer and solution.

Which of the following is not a declaration of the dictionary?

Select an option to see the answer and solution.

What will be the output of the following Python code snippet?
d = {"john":40, "peter":45}
"john" in d

Select an option to see the answer and solution.

What will be the output of the following Python code snippet?
total={}
def insert(items):
    if items in total:
        total[items] += 1
    else:
        total[items] = 1
insert('Apple')
insert('Ball')
insert('Apple')
print (len(total))

Select an option to see the answer and solution.

If a is a dictionary with some key-value pairs, what does a.popitem() do?

Select an option to see the answer and solution.

What will be the output of the following Python code snippet?
d = {"john":40, "peter":45}
d["john"]

Select an option to see the answer and solution.

What will be the output of the following Python code snippet?
a={1:"A",2:"B",3:"C"}
print(a.get(5,4))

Select an option to see the answer and solution.

What will be the output of the following Python code snippet?
a={1:"A",2:"B",3:"C"}
print(a.setdefault(3))

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

What will be the output of the following Python code snippet?
a = {}
a[1] = 1
a['1'] = 2
a[1]=a[1]+1
count = 0
for i in a:
    count += a[i]
print(count)

Select an option to see the answer and solution.

What will be the output of the following Python code?
>>> import collections
>>> a=dict()
>>> a=collections.defaultdict(int)
>>> a[1]

Select an option to see the answer and solution.

Suppose d = {"john":40, "peter":45}, what happens when we try to retrieve a value using the expression d["susan"]?

Select an option to see the answer and solution.

What will be the output of the following Python code snippet?
a={}
a['a']=1
a['b']=[2,3,4]
print(a)

Select an option to see the answer and solution.

What will be the output of the following Python code?
>>> a={'B':5,'A':9,'C':7}
>>> sorted(a)

Select an option to see the answer and solution.

What will be the output of the following Python code?
>>> import collections
>>> b=dict()
>>> b=collections.defaultdict(lambda: 7)
>>> b[4]

Select an option to see the answer and solution.

What will be the output of the following Python code?
>>> a={"a":1,"b":2,"c":3}
>>> b=dict(zip(a.values(),a.keys()))
>>> b

Select an option to see the answer and solution.

Which of the following isn't true about dictionary keys?

Select an option to see the answer and solution.

What will be the output of the following Python code?
>>> a={i: 'A' + str(i) for i in range(5)}
>>> a

Select an option to see the answer and solution.

What will be the output of the following Python code?
>>> b={}
>>> all(b)

Select an option to see the answer and solution.