Unseen Passage

For Class 4 to Class 12

Dictionaries Class 11 Computer Science Important Questions

Please refer to Dictionaries Class 11 Computer Science Important Questions with answers below. These solved questions for Chapter 9 Dictionaries in NCERT Book for Class 11 Computer Science have been prepared based on the latest syllabus and examination guidelines issued by CBSE, NCERT, and KVS. Students should learn these solved problems properly as these will help them to get better marks in your class tests and examinations. You will also be able to understand how to write answers properly. Revise these questions and answers regularly. We have provided Notes for Class 11 Computer Science for all chapters in your textbooks.

Important Questions Class 11 Computer Science Chapter 9 Dictionaries

All Dictionaries Class 11 Computer Science Important Questions provided below have been prepared by expert teachers of Standard 11 Computer Science. Please learn them and let us know if you have any questions.

Short Answer Type Questions

Question: Find the output.
Student = {1 : ‘Aman’, 2 : ‘Bablu’, 3 : ‘Chandan’}
Student [2] = ‘Sahil’
print(Student)
Student[4] = ‘Aasha’
print (Student)
Answer: Output
{1 : ‘Aman’, 2 : ‘Sahil’, 3 : ‘Chandan’}
{1 : ‘Aman’, 2 : ‘Sahil’, 3 : ‘Chandan’, 4 : ‘Aasha’}

Question: How can you add following data in empty dictionary?
Keys Values
A Agra
B Bengluru
C Chennai
D Delhi
Answer: dic = {}
dic[‘A’] = ‘Agra’
dic[‘B’] = ‘Bengluru’
dic[‘C’] = ‘Chennai’
dic[‘D’] = ‘Delhi’

Question: What are in and not in membership operators in dictionary?
Answer: in and not in membership operators are used with dictionary. These operators check whether a specific key is present in dictionary or not. If it is present then it will give True, otherwise False.
Syntax key in dictionary_name key not in dictionary_name

Question: Find the output.
dic = {‘data1’ : 200, ‘data2’ : 56, ‘data3’: 47}
result = 1
for key in dic:
result = result*dic[key]
print(result)
Answer: Output
526400

Question: What is the advantage of pop( ) method over a del keyword?
Answer: Advantage of pop( ) method over a del keyword is that it provides the mechanism to print desired value if tried to remove a non-existing dictionary pair.
Syntax
dictionary_name.pop(key, ‘Text’)

Question: Find the output of the following code.
my_dict = {‘data1’ : 200, ‘data2’ : 154, ‘data3’:277}
print(sum(my_dict.values()))
Answer: Output
631

Question: Write the short note on
(i) sorted( )
(ii) fromkeys( )
Answer: (i) sorted() This method returns a sorted sequence of the keys in the dictionary.
(ii) fromkeys () This method creates a new dictionary from the given sequence of elements with a value provided by the user.

Question: Predict the output.
dic = {1 : [45, 89, 65], ‘A’ : (23, 45, 6)}
print(dic.keys())
print(dic.values())
Answer: Output
dict_keys([‘A’, 1])
dict_values ([[45, 89, 65], (23, 45, 6)])

Question: Find the output.
dic = {‘data1’ : 200, ‘data2’ : 56, ‘data3’: 47}
result = 1
for key in dic:
result = result*dic[key]
print(result)
Answer: Output
526400

Question:Predict the output.
dic = {‘a’ : 1, ‘b’ : 2, ‘c’ : 3, ‘d’ : 4}
print(dic)
if ‘a’ in dic :
del dic[‘a’]
print(dic)
Answer: Output
{‘d’ : 4, ‘a’ : 1, ‘c’ : 3, ‘b’ : 2}
{‘d’ : 4, ‘c’ : 3, ‘b’ : 2}

Question: Read the code shown below and pick out the keys.
d = {“Ansh” : 18, “Varsha” : 20}
Answer: “Ansh” and “Varsha”

Question:  Write the following methods.
(i) max ( ) (ii) min ( )
Answer: (i) max () This method is used to return the maximum key from the dictionary.
Syntax max(dict)
(ii) min ( ) This method is used to return the minimum key from the dictionary.
Syntax min (dict)

