Class 11 Comp Sci Assignmeent Module 1 Dictionary

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Computer Science Capsule 2 - 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:

22. Write the output of the code given below:


dict1={1:["Rohit",20], 2:["Siya",90]}
dict2={1:["Rahul",95], 5:["Rajan",80]}
dict1.update(dict2)
print(dict1.values())
23. Predict the output of the following code:
dict1 = {10:"p",20:"p",30:"q",40:"s"}
str1, s = "",0
l = list(dict1)
p=0
for k, it in dict1.items():
if it not in str1:
str1 += it * p
else:
s += l[p]*k+1
p += 1
s+=max(dict1)
print(str1,s)
24. For a given dictionary object d1 = {1:"Ram", 2:"Sita"}, Explain the difference
between the following statement1 and Statement 2
d2 = d1
d2 = d1.copy()
25. Write a program to create a dictionary d1 such that the key field will have integers
starting from 1 to n where n is the number of elements. The value part of different
elements inputted by the user can be of data types - int, float, bool, str.
Calculate the frequency of each data type entered in the value part of each element
of d1 and create another dictionary d2 from d1, which should have data types as
it’s keys and its values as frequency of each data type.
Example: If d1 = {1: "pen", 2: 5, 3: 10, 4: True, 5: 7.8}
d2 should be {"str": 1, "int": 2, "bool": 1, "float":1}
25. Write a program to create a dictionary Coach having coach details. Each element
of dictionary has coach id as key and name, sports, experience and fees. The
value part is represented as a list. The fees is to be assigned as per the following
criteria
Experience Fees
<3 2000
>=3 &<5 4000
>=5 6000
Write a menu driven program with following options:
1. Display the Count of all coaches of a user given sport.
2. Count the number of coaches whose Fees is more than 5000
3. Search and display the details of a user given coach id
4. Count and display the details of all coaches whose experience is more
than 6 years
26. Give output of the following statements:
D1 = dict.fromkeys([10,12,14],100)
print(D1)
D2 = dict.fromkeys([10,12,13])
print(D2)
D3 = dict.fromkeys([10,12,14],[1,2,3])
print(D3)
27. Explain two ways to create empty dictionary with suitable example.
28. What will be the return data type of the following method/function when they are
used on dictionary object
a. items()
b. copy()
c. len()
d. sorted()
29. For a dictionary object d1 = {10:120,11:230,13:240}, what will be the output of the
statement : print(max(d1) + sum(d1))
30. Dictionary is mutable data type. Give the suitable example to prove mutability
feature of dictionary.
31. Explain the two usage of in operator for dictionary object with example.

You might also like