CBSE CS

cbse cs logo

Home

MCQ on Dictionary in Python

Set 1: MCQ on Dictionary in Python


Question 1. Select the correct ways to get the value of marks key.

student = {
"name": "Emma",
"class": 9,
"marks": 75
}

Option a.) m = student.get(2)
Option b.) m = student.get('marks')
Option c.) m = student[2])
Option d.) none
Answer

#Ans. b





Question 2. What is the output of the following code

dict1 = {"key1":1, "key2":2}
dict2 = {"key2":2, "key1":1}
print(dict1 == dict2)

Option a.) True
Option b.) False
Answer

#Ans. a





Question 3. What is the output of the following
sampleDict = dict([
('first', 1),
('second', 2),
('third', 3)
])
print(sampleDict)

Option a.) [ ('first', 100), ('second', 200), ('third', 300) ]
Option b.) Options: SyntaxError: invalid syntax
Option c.) {'first': 1, 'second': 2, 'third': 3}
Option d.) none
Answer

#Ans. c





Question 4. In Python, Dictionaries are immutable


Option a.) False
Option b.) True
Answer

#Ans. a




50+ MCQ on Dictionary in Python for class 11 and 12


Question 5. Select the all correct way to remove the key marks from a dictionary

student = {
"name": "Emma",
"class": 9,
"marks": 75
}

Option a.) student.pop("marks")
Option b.) del student["marks"]
Option c.) student.remove("marks")
Option d.) a and b
Answer

#Ans. d





Question 6. Dictionary keys must be immutable


Option a.) True
Option b.) False
Answer

#Ans. a





Question 7. Select correct ways to create an empty dictionary


Option a.) sampleDict = {}
Option b.) sampleDict = dict()
Option c.) sampleDict = dict{}
Option d.) a and b
Answer

#Ans. d





Question 8. What is the output of the following dictionary operation

dict1 = {"name": "Mike", "salary": 8000}
temp = dict1.pop("age")
print(temp)

Option a.) KeyError: 'age'
Option b.) None
Answer

#Ans. a





Question 9. Select the correct way to print Emma's age.

student = {1: {'name': 'Emma', 'age': '27', 'sex': 'Female'},
2: {'name': 'Mike', 'age': '22', 'sex': 'Male'}}

Option a.) student[0][1]
Option b.) student[1]["age"]
Option c.) student[0]["age"]
Option d.) none
Answer

#Ans. b.





Question 10. Select all correct ways to copy a dictionary in Python


Option a.) dict2 = dict1.copy()
Option b.) dict2 = dict(dict1)
Option c.) dict2 = dict1
Option d.) a and b
Answer

#Ans. d





Question 11. Please select all correct ways to empty the following dictionary

student = {"name": "Emma",
"class": 9,
"marks": 75 }


Option a.) del student
Option b.) del student[0:2]
Option c.) student.clear()
Option d.) none
Answer

#Ans. c





Question 12. What is the output of the following dictionary operation

dict1 = {"name": "Mike", "salary": 8000}
temp = dict1.get("age")
print(temp)


Option a.) KeyError: 'age'
Option b.) None
Answer

#Ans. b





Question 13. Select the correct way to access the value of a history subject

sampleDict = {
"class":{
"student":{
"name":"Mike",
"marks":{
"physics":70,
"history":80
}
}
}
}

Option a.) sampleDict['class']['student']['marks']['history']
Option b.) sampleDict['class']['student']['marks'][1]
Option c.) sampleDict['class'][0]['marks']['history']
Option d.) none
Answer

#Ans. a




50+ MCQ on Dictionary in Python for class 11 and 12


Question 14. Items are accessed by their position in a dictionary and All the keys in a dictionary must be of the same type.


Option a.) True
Option b.) False
Answer

#Ans. b




Set 2: MCQ on Dictionary in Python

Question 1. Which of the following statements create a dictionary?

Option a.) d = {}
Option b.) d = {'john':40, 'peter':45}
Option c.) d = {40:'john', 45:'peter'}
Option d.) All of the mentioned
Answer

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



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

d = {"john":40, "peter":45}
keys are?
Option a.) 'john', 40, 45, and 'peter'
Option b.) 'john' and 'peter'
Option c.) 40 and 45
Option d.) d = (40:'john', 45:'peter')

Answer

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




advertisement

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

d = {"john":40, "peter":45}
"john" in d

Option a.) True
Option b.) False
Option c.) None
Option d.) Error

Answer

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




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

d1 = {"john":40, "peter":45}
d2 = {"john":466, "peter":45}
d1 == d2

