Dictionary in Python MCQ for class 11-12

Dictionary in Python MCQ for class 11-12

 

1. In order to store values in terms of key and value we use what core data type

a) list

b) tuple

c) class

d) dictionary

 

d) dictionary

 

 

2. What is the output of the following code?

a={1:”A”,2:”B”,3:”C”}

print(a.setdefault(3))

a) {1: ‘A’, 2: ‘B’, 3: ‘C’} c) d)

b) C

c) {1: 3, 2: 3, 3: 3}

d) No method called setdefault() exists for dictionary

b) C

 

 

3. Which of the following statements create a dictionary?

a) d = {}

b) d = {“john”:40, “peter”:45}

c) d = {40:”john”, 45:”peter”}

d) All of the mentioned

d) All of the mentioned

 

 

4. Read the code shown below carefully and pick out the keys?

d = {“john”:40, “peter”:45}

a) “john”, 40, 45, and “peter”

b) “john” and “peter”

c) 40 and 45

d) d = (40:”john”, 45:”peter”)

b) “john” and “peter”

 

 

5. What will be the output?

d = {“john”:40, “peter”:45}

“john” in d

a) True

b) False

c) None

d) Error

a) True

 

 

6. What will be the output?

d1 = {“john”:40, “peter”:45}

d2 = {“john”:466, “peter”:45}

d1 == d2

a) True

b) False

c) None

d) Error

b) False

 

 

7. What will be the output?

d1 = {“john”:40, “peter”:45}

d2 = {“john”:466, “peter”:45}

d1 > d2

a) True

b) False

c) Error

d) None

c) Error

 

 

8. What is the output? d = {“john”:40, “peter”:45}

d[“john”]

a) 40

b) 45

c) “john”

d) “peter”

a) 40

 

 

9. Suppose d = {“john”:40, “peter”:45}, to delete the entry for “john” what command do we use:

a) d.delete(“john”:40)

b) d.delete(“john”)

c) del d[“john”]

d) del d(“john”:40)

c) del d[“john”]

 

 

10. Suppose d = {“john”:40, “peter”:45}. To obtain the number of entries in dictionary which command do we use?

a) d.size()

b) len

c) size

d) d.len()

b) len

 

 

11. What will be the output?

d = {“john”:40, “peter”:45}

print(list(d.keys()))

a) [“john”, “peter”]

b) [“john”:40, “peter”:45]

c) (“john”, “peter”)

d) (“john”:40, “peter”:45)

 

a) [“john”, “peter”]

 

 

12. Which of the following is correct with respect to above Python code? d = {“a”:3 ,”b”: 7}

print(list(d.keys()))

a) a dictionary d is created.

b) a and b are the keys of dictionary d.

c) 3 and 7 are the values of dictionary d

d) All of the above.

 

d) All of the above.

 

 

13. Which one of the following is correct?

a) In python, a dictionary can have two same keys with different values.

b) In python, a dictionary can have two same values with different keys

c) In python, a dictionary can have two same keys or same values but cannot have two same key-value pair

d) In python, a dictionary can neither have two same keys nor two same values.

a) In python, a dictionary can have two same values with different keys.

 

 

14. What will be the output of above Python code?

d1={“abc”:5,”def”:6,”ghi”:7}

print(d1[0])

a) abc

b) 5

c) {“abc”:5}

d) Error

d) Error

 

 

15. What will the below Python code do?

dict={“Phy”:94,”Che”:70,”Bio”:82,”Eng”:95}

dict.update({“Che”:72,”Bio”:80})

a) It will create new dictionary as dict={“Che”:72,”Bio”:80} and old dict will be deleted.

b) It will throw an error as dictionary cannot be updated.

c) It will simply update the dictionary as dict={“Phy”:94,”Che”:72,”Bio”:80,”Eng”:95}

d) It will not throw any error but it will not do any changes in dict

 

c) It will simply update the dictionary as dict={“Phy”:94,”Che”:72,”Bio”:80,”Eng”:95}

 

 

16. What will be the output of above Python code?

d1={“abc”:5,”def”:6,”ghi”:7}

print(d1[0])

