Class 11 Comp Sci Assignmeent Module 1 Dictionary
Class 11 Comp Sci Assignmeent Module 1 Dictionary
Class 11 Comp Sci Assignmeent Module 1 Dictionary
Points to Revise
- Dictionary is a mapping data type where each element is key-value pair.
- The key-value pair is called an item where each key is separated from its value
by a colon.
- Multiple items (key value pair) are put inside curly braces where each item is
separated by a ,. Keys are unique and used within [] with the name of the
dictionary object to access the value
- Keys are of immutable type but values can be mutable.
- Dictionaries are mutable which implies that the contents of the dictionary can be
changed after it has been created.
- The membership operator in checks if the key is present in the dictionary and
returns True, else it returns False. The in operator when used with a for loop will
return the key of each element of the dictionary object.
- Dictionary methods/built-in functions are len(),dict(), keys(),values(),items(),
- get(), update(),clear(), copy(), fromkeys(), setdefault(),max(),min(),sum(),pop(),
- popitem(),sorted() and del statement.
- len() Returns the number of elements (key-value pairs) in the dictionary.
- dict() is used to create an empty dictionary.
- clear() removes all elements from the dictionary.
- copy() creates a copy of the dictionary object
- items() returns the dictionary items in the key-value pair form.
- keys() returns all the keys of the dictionary.
- values() returns all the values of the dictionary
- get() returns a value for the given key.
- update() merges key:value pairs from new dictionary into the original dictionary
by adding or replacing the values as needed.
- fromkeys() create a new dictionary from a sequence containing all keys and a
common value, which will be assigned to all the keys.
- max() returns maximum key of the dictionary.
- min() returns minimum key of the dictionary.
- sum() returns sum of all the keys of the dictionary.
- pop() takes the key of a dictionary as argument and deletes the item with the
matching key from the dictionaryand displays the deleted value.
- setdefault() inserts a new key:value pair in the dictionary only if the key does not
exist and it will returns the current value of the element inserted.
- popitem() method removes and returns the key:value pair which was entered last
in the dictionary.
- sorted() returns a list which is sorted on the basis of keys of a dictionary.
- del statement is used to remove an item in form of key:value pair from a
dictionary.
Assignment - Dictionary
1. The key and value of a dictionary object is separated by ___________ symbol.
a. ,
b. ;
c. –
d. :
2. State True or False
All sequence data type can be used as a key and value in the dictionary object.
3. Which of the following is the correct statement to access the value of an element
of dictionary through key?
a. <dictionary> [<key>]
b. <dictionary> <key>
c. <dictionary>.<key>
d. <dictionary>-<key>
4. Which of the following is not a valid declaration of the dictionary?
a. {1: ‘A’, 2: ‘B’} ` b. dict([[1,”A”],[2,”B”]])
c. a = {[1,2]:"A",[2,1]:"B",3:"C"} d. a = {(1,2):"A",(2,1):"B",3:"C"}
5. Given two dictionary objects
d1 = {101:"COBOL",102:"BASIC",103:"C"}
d2 = {104:"C++",103:"HTMl",101:"Python"}
Which of the following statements will change the content of object d2 as shown
d2 = {104: 'C++', 103: 'C', 101: 'COBOL', 102: 'BASIC'}
a. d2.update(dl)
b. d1 += d2
c. d1.concatenate (d2)
d. dl.merge (d2)
6. Which of the following operator can be used with dictionary objects
a. >= b. += c. == d. <=
7. Which among the following will create an empty dictionary?
a. D = dict()
b. D = dict{}
c. D = ()
d. D = {}
8. For a dictionary object, d = {"Ram":40, "Peter":45}, which statement will delete
the entry for “Ram”?
a) d.delete("Ram":40)
b) d.delete("Ram")
c) del d["Ram"]
d) del d("john":40)
9. For a dictionary object d1 = {12:100,10:13}, the statement print(13 in d1) will
display ______
a. 10
b. True
c. False
d. 13
10. The dictionary operation that takes a key and finds the corresponding value is
known as
a. Mapping
b. Packing
c. Lookup
d. None of the above
11. Assertion(A):
The len() is a built in function of Python which returns the number of elements
present in dictionary object
Reasoning(R):
For a dictionary object d = {"Ram":40, "Peter":45, "Salma":80}, the statement
print(len(d)) will display 6
Mark the correct choice as
(a) Both A and R are true and R is the correct explanation for A
(b) Both A and R are true and R is not the correct explanation for A
(c) A is True but R is False
(d) A is false but R is True
12. Assertion(A):
The setdefault() method inserts a new key:value pair in a dictionary object if the
key:value pair does not exist
Reasoning(R)
If the key:value pair already exists, it raises an exception
Mark the correct choice as
(a) Both A and R are true and R is the correct explanation for A
(b) Both A and R are true and R is not the correct explanation for A
(c) A is True but R is False
(d) A is false but R is True
13. Assertion(A):
A dictionary object has a key and value part where the key is used to access the
value of each dictionary element
Reasoning(R):
Literals of all data types can be used as the key of a dictionary object.
Mark the correct choice as
a. Both A and R are true and R is the correct explanation for A
b. Both A and R are true and R is not the correct explanation for A
c. A is True but R is False
d. A is false but R is True
14. Name the function which can be used for the following operations
a. To remove all elements present in a dictionary object
b. To return the value part of a given key
c. To display the maximum key value of a dictionary object
15. For a dictionary object d = {"Ram":40, "Peter":45, "Salma":80}, Give statements
using two different methods to delete element "Salma":80 from d.
16. Predict the output of the following code:
17 Differentiate between :
a. clear() and delete
b. fromkeys () and keys()
18. Give one similarity and one difference between :
a. update() and setdefault()
b. pop() and popitem()
19. What will be the output of the following
student = {}
student[1] = [31,29,33]
student[2] = (18,25,31)
student[1][1] =100
print(student)
a. {1: [31, 100, 33], 2: (18, 25, 31)}
b. {100: [31, 29, 33], 2: (18, 25, 31)}
c. {100: [31, 29, 33], 2: (18, 100, 31)}
d. It will result an error.
20. Predict the output of the following code:
List2= [10,12,13,15]
dict1= {}
ctr=0
for el in List2:
n=5
dict1[el] = n
ctr += 1
if n%2 ==0:
dict1[el] = dict1[el] + ctr
else:
dict1[el] = dict11[el] - ctr
n+=1
print(dict1)
21. Predict the output of the following code: