Font size
WorksheetsCOMPUTER DICTIONARY
Total questions: 22
Worksheet time: 12mins
You have the following dictionary definition:
d = {'foo': 100, 'bar': 200, 'baz': 300}
What method call will delete the entry whose value is 200?
delete d('bar')
d.pop("bar")
d.remove("bar")
None of the above
Which method will remove an element the Dictionary data type?
.pop()
.discard()
.remove()
Please select all correct ways to empty the following dictionary
student = {
"name": "Emma",
"class": 9,
"marks": 75
}
del student
del student[0:2]
student.clear()
Items are accessed by their position in a dictionary
True
False
_________________ method will not only delete the item from dictionary, but also return the deleted value.
del ( )
pop( )
Clear( )
________________method will returns number of key-value pairs in the given dictionary.
len( )
pop( )
del( )
keys( )
values( )
What would be the output of the following code snippet?
a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}
d_items = a_dict.items()
print(d_items)
('color', 'blue'), ('fruit', 'apple'), ('pet', 'dog')
dict_items([('color'), ('fruit'), ('pet')])
[('color', 'blue'), ('fruit', 'apple'), ('pet', 'dog')]
dict_items([('color', 'blue'), ('fruit', 'apple'), ('pet', 'dog')])
What is the output of the following code
dict1 = {"key1":1, "key2":2}
dict2 = {"key2":2, "key1":1}
print(dict1 == dict2)
True
False
Select correct ways to create an empty dictionary
sampleDict = {}
sampleDict = dict()
sampleDict = dict{}
Which of the following code deletes the key 103 and its value
del grades[103]
grades.pop(103)
del grades["103"]
grades.pop("103")
What would this out put? print(users[0][2])
23
ben
sam
smith
a={1:"A",2:"B"}
a.clear()
print(a)
None
{ None:None, None:None, None:None}
{1:None, 2:None, 3:None}
{ }
Select the all correct way to remove the key marks from a dictionary
student = { "name": "Emma", "class": 9, "marks": 75 }
student.pop("marks")
del student["marks"]
student.remove("marks")
What will be the result of
d={'a':1,'b':2,"c":3}
print(d[0])
a
b
c
Error
What will be printed by the following statement?
d={"cat":12,"dog":6,"elephant":23,"bear":20}
print("dog" in d)
True
False
Error
None
Which one below is a correct dictionary ?
>>> age = {‘Mary’: 10, ‘Sanjay’: 8}
>>> age = (‘Mary’: 10, ‘Sanjay’: 8)
>>> age = {‘Mary’, 10: ‘Sanjay’, 8}
>>> age = {‘Mary’, '10': ‘Sanjay’, '8'}
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=" ")
1 A 2 B 3 C
1 2 3
A B C
1:”A” 2:”B” 3:”C”
What will be the output of the following Python code snippet?
>>> a={1:"A",2:"B",3:"C"}
>>> del a
method del doesn’t exist for the dictionary
del deletes the values in the dictionary
del deletes the entire dictionary
del deletes the keys in the dictionary
dl={1:10, 2:20, 3:30, 4:40}
d2={5:50, 6:60, 7:70}
dl.update (d2)
print (dl)
[(1, 10), (2, 20), (3, 30), (4, 40), (5, 50)]
{1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60, 7: 70}
{1:10, 2: 20, 4: 40, 5: 50, 6: 60, 7: 70}
[1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60, 7: 70]
Select the all correct way to remove the key ‘Salary‘ from a dictionary
emp = { "name": "John", "Designation": "Manager", "Salary": 90000,}
emp.pop(“Salary”)
del emp[“Salary”]
emp.popitem()
emp.remove(“key2”)
To view all the keys in the dictionary which function is used?
dict.function()
dict.keys()
dict.values()
I don't Know
Which of the following is not a valid way to define this dictionary in Python:
d = dict([
('foo', 100),
('bar', 200),
('baz', 300)
])
d = {}
d['foo'] = 100
d['bar'] = 200
d['baz'] = 300
d = dict(foo=100, bar=200, baz=300)
d = {'foo': 100, 'bar': 200, 'baz': 300}
d = { ('foo', 100), ('bar', 200), ('baz', 300) }