a) abc

b) 5

c) {“abc”:5}

d) Error

d) Error

 

 

17. Which of the following will delete key_value pair for key=”tiger” in dictionary?

dic={“lion”:”wild”, “tiger”:”wild”, “cat”:”domestic”, “dog”:”domestic”}

a) del dic[“tiger”]

b) dic[“tiger”].delete()

c) delete(dic.[“tiger”])

d) del(dic.[“tiger”])

a) del dic[“tiger”]

 

 

18. Which of the following will give error?

Suppose dict1={“a”:1,”b”:2,”c”:3}

a) print(len(dict1))

b) print(dict1.get(“b”))

c) dict1[“a”]=5

d) None of these.

d) None of these.

 

 

19. Which of the following Python codes will give same output if

(i) dict.pop(“book”)

(ii) del dict[“book”]

(iii) dict.update({“diary”:1,”novel”:5})

dict={“diary”:1,”book”:3,”novel”:5}

a) i, ii, iii

b) i, ii

c) i, iii

d) ii, iii

b) i, ii

 

 

20. Keys of the dictionary must be :

a) Similar

b) Unique

c) Can be similar or unique

d) All of these

b) Unique

 

 

21. Which of these about a dictionary is false?

a) The values of a dictionary can be accessed using keys

b) The keys of a dictionary can be accessed using values

c) Dictionaries aren’t ordered

d) Dictionaries are mutable

b) The keys of a dictionary can be accessed using values

 

 

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) { }

c) {1,”A”,2”B”}

 

 

23. What will be the output of the following Python code snippet?

a={1:”A”,2:”B”,3:”C”}

print(a.get(1,4))

a) 1

b) A

c) 4

d) Invalid syntax for get method

 

a) A

 

 

24. What will be the output of the following Python code snippet?

a={1:”A”,2:”B”,3:”C”}

print(a.get(5,4))

a) 1

b) A

c) 4

d) Invalid syntax for get method

 

c) 4

 

 

25. What will be the output of the following Python code snippet?

a={1:”A”,2:”B”,3:”C”}

a.setdefault(4,”D”)

print(a)

a) {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’}

b) None

c) Error

d) [1,3,6,10]

 

a) {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’}

 

 

26. 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

b) {1: ‘A’, 2: ‘B’, 3: ‘C’}

 

 

27. What will be the output of the following Python code?

a={1:”A”,2:”B”,3:”C”}

a.clear()

print(a)

a) None

b) { None:None, None:None, None:None}

c) {1:None, 2:None, 3:None}

d) { }

 

d) { }

 

 

28. 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}

b) {1: 5, 2: 3}

 

 

29. What will be the output of the following Python code?

a={1:”A”,2:”B”,3:”C”}

for i in a:

print(i,end=” “)

a) 1 2 3

b) ‘A’ ‘B’ ‘C’

c) 1 ‘A’ 2 ‘B’ 3 ‘C’

d) Error, it should be: for i in a.items():

a) 1 2 3

 

 

30. What will be the output of the following Python code?

>>> a={1:”A”,2:”B”,3:”C”}

>>> a.items()

a) Syntax error

b) dict_items([(‘A’), (‘B’), (‘C’)])

c) dict_items([(1,2,3)])

d) dict_items([(1, ‘A’), (2, ‘B’), (3, ‘C’)])

 

d) dict_items([(1, ‘A’), (2, ‘B’), (3, ‘C’)])

 

 

31. What is the output of the following code?

a={1:”A”,2:”B”,3:”C”}

for i,j in a.items():

print(i,j,end=” “)

a) 1 A 2 B 3 C

b) 1 2 3

c) A B C

d) 1:”A” 2:”B” 3:”C”

a) 1 A 2 B 3 C

 

 

32. What is the output of the following code?

a={1:”A”,2:”B”,3:”C”}

for i,j in a.items():

print(i,j,end=” “)

a) 1 A 2 B 3 C

b) 1 2 3

c) A B C

d) 1:”A” 2:”B” 3:”C”

a) 1 A 2 B 3 C

 

Leave a Reply

Your email address will not be published. Required fields are marked *