0% found this document useful (0 votes)
162 views4 pages

MCQ Dictionary

Uploaded by

gamertoed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
162 views4 pages

MCQ Dictionary

Uploaded by

gamertoed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

MCQ : DICTIONARY

1. What will be the output of the following code?


D1={1: “One”,2: “Two”, 3: “C”}
D2={4: “Four”,5: “Five”}
D1.update(D2)
print (D1)
a) {4:’Four’,5: ‘Five’} b) Method update() doesn’t exist for dictionary
c) {1: “One”,2: “Two”, 3: “C”} d) {1: “One”,2: “Two”, 3: “C”,4: ‘Four’,5: ‘Five’}
2. Given the following dictionaries
D1={"Exam":"ICSCE", "Year":2023, "Total":500}
Which statement will add a new value pair (Grade: “A++”) in dictionary D1?
a. D1.update(“Remark” : “Excellent”) b. D1 + {“Remark” : “Excellent”}
c. D1[“Remarks”] = “Excellent” d. D1.merge({“Remark” : “Excellent”})
3. Given the following dictionaries
exam={"Exam":"AISSCE","Year":2023}
result={"Total":500,"Pass_Marks":165}
which statement among the following options will update exam with values of result ?
(a) exam.update(result) (b) exam+result (c) exam.add(result) (d) merge(result)
4. Which is the correct form of declaration of dictionary?
a) Day={1:’Monday’,2:’Tuesday’,3:’Wednesday’}
b) Day={‘1:Monday’,’2:Tuesday’,’3:Wednesday’}
c) Day=(1:’Monday’,2:’Tuesday’,3:’Wednesday’)
d) Day=[1:’Monday’,2:’Tuesday’,3:’Wednesday’]
5. What will be the output of the following Python code snippet?
D1={“John”:40 , “Peter” : 45}
D2= { “John”: 466, “Peter” : 45}
print(D1>D2)
a) Ture b) False c) Error d) None
6. Write a python command to create list of keys from a dictionary.
dict={‘a’:’A’, ‘b’:’B’, ‘c’:’C’, ‘d’:’D’}
a) dict.keys() b)keys() c) key.dict() d) None of these
7. Given the following dictionaries
Dict1={‘Tuple’:’immutable’,’List’:’mutable’,’Dictionary’:’mutable’,’String’:’immutable’}
Which of the following will delete key: value pair for key=‘Dictionary’ in the above mentioned dictionary?
a. del Dict1[‘Dictinary’] b. Dict1[‘Dictionary’].delete( )
c. delete (Dict1.[“Dictionary”]) d. del(Dict1.[‘Dictionary’])
8. If my_dict is a dictionary as defined below, then which of the following statements will raise an exception?
my_dict = {'apple': 10, 'banana': 20, 'orange': 30}
(a) my_dict.get('orange') (b) print(my_dict['apple', 'banana'])
(c) my_dict['apple']=20 (d) print(str(my_dict))
9. Which of the following will delete key-value pair for key = “Red” from a dictionary D1?
a. delete D1("Red") b. del D1["Red"] c. del.D1["Red"] d. D1.del["Red"]

AMIT KUMAR KUSHWAHA(9891145409) Page 1


10. Which of the following statement(s) would give an error after executing the following code?
D={'rno':32,'name':'Ms Archana','subject':['hindi','english','cs'],'marks':(85,75,89)} #S1
print(D) #S2
D['subject'][2]='IP' #S3
D['marks'][2]=80 #S4
print(D) #S5
(a) S1 (b) S3 (c) S4 (d) S3 and S4
11. What will be the output of the following python dictionary operation?
data = {'A':2000, 'B':2500, 'C':3000, 'A':4000}
print(data)
a) {'A':2000, 'B':2500, 'C':3000, 'A':4000} b) {'A':2000, 'B':2500, 'C':3000}
c) {'A':4000, 'B':2500, 'C':3000} d) It will generate an error.
12. What will the following code do?
dict={"Exam":"AISSCE", "Year":2022}
dict.update({"Year”:2023} )
a. It will create new dictionary dict={” Year”:2023}and old dictionary will be deleted
b. It will throw an error and dictionary cannot updated
c. It will make the content of dictionary as dict={"Exam":"AISSCE", "Year":2023}
d. It will throw an error and dictionary and do nothing
13. State True or False:
"A dictionary key must be of a data type that is mutable."
14. Which of the following Statement(s) would give an error after executing the following code?
d= ("A":1, "B": 2, "C:3, "D":4) #statement 1
sum_keys =0 #statement 2
for val in d.keys: #statement 3
sum_keys = sum_keys+ val #statement 4
print(sum_keys)
a) statement 1 b) statement 2 c) statement 3 d) statement 4
15. Predict the output.
marks = {"Ashok":98.6, "Ramesh":95.5}
print(list(marks.keys()))
(a) ‘Ashok’, ‘Ramesh’ (b) 98.6, 95.5 (c) [‘Ashok’,’Ramesh’] (d) (‘Ashok’,’Ramesh’)
16. What will be output of the following code:
d1={1:2,3:4,5:6}
d2=d1.get(3,5)
print(d2)
a) 3 b) 4 c) 5 d) Error
17. Given the following dictionary
Emp1={“salary”:10000,‟dept”:‟sales”,‟age”:24,”name”:‟john”}
Emp1.keys() can give the output as
a. (“salary”,‟dept”,‟age”,‟name”) b. [“name”, “salary”,”dept”,‟age”]
c. [10000,‟sales”,24,‟john”] d. {“salary”,‟dept”,‟age”,‟name”}