Question: What will be the output?
dic = {“Ansh” : 25, “Ritu” : 26}
print (list (dic.keys())
Answer: [‘Ansh’, ‘Ritu’]

Question: What is the use of len( ) method in dictionary?
Answer: len( ) method is used to return the total length of the dictionary.
Syntax len(dictionary_name)
For example,
>>>dic = {1 : ‘One’, 2 : ‘Two’,
3 : ‘Three’, 4 : ‘Four’}
>>>len(dic)
4

Question: Define the popitem ( ) method with its syntax.
Answer: popitem ( ) method in dictionary helps to achieve similar purpose. It removes the arbitrary key value pair from the dictionary and returns it as a tuple. There is an update for this method from Python version 3.7 only.
Syntax dict.popitem( )

Question: Define clear( ) method in dictionary.
Answer: clear( ) method is used to remove the elements of the dictionary. It produces an empty dictionary. It will only delete elements not a dictionary. It does not take any parameter and does not return any value.
Syntax dictionary_name.clear()
For example,
>>>dic = {1 : ‘One’, 2 : ‘Two’}
>>>dic.clear()
>>>dic
{}

Question: Give an example to iterate over (traversing) a
dictionary through all keys value pair?
Answer: dic = {1 : ‘One’, 2 : ‘Two’, 3 : ‘Three’, 4 :‘Four’}
print (“Keys : Values”)
for i in dic :
print (i, “:”, dic[i])
Output
Keys : Values
1 : One
2 : Two
3 : Three
4 : Four

Question: Write about the setdefault ( ) method with an example.
Answer: setdefault ( ) method returns the value of a key (if the key is in dictionary). If not, it inserts key with a value to the dictionary.
Syntax
dict.setdefault (Key [, default_value])
For example,
dic = {‘Anu’ : 20, ‘Rahul’ : 25}
dic1 = dic.setdefault(‘Anu’)
print (‘Key : ’, dic1)
Output
Key : 20

Question: When to use tuple or dictionary in Python? Give some examples of programming situations mentioning their usefulness.
Answer: Tuples are used to store the data which is not intended to change during the course of execution of the program.
For example, if the name of months is needed in a program, then the same can be stored in the tuple as generally, the names will either be iterated for a loop or referenced sometimes during the execution of the program.
Dictionary is used to store associative data like student’s roll no. and the student’s name. Here, the roll no. will act as a key to find the corresponding student’s name. The position of the data does not matter as the data can easily be searched by using the corresponding key.

Question: Write a Python program to create a dictionary from a string.
Note: Track the count of the letters from the string.
Sample string : ‘w3resource’
Expected output : {‘3’: 1, ‘s’: 1, ‘r’: 2, ‘u’: 1, ‘w’: 1,
‘c’: 1, ‘e’: 2, ‘o’: 1}
Answer: st = input(“Enter a string: ”)
dic = {}
for ch in st:
if ch in dic:
dic[ch] += 1
else:
dic[ch] = 1
for key in dic:
print(key,‘:’,dic[key])

Question: Write a Python program to find the highest 2 values in a dictionary.
Answer: dict1={‘One’:65,‘Two’:12,‘Three’:89,‘Four’:65,‘Fi
ve’:56}
h=0
sh=0
for key in dict1:
if dict1[key]>h:
sh=h
h=dict1[key]
print(‘highest value’,h)
print(‘second highest value’,sh)

 Long Answer Type Questions

Question: Write the short note on following with an example.
(i) update( )
(ii) len( )
Answer: (i) update( ) This method is used to update the dictionary with the elements from the another dictionary object or from an iterable of key/value pairs.
Syntax
dictionary_name1.update (dictionary_name2)
e.g.
>>>Student = {1:‘Ashwani’, 2:‘Shiva’, 3:‘Sourabh’,
4:‘Harsh’}
>>>Student [2] = ‘Manish’
>>>Student
Output
1 : ‘Ashwani’, 2 : ‘Manish’, 3 : ‘Sourabh’, 4 : ‘Harsh’
(ii) len() This method is used to return the total length of the dictionary or number of keys.
Syntax
len(dictionary_name)
e.g.
dic = {‘A’ : ‘One’, ‘B’ : ‘Two’, ‘C’ : ‘Four’, ‘D’ :
‘Four’}
dic1 = {‘A’ : ‘One’, ‘B’ : ‘Two, ‘C’ : ‘Three’, ‘D’ :
‘Four’, ‘E’ : ‘Five’}
dic.update(dic1)
print(dic)
a = len(dic)
print(‘The length is’, a)
Output
{‘A’ : ‘One’, ‘B’ : ‘Two’, ‘C’ : ‘Three’, ‘D’ : ‘Four’, ‘E’ :
‘Five’}
The length is 5

Question: Write a Python program to split dictionary keys and values into separate lists.
Answer: dic = {‘A’ : ‘Apple’, ‘B’ : ‘Ball’, ‘C’ : ‘Cat’, ‘D’ :
‘Dog’, ‘E’ : ‘Elephant’}
print(“Original Dictionary:”,str(dic))
# split dictionary into keys and values
keys = dic.keys()
values = dic.values()
#printing keys and values seperately
print (“keys : ”, str(keys))
print (“values : ”, str(values))
Output
Original Dictionary : {‘A’ : ‘Apple’, ‘B’ : ‘Ball’, ‘C’ : ‘Cat’, ‘D’ :
‘Dog’, ‘E’ : ‘Elephant’}
keys : dict_keys ([‘A’, ‘B’, ‘C’, ‘D’, ‘E’])
values : dict_values ([‘Apple’, ‘Ball’, ‘Cat’, ‘Dog’, ‘Elephant’])

Question: Find the output.
(i) x = {(1, 2) : 1, (2, 3) : 2}
print (x[1, 2])
(ii) x = {‘a’ : 1, ‘b’ : 2, ‘c’ : 3}
print (x[‘a’, ‘b’])
(iii) a = {}
a[1] = 1
a[‘1’] = 2
a[1] + = 1
sum = 0
for i in a:
sum = sum + a[i]
print(sum)
Answer: (i) 1 (ii) KeyError (iii) 4

Question: Create a dictionary ‘ODD’ of odd numbers between 1 and 10, where the key is the decimal number and the value is the corresponding number
in words. Perform the following operations on this dictionary:
(i) Display the keys
(ii) Display the values
(iii) Display the items
(iv) Length of the dictionary
(v) Check if 7 is present or not
(vi) Check if 2 is present or not
(vii) Retrieve the value corresponding to the key 9
(viii) Delete the item from the dictionary
corresponding to the key 9
>>> ODD = {1:‘One’,3:‘Three’,5:‘Five’,7:
‘Seven’,9:‘Nine’}
>>> ODD
{1: ‘One’, 3: ‘Three’, 5: ‘Five’, 7: ‘Seven’, 9:
‘Nine’}
Answer: (i) >>> ODD.keys()
dict_keys([1, 3, 5, 7, 9])
(ii) >>> ODD.values()
dict_values([‘One’, ‘Three’, ‘Five’, ‘Seven’,
‘Nine’])
(iii) >>> ODD.items()
dict_items([(1, ‘One’), (3, ‘Three’), (5, ‘Five’),
(7, ‘Seven’), (9, ‘Nine’)])
(iv) >>> len(ODD)
5
(v) >>> 7 in ODD
True
(vi) >>> 2 in ODD
False
(vii) >>> ODD.get(9)
‘Nine’
(viii) >>> del ODD[9]
>>> ODD
{1: ‘One’, 3: ‘Three’, 5: ‘Five’, 7: ‘Seven’}

Question: Find the output.
(i) n = {‘a’ : [2, 3, 1], ‘b’ : [5, 2, 1], ‘c’ : [2, 3,
4]}
sorted_dic = {i : sorted(j) for i, j in n.items ()}
print(sorted_dic)
(ii) dict = {‘c’ : 789, ‘a’ : 796, ‘b’ : 908}
for i in sorted(dict):
print(dict[i])
(iii) students = {‘Sahil’ : {‘Class’ : 11,
‘roll_no’ : 21},
‘Puneet’ : {‘Class’ : 11,
‘roll_no’ : 30}}
for i in students :
print (i)
for j in students [i]:
print (j, ‘:’, students [i][j])
Answer: (i) {‘b’ : [1, 2, 5], ‘c’ : [2, 3, 4], ‘a’ : [1, 2, 3]}
(ii) 796
908
789
(iii) Sahil
Class : 11
roll_no : 21
Puneet
Class : 11
roll_no = 30

Question: Predict the output.
(i) dic = {}
dic[1] = 1
dic[‘1’] = 2
dic[1.0] = 4
sum = 0
for i in dic:
sum = sum + dic[i]
print(sum)
(ii) dic = {}
dic [(1, 2, 4)] = 8
dic [(4, 2, 1)] = 10
dic [(1, 2)] = 24
sum = 0
for i in dic :
sum = sum + dic [i]
print (sum)
print (dic)
Answer: (i) 6
(ii) 42
{(1, 2) : 24, (4, 2, 1) : 10, (1, 2, 4) : 8}

Question: Find the output of the given Python program.
key1 = [“Data 1”, “Data 2”]
name = [“Manish”, “Nitin”]
marks = [480, 465]
print (“The original key list : ” + str(key1))
print (“The original nested name list : ” + str(name))
print (“The original nested marks list : ” + str(marks))
output = {key : {‘Name’ : name, ‘Marks’ : marks} for key,
name, marks in zip(key1, name, marks)}
print(“The dictionary after creation :”, str(output))
Answer: Output
The original key list : [‘Data 1’, ‘Data 2’]
The original nested name list : [‘Manish’, ‘Nitin’]
The original nested marks list : [480, 465]
The dictionary after creation : {‘Data 2’ : {‘Name’ : ‘Nitin’,
‘Marks’ : 465}, ‘Data 1’ : {‘Name’ : ‘Manish’, ‘Marks’ : 480}}

Question: Write Python program to test if dictionary contains unique keys and values.
Answer: dict1 = {‘Manish’ : 1, ‘Akshat’ : 2, ‘Akansha’ : 3,
‘Nikuj’ : 1}
print(“The original dictionary : ” + str(dict1))
flag = False
val = dict()
for keys in dict1:
if dict1[keys] in val:
flag = True
break
else :
val[dict1[keys]] = 1
print(“Does dictionary contain repetition: ” +
str(flag))
Output
The original dictionary : {‘Nikunj’ : 1, ‘Akshat’ : 2, ‘Akansha’ :
3, ‘Manish’ : 1}
Does dictionary contain repetition : True

Question: Write a Python program to remove a dictionary
from list of dictionaries.
Answer: list1 = [{“id” : 101, “data” : “HappY”},
{“id” : 102, “data” : “BirthDaY”},
{“id” : 103, “data” : “Vyom”}]
print(“The original list is : ”)
for a in list1:
print(a)
for i in range(len(list1)):
if list1[i][‘id’] == 103:
del list1[i]
break
print (“List after deletion of dictionary :”)
for b in list1:
print(b)
Output
The original list is :
{‘id’ : 101, ‘data’ : ‘HappY’}
{‘id’ : 102, ‘data’ : ‘BirthDaY’}
{‘id’ : 103, ‘data’ : ‘Vyom’}
List after deletion of dictionary :
{‘id’ : 101, ‘data’ : ‘HappY’}
{‘id’ : 102, ‘data’ : ‘BirthDaY’}

Question: Write a program to enter names of employees and their salaries as input and store them in a dictionary.
Answer: num = int(input(“Enter the number of employees:
”))
count = 1
employee = dict()
while count <= num:
name = input(“Enter the name of the
Employee: ”)
salary = int(input(“Enter the salary: ”))
employee[name] = salary
count + = 1
print(“\n\nEMPLOYEE_NAME\tSALARY”)
for k in employee:
print(k,‘\t\t’,employee[k])

Question: Consider the following dictionary stateCapital = {“AndhraPradesh”:“Hyderabad”,
“Bihar”:“Patna”,“Maharashtra”:“Mumbai”,
“Rajasthan”:“Jaipur”}
Find the output of the following statements.
(i) print(stateCapital.get(“Bihar”))
(ii) print(stateCapital.keys())
(iii) print(stateCapital.values())
(iv) print(stateCapital.items())
(v) print(len(stateCapital))
(vi) print(“Maharashtra” in stateCapital)
(vii) print(stateCapital.get(“Assam”))
(viii) del stateCapital[“Rajasthan”]
print(stateCapital)
Answer: (i) Patna
(ii) dict_keys([‘AndhraPradesh’, ‘Bihar’, ‘Maharashtra’,
‘Rajasthan’])
(iii) dict_values([‘Hyderabad’, ‘Patna’, ‘Mumbai’, ‘Jaipur’])
(iv) dict_items([(‘AndhraPradesh’, ‘Hyderabad’), (‘Bihar’,
‘Patna’), (‘Maharashtra’, ‘Mumbai’), (‘Rajasthan’,
‘Jaipur’)])
(v) 4
(vi) True
(vii) None
(viii) {‘AndhraPradesh’: ‘Hyderabad’, ‘Bihar’: ‘Patna’,
‘Maharashtra’: ‘Mumbai’}

Question: Dictionaries are Python’s implementation of a data structure that is more generally known as an associative array. A dictionary consists of a collection of key-value pair. Each key-value pair maps the key to its associated value.
You can define a dictionary by enclosing a comma-separated list of key-value pair in curly braces {}. A colon (:) separates each key from its associated value:
(i) What is dictionary?
(ii) Is dictionary mutable or immutable?
(iii) Write the syntax to create dictionary.
(iv) Can we create empty dictionary?
(v)Which feature is used to access the elements from a dictionary?
Answer: (i) Dictionary is an unordered collection of data values that stored the key : value pair instead of single value as an element.
(ii) Dictionary is immutable which means they cannot be changed after creation.
(iii) dictionary_name = {key1 : value1, key2 : value2,…}
(iv) Yes, we can create empty dictionary.
For example, dic1 = { }
(v) Keys are used to access the elements from a dictionary.

Dictionaries Class 11 Computer Science Important Questions

Related Posts

error: Content is protected !!