Option a.) True
Option b.) False
Option c.) None
Option d.) Error

Answer

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




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

d1 = {"john":40, "peter":45}
d2 = {"john":466, "peter":45}
d1 > d2

Option a.) True
Option b.) False
Option c.) Error
Option d.) None

Answer

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





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

d = {"john":40, "peter":45}
d["john"]

Option a.) 40
Option b.) 45
Option c.) 'john'
Option d.) 'peter'

Answer

#Answer: a Explanation: Execute in the shell to verify.





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

Option a.) d.delete('john':40)
Option b.) d.delete('john')
Option c.) del d['john']
Option d.) del d('john':40)

Answer

#Answer: c Explanation: Execute in the shell to verify.





Question 8. Suppose d = {'john':40, 'peter':45}. To obtain the number of entries in dictionary which command do we use?

Option a.) d.size()
Option b.) len(d)
Option c.) size(d)
Option d.) d.len()

Answer

#Answer: b Explanation: Execute in the shell to verify.





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

d = {"john":40, "peter":45}
print(list(d.keys()))

Option a.) ['john', 'peter']
Option b.) ['john':40, 'peter':45]
Option c.) ('john', 'peter')
Option d.) ('john':40, 'peter':45)

Answer

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





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

Option a.) Since 'susan' is not a value in the set, Python raises a KeyError exception
Option b.) It is executed fine and no exception is raised, and it returns None
Option c.) Since 'susan' is not a key in the set, Python raises a KeyError exception
Option d.) Since 'susan' is not a key in the set, Python raises a syntax error

Answer

#Answer: c Explanation: Execute in the shell to verify.



Set 3: MCQ on Dictionary in Python


Question 1. Which of the statements about dictionary values if false?

Option a.) More than one key can have the same value
Option b.) The values of the dictionary can be accessed as dict[key]
Option c.) Values of a dictionary must be unique
Option d.) Values of a dictionary can be a mixture of letters and numbers

Answer

#Answer: c Explanation: More than one key can have the same value.





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

>>> a={1:"A",2:"B",3:"C"}
>>> del a

Option a.) method del doesn’t exist for the dictionary
Option b.) del deletes the values in the dictionary
Option c.) del deletes the entire dictionary
Option d.) del deletes the keys in the dictionary

Answer

#Answer: c Explanation: del deletes the entire dictionary and any further attempt to access it will throw an error.




advertisement

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

Option a.) Removes an arbitrary element
Option b.) Removes all the key-value pairs
Option c.) Removes the key-value pair for the key given as an argument
Option d.) Invalid method for dictionary

Answer

#Answer: a Explanation: The method popitem() removes a random key-value pair.





Question 4. 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))

Option a.) 3
Option b.) 1
Option c.) 2
Option d.) 0

Answer

#Answer: c Explanation: The insert() function counts the number of occurrences of the item being inserted into the dictionary. There are only 2 keys present since the key ‘Apple’ is repeated. Thus, the length of the dictionary is 2.





Question 5. 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)

Option a.) 1
Option b.) 2
Option c.) 4
Option d.) Error, the keys can’t be a mixture of letters and numbers

Answer

#Answer: c Explanation: The above piece of code basically finds the sum of the values of keys.





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

numbers = {}
letters = {}
comb = {}
numbers[1] = 56
numbers[3] = 7
letters[4] = 'B'
comb['Numbers'] = numbers
comb['Letters'] = letters
print(comb)

Option a.) Error, dictionary in a dictionary can’t exist
Option b.) ‘Numbers’: {1: 56, 3: 7}
Option c.) {‘Numbers’: {1: 56}, ‘Letters’: {4: ‘B’}}
Option d.) {‘Numbers’: {1: 56, 3: 7}, ‘Letters’: {4: ‘B’}}

Answer

#Answer: d Explanation: Dictionary in a dictionary can exist.





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

test = {1:'A', 2:'B', 3:'C'}
test = {}
print(len(test))

Option a.) 0
Option b.) None
Option c.) 3
Option d.) An exception is thrown

Answer

#Answer: a Explanation: In the second line of code, the dictionary becomes an empty dictionary. Thus, length=0.





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

test = {1:'A', 2:'B', 3:'C'}
del test[1]
test[1] = 'D'
del test[2]
print(len(test))

Option a.) 0
Option b.) 2
Option c.) Error as the key-value pair of 1:’A’ is already deleted
Option d.) 1

Answer

#Answer: b Explanation: After the key-value pair of 1:’A’ is deleted, the key-value pair of 1:’D’ is added.