18. Choose the most correct statement among the following –


AMIT KUMAR KUSHWAHA(9891145409) Page 2
(a) a dictionary is a sequential set of elements
(b) a dictionary is a set of key-value pairs
(c) a dictionary is a sequential collection of elements key-value pairs
(d) a dictionary is a non-sequential collection of elements
19. What will be output of the following code:
d1={1:2,3:4,5:6}
d2=d1.popitem()
print(d2)
a) {1:2} b) {5:6} c) (1,2) d) (5,6)

20. What is printed by the following statements?


D1 = {"cat":17, "dog":6, "elephant":23, "bear":20}
print ("dog" in D1)
a. True b. False c. Error d. None
21. Which of the following will raise an error if the given key is not found in the dictionary ?
a) del statement b) B. pop() c) get() d) all of these
22. Which of the following is not a declaration of the dictionary?
a) {1: ‘A’, 2: ‘B’} b) dict([[1,”A”],[2,”B”]]) c) {1,”A”,2”B”} D. { }
23. Write the output of-
d={0:'zero',1:'one',2:'two',3:'three'}
for i in d:
print(i, end=' ')
a) {0:'zero',1:'one',2:'two',3:'three'} b). 'zero','one','two','three' c) Error d) 0 1 2 3
24. Suppose d = {“john”:40, “peter”:45}. To obtain the number of entries in dictionary which command do we
use?
a)d.size() b)len(d) c)size(d) d)d.len()
25. Suppose d = {“john”:40, “peter”:45}, what happens when we try to retrieve a value using the expression
d[“susan”]?
a) Since “susan” is not a value in the set, Python raises a KeyError exception
b) It is executed fine and no exception is raised, and it returns None
c) Since “susan” is not a key in the set, Python raises a KeyError exception
d) Since “susan” is not a key in the set, Python raises a syntax error
26. What will be the output of the following Python code snippet?
a={1:"A",2:"B",3:"C"}
for i,j in a.items():
print(i,j)
a) 1 A 2 B 3 C b) 1 2 3 c) A B C d) 1:”A” 2:”B” 3:”C”
27. What will be the output of the following Python code snippet?
a={1:"A",2:"B",3:"C"}
print(a.setdefault(3))
a) {1: ‘A’, 2: ‘B’, 3: ‘C’} b) C
c) {1: 3, 2: 3, 3: 3} d) No method called setdefault() exists for dictionary
28. What will be the output of the following Python code?
a={1:"A",2:"B",3:"C"}
AMIT KUMAR KUSHWAHA(9891145409) Page 3
b={3:"D",4:"E"}
a.update(b)
print(a)
a) {1: ‘A’, 2: ‘B’, 3: ‘D’, 4:"E"} b) Method update() doesn’t exist for dictionaries
c) {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’, 5: ‘E’} d) {4: ‘D’, 5: ‘E’}
29. What will be the output of the following Python code?
a={1:"A",2:"B",3:"C"}
b=a.copy()
b[2]="D"
print(a)
a) Error, copy() method doesn’t exist for dictionaries b) {1: ‘A’, 2: ‘B’, 3: ‘C’}
c) {1: ‘A’, 2: ‘D’, 3: ‘C’} d) “None” is printed
30. What will be the output of the following Python code?
a={1:5,2:3,3:4}
a.pop(3)
print(a)
a) {1: 5} b) {1: 5, 2: 3}
c) Error, syntax error for pop() method d) {1: 5, 3: 4}

AMIT KUMAR KUSHWAHA(9891145409) Page 4

You might also like