-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictionaryQuiz.py
164 lines (116 loc) · 2.47 KB
/
dictionaryQuiz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# Created by Elshad Karimov on 26/04/2020.
# Copyright © 2020 AppMillers. All rights reserved
# Dictionary Interview Questions
# Q-1. What will be the output of the following code snippet?
a = {(1,2):1,(2,3):2}
print(a[1,2])
# A. Key Error
# B. 1
# C. {(2,3):2}
# D. {(1,2):1}
# Q-2. What will be the output of the following code snippet?
a = {'a':1,'b':2,'c':3}
# print (a['a','b'])
# A. Key Error
# B. [1,2]
# C. {‘a’:1,’b’:2}
# D. (1,2)
# Q-3. What will be the output of the following code block?
fruit = {}
def addone(index):
if index in fruit:
fruit[index] += 1
else:
fruit[index] = 1
addone('Apple')
addone('Banana')
addone('apple')
print (len(fruit))
# A. 1
# B. 2
# C. 3
# D. 4
# Q-4. What will be the output of the following code block?
arr = {}
arr[1] = 1
arr['1'] = 2
arr[1] += 1
sum = 0
for k in arr:
sum += arr[k]
print(sum)
# A. 1
# B. 2
# C. 3
# D. 4
# Q-5. What will be the output of the following code snippet?
my_dict = {}
my_dict[1] = 1
my_dict['1'] = 2
my_dict[1.0] = 4
sum = 0
for k in my_dict:
sum += my_dict[k]
print (sum)
# A. 7
# B. Syntax error
# C. 3
# D. 6
# Q-6. What will be the output of the following code snippet?
my_dict = {}
my_dict[(1,2,4)] = 8
my_dict[(4,2,1)] = 10
my_dict[(1,2)] = 12
sum = 0
for k in my_dict:
sum += my_dict[k]
print (sum)
print(my_dict)
# A. Syntax error
# B. 30
# {(1, 2): 12, (4, 2, 1): 10, (1, 2, 4): 8}
# C. 47
# {(1, 2): 12, (4, 2, 1): 10, (1, 2, 4): 8}
# D. 30
# {[1, 2]: 12, [4, 2, 1]: 10, [1, 2, 4]: 8}
# Q-7. What will be the output of the following code snippet?
box = {}
jars = {}
crates = {}
box['biscuit'] = 1
box['cake'] = 3
jars['jam'] = 4
crates['box'] = box
crates['jars'] = jars
# print(len(crates[box]))
# A. 1
# B. 3
# C. 4
# D. Type Error
# Q-8. What will be the output of the following code block?
dict = {'c': 97, 'a': 96, 'b': 98}
for _ in sorted(dict):
print (dict[_])
# A. 96 98 97
# B. 96 97 98
# C. 98 97 96
# D. NameError
# Q-9. What will be the output of the following code snippet?
rec = {"Name" : "Python", "Age":"20"}
r = rec.copy()
print(id(r) == id(rec))
# A. True
# B. False
# C. 0
# D. 1
# Q-10. What will be the output of the following code snippet?
rec = {"Name" : "Python", "Age":"20", "Addr" : "NJ", "Country" : "USA"}
id1 = id(rec)
del rec
rec = {"Name" : "Python", "Age":"20", "Addr" : "NJ", "Country" : "USA"}
id2 = id(rec)
print(id1 == id2)
# A. True
# B. False
# C. 1
# D. Exception