Question 9. 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)

Option a.) An exception is thrown
Option b.) 3
Option c.) 6
Option d.) 2

Answer

#Answer: c Explanation: The value of key 1 is 4 since 1 and 1.0 are the same. Then, the function count() gives the sum of all the values of the keys (2+4).





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

a={}
a['a']=1
a['b']=[2,3,4]
print(a)

Option a.) Exception is thrown
Option b.) {‘b’: [2], ‘a’: 1}
Option c.) {‘b’: [2], ‘a’: [3]}
Option d.) {‘b’: [2, 3, 4], ‘a’: 1}

Answer

#Answer: d Explanation: Mutable members can be used as the values of the dictionary but they cannot be used as the keys of the dictionary.





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

>>>import collections
>>> a=collections.Counter([1,1,2,3,3,4,4,4])
>>> a

Option a.) {1,2,3,4}
Option b.) Counter({4, 1, 3, 2})
Option c.) Counter({4: 3, 1: 2, 3: 2, 2: 1})
Option d.) {4: 3, 1: 2, 3: 2, 2: 1}

Answer

#Answer: c Explanation: The statement a=collections.OrderedDict() generates a dictionary with the number as the key and the count of times the number appears as the value.





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

>>>import collections
>>> b=collections.Counter([2,2,3,4,4,4])
>>> b.most_common(1)

Option a.) Counter({4: 3, 2: 2, 3: 1})
Option b.) {3:1}
Option c.) {4:3}
Option d.) [(4, 3)]

Answer

#Answer: d Explanation: The most_common() method returns the n number key-value pairs where the value is the most recurring.





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

>>>import collections
>>> b=collections.Counter([2,2,3,4,4,4])
>>> b.most_common(1)

Option a.) Counter({4: 3, 2: 2, 3: 1})
Option b.) {3:1}
Option c.) {4:3}
Option d.) [(4, 3)]


Answer

#Answer: d Explanation: The most_common() method returns the n number key-value pairs where the value is the most recurring.





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

>>> import collections
>>> a=collections.Counter([2,2,3,3,3,4])
>>> b=collections.Counter([2,2,3,4,4])
>>> a|b

Option a.) Counter({3: 3, 2: 2, 4: 2})
Option b.) Counter({2: 2, 3: 1, 4: 1})
Option c.) Counter({3: 2})
Option d.) Counter({4: 1})


Answer

#Answer: a Explanation: a|b returns the pair of keys and the highest recurring value.





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

>>> import collections
>>> a=collections.Counter([3,3,4,5])
>>> b=collections.Counter([3,4,4,5,5,5])
>>> a&b

Option a.) Counter({3: 12, 4: 1, 5: 1})
Option b.) Counter({3: 1, 4: 1, 5: 1})
Option c.) Counter({4: 2})
Option d.) Counter({5: 1})

Answer

#Answer: b Explanation: a&b returns the pair of keys and the lowest recurring value.



Set 4: MCQ on Dictionary in Python



Question 1. Which of these about a dictionary is false?

Option a.) The values of a dictionary can be accessed using keys
Option b.) The keys of a dictionary can be accessed using values
Option c.) Dictionaries aren't ordered
Option d.) Dictionaries are mutable

Answer

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





Question 2. Which of the following is not a declaration of the dictionary?

Option a.) {1: 'A', 2: 'B'}
Option b.) dict([[1,'A'],[2,'B']])
Option c.) {1,'A',2'B'}
Option d.) { }

Answer

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





Question 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=" ")

Option a.) 1 A 2 B 3 C
Option b.) 1 2 3
Option c.) A B C
Option d.) 1:'A' 2:'B' 3:'C'

Answer

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





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

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

Option a.) 1
Option b.) A
Option c.) 4
Option d.) Invalid syntax for get method

Answer

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





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

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

Option a.) Error, invalid syntax
Option b.) A
Option c.) 5
Option d.) 4

Answer

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





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

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

Option a.) {1: 'A', 2: 'B', 3: 'C'}
Option b.) C
Option c.) {1: 3, 2: 3, 3: 3}
Option d.) No method called setdefault() exists for dictionary

Answer

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





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

a={1:"A",2:"B",3:"C"}

Option a.) setdefault(4,"D")
print(a.

Option a.) {1: 'A', 2: 'B', 3: 'C', 4: 'D'}
Option b.) None
Option c.) Error
Option d.) [1,3,6,10]

Answer

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





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

a={1:"A",2:"B",3:"C"}
b={4:"D",5:"E"}

