Dictionaries || Type B || Sumita Arora || Class 11 || Computer science || Information Practices


Q1. Which of the following will result in an error for a given valid dictionary D?

(i)
D + 3

(ii)
D * 3

(iii)
D + {3 : "3"}

(iv)
D.update ({3 : "3"})

(v)
D.update({"3" : 3})

(vi)
D.update("3": 3)





Q2. The following code is giving some error. Find out the error and correct it.

d1 = {"a" : 1, 1 : "a", [1, "a"] : "two"}






Q3. The following code has two dictionaries with tuples as keys. While one of these dictionaries being successfully created, the other is giving some error. Find out which dictionary will be successfully and which one will give error and correct it:

dict1 = {(1, 2) : [1, 2], (3, 4) : [3, 4]}
dict2 = {([1], [2]) : [1, 2] : ([3], [4]) : [3, 4]}





Q4. Nesting of dictionary allows you to store a dictionary inside another dictionary. Then why is following code raising error? What can you do to correct it?

d1 = {1 : 10, 2 : 20, 3: 30}
d2 = {1 : 40, 2 : 50, 3 : 60}
d3 = {1 : 70, 2 : 80, 3 : 90}
d4 = {d1 : "a", d2 : "b", d3 : "c"}






Q5. Why is following code not giving correct output even when 25 is a member of the dictionary?

dic1 = {'age' : 25, 'name' : "xyz", 'salary' : 23450.5}
val = dic1['age']
if val in dic1 :
    print("This is member of the dictionary")
else :
    print("This is not a member of the dictionary")







Q6. What is the output produced by above code:

d1 = {5 : [6, 7, 8],"a" : (1, 2, 3)}
print (d1.keys())
print(d1.values())







Q7. Consider the following code and then answer the questions that follow:

myDict = {'a' : 27, 'b' : 43, 'c' : 25, 'd' : 30}
valA ' '
for i in myDict :
    if i > valA :
        valA = 1
        valB = myDict[i]
print (valA)# Line 1
print (valB)# Line 2
print (30 in myDict)# Line 3
myLst = (myDcit.items())
mylst. sort()# Line 4
print (myLst[-1])# Line 5

(i) What output does Line 1 produce?
(ii) What output does Line 2 produce?
(iii) What output does Line 3 produce?
(iv) What output does Line 5 produce?
(v) What is the return value from the list sort() function (Line 5) and what is its type?






Q8. What will be the output produced by following code?

d1 = {5 : "number", "a" : "string", (1, 2) : 'tuple'}
print ("Dictionary contents")
for x in d1.keys() : # Iterate on a key list
    print (x, ':', d1[x], end = ' ')
    print (d1[x] * 3)
    print ()







Q9. Predict the output:

(i)
d = dict ()
d ['left'] = '<'
d ['right'] = '>'
print ('{left} and {right} or {right} and {left}')

(ii)
d = dict ()
d ['left'] = '<'
d ['right' ] = '>'
d ['end'] = ' '
print(a ["left"] and d ['right'] or d ['right'] and d ['left'])
print (d ['left'] and d ['right'] or d ['right'] and d ['left'] )
print( (d['left'] and d [ 'right'] or d ['right'] and d ['left'] ) and d ['end'])
print ("end")

(iii)
text = "abracadabraaabbccrr"
counts = {}
ct = 0
lst = []
for word in text :
    if word not in lst :
        lst . append(word)
        counts[word] = 0
    ct = ct + 1
    counts [word] = counts [word] + 1
    print (counts)
    print (lst)

(iv)
list1 = [2, 3, 3, 5, 3, 2, 5, 1, 1]
counts = {}
ct = 0
lst = []
for num in list1 :
    if num not in lst :
        lst . append(num)
        counts[num] = 0
    ct = ct + 1
    counts[num] = counts[num] + 1
    print (counts)
    for key in counts.keys() :
        counts[key] = key * counts [key]
    print (counts)



Q10. 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:

(a) Display the keys
(b) Display the values
(c) Display the items
(d) Find the length of the dictionary
(e) Check if 7 is present or not
(f) Check if 2 is present or not
(g) Retrieve the value corresponding to the key 9
(h) Delete the item from the dictionary corresponding to the key 9




Q11. Find the errors:

(a)
text = "abracadbra"
counts = {}
for word in text :
    counts[word] = counts [word] + 1

(b)
(b)
my_dict = {}
my_dict[1, 2, 4] = 8
my_dict[[4, 2, 1]] = 10
print (my_dict)






Q12. Predict the output:

(a)
fruit = {}
L1 = ['Apple', 'banana', 'apple']
for index in L1 :
    if index in fruit :
        fruit [index] += 1
    else :
        fruit [index] = 1
print (len(fruit))
print (fruit)

(b)
arr = {}
arr [1] = 1
arr ['1'] = 2
arr[1] += 1
sum = 0
for k in arr :
    sum += arr[1]
print (sum)






Q13. Predict the output:

(a)
a = {(1, 2) : 1, (2, 3) : 2}
print(a[1, 2])

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







Q14. Find the error/output. Consider below given two sets of codes. Which one will produce an error? Also, predict the output produced by the correct code.

(a)
box = {}
jars = {'Jam' : 4}
crates = {}
box['biscuit'] = 1
box['cake'] = 3
crates['box'] = box
crates['jars'] = jars
print (len(crates[box]))

(b)
box = {}
jars = {'Jam' : 4}
crates = {}
box['biscuit'] = 1
box['cake'] = 3
crates['box'] = box
crates['jars'] = jars
print (len(crates['box']))



Q15. Predict the output:

dct = {}
dct[1] = 1
dct['1'] = 2
dct[1.0] = 4
sum = 0
for k in dct:
    print(k, sum)
    sum += dct[k]
print (sum)





Q16. Fill in the blanks of the following code so that the values and keys of dictionary d are inverted to create dictionary fd.

d = {'a':1, 'b':2, 'c':3}
print(d)
fd = { }
for key, value in d. _____() :
    fd [____] = [____]
print(fd)

13 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post