Option a.) update(b.
print(a.

Option a.) {1: 'A', 2: 'B', 3: 'C'}
Option b.) Method update() doesn't exist for dictionaries
Option c.) {1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E'}
Option d.) {4: 'D', 5: 'E'}

Answer

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





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

Option a.) Error, copy() method doesn't exist for dictionaries
Option b.) {1: 'A', 2: 'B', 3: 'C'}
Option c.) {1: 'A', 2: 'D', 3: 'C'}
Option d.) 'None' is printed

Answer

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





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

a={1:"A",2:"B",3:"C"}

Option a.) clear()
print(a.

Option a.) None
Option b.) { None:None, None:None, None:None}
Option c.) {1:None, 2:None, 3:None}
Option d.) { }

Answer

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





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

Option a.) More than one key isn't allowed
Option b.) Keys must be immutable
Option c.) Keys must be integers
Option d.) When duplicate keys encountered, the last assignment wins

Answer

#Answer: c Explanation: Keys of a dictionary may be any data type that is immutable.





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

a={1:5,2:3,3:4}

Option a.) pop(3)
print(a)

Option a.) {1: 5}
Option b.) {1: 5, 2: 3}
Option c.) Error, syntax error for pop() method
Option d.) {1: 5, 3: 4}

Answer

#Answer: b Explanation: pop() method removes the key-value pair for the key mentioned in the pop() method.





Question 13. What will be the output of the following Python code?

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

Option a.) 9
Option b.) 3
Option c.) Too many arguments for pop() method
Option d.) 4

Answer

#Answer: a Explanation: 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.





Question 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=" ")

Option a.) 1 2 3
Option b.) 'A' 'B' 'C'
Option c.) 1 'A' 2 'B' 3 'C'
Option d.) Error, it should be: for i in a.items():

Answer

#Answer: a Explanation: The variable i iterates over the keys of the dictionary and hence the keys are printed.





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

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

Option a.) Syntax error
Option b.) dict_items([('A'), ('B'), ('C')])
Option c.) dict_items([(1,2,3)])
Option d.) dict_items([(1, 'A'), (2, 'B'), (3, 'C')])

Answer

#Answer: d Explanation: The method items() returns list of tuples with each tuple having a key-value pair.



Set-5 MCQ on dictionary in Python


Question 1. Which of the following statements create a dictionary?

Option a.) d = {}
Option b.) d = {"john":40, "peter":45}
Option c.) d = {40:"john", 45:"peter"}
Option d.) All of the mentioned
Answer

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





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

d = {"john":40, "peter":45}

Option a.) "john", 40, 45, and "peter"
Option b.) "john" and "peter"
Option c.) 40 and 45
Option d.) d = (40:"john", 45:"peter")
Answer

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





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

d = {"john":40, "peter":45}
"john" in d

Option a.) True
Option b.) False
Option c.) None
Option d.) Error
Answer

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





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

d1 = {"john":40, "peter":45}
d2 = {"john":466, "peter":45}
d1 == d2

Option a.) True
Option b.) False
Option c.) None
Option d.) Error
Answer

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





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

d1 = {"john":40, "peter":45}
d2 = {"john":466, "peter":45}
d1 > d2

Option a.) True
Option b.) False
Option c.) Error
Option d.) None
Answer

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





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

d = {"john":40, "peter":45}
d["john"]

Option a.) 40
Option b.) 45
Option c.) "john"
Option d.) "peter"
Answer

#Answer: a Clarification: Execute in the shell to verify.





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

Option a.) d.delete("john":40)
Option b.) d.delete("john")
Option c.) del d["john"]
Option d.) del d("john":40)
Answer

#Answer: c Clarification: Execute in the shell to verify.





Question 8. Suppose d = {"john":40, "peter":45}. To obtain the number of entries in dictionary which command do we use?

Option a.) d.size()
Option b.) len(d)
Option c.) size(d)
Option d.) d.len()
Answer

#Answer: b Clarification: Execute in the shell to verify.





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

d = {"john":40, "peter":45}
print(list(d.keys()))

Option a.) ["john", "peter"]
Option b.) ["john":40, "peter":45]
Option c.) ("john", "peter")
Option d.) ("john":40, "peter":45)
Answer

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





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

Option a.) Since "susan" is not a value in the set, Python raises a KeyError exception
Option b.) It is executed fine and no exception is raised, and it returns None
Option c.) Since "susan" is not a key in the set, Python raises a KeyError exception
Option d.) Since "susan" is not a key in the set, Python raises a syntax error
Answer

#Answer: c Clarification: Execute in the shell to verify.



error: Content is protected !!