0% found this document useful (0 votes)
447 views

Chapter-4 (2) MCQ

This document contains multiple choice, fill in the blank, and true/false questions about dictionaries in Python. Some key points covered include: - Dictionaries are unordered sets of key-value pairs where keys must be immutable types. - Common dictionary methods include keys(), values(), items(), update(), get(), pop(), and del to delete items. - The del statement or pop() function can be used to delete an element using its key, and will raise an error if the key is not found. - A dictionary's keys and values can be strings, integers, or a combination of both, but it cannot contain duplicate keys or the same key-value pair twice.

Uploaded by

eyaansiddiqui703
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)
447 views

Chapter-4 (2) MCQ

This document contains multiple choice, fill in the blank, and true/false questions about dictionaries in Python. Some key points covered include: - Dictionaries are unordered sets of key-value pairs where keys must be immutable types. - Common dictionary methods include keys(), values(), items(), update(), get(), pop(), and del to delete items. - The del statement or pop() function can be used to delete an element using its key, and will raise an error if the key is not found. - A dictionary's keys and values can be strings, integers, or a combination of both, but it cannot contain duplicate keys or the same key-value pair twice.

Uploaded by

eyaansiddiqui703
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/ 6

CHAPTER-4 DICTIONARIES

Multiple Choice Question


1. Dictionaries are set of elements.
(a) sorted
(b) ordered
(c) unordered
(d) random

2. Dictionaries are also called _____.


(a) mappings
(b) hashes
(c) associative arrays
(d) all of these

3. Dictionaries are _____ data types of Python.


(a) mutable
(b) immutable
(c) simple
(d) all of these

4. Which of the following functions will return the key, value pairs of a
dictionary?
(a) keys()
(b) values()
(c) items()
(d) all of these

5. Which of the following can be used to delete item(s) from a dictionary?


(a) del statement
(b) get()
(c) getitem()
(d) all of these

6. Which of the following will raise an error if the given key is not found in the
dictionary ?
(a) del statement
(b) pop()
(c) getitem()
(d) all of these

7. Which of the following is correct with respect to above Python code?


d = {"a":3,"b":7}
(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 these.

8. What would the following code print?


d = {'spring': 'autum', "autumn": "fall", "fall":"spring"}
print (d["autumn"])
(a) autumn
(b) fall
(c) spring
(d) Error

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

10. What is printed by the following statements ?


D1 = {"cat":17, "dog":6, "elephant":23, "bear":20}
print (25 in D1)
(a) True
(b) False
(c) Error
(d) None

11. What will be the result of the following code?


d1 = {"abc":5,"def":6, "ghi":7}
print (d1[0])
(a) abc
(b) 5
(c) (“abc” : 5)
(d) Error

12. What will the following 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.
13. What will be the result of the following code ?
dict = {"Jo" : 1, "Ra" : 2}
dict.update({"Phoebe":2})
print (dict)
(a) {"Jo":1,"Ra" :2, "Ph" : 2}
(b) {"Jo":1,"Ra":2}
(c) {"Jo":1,"Ph" :2}
(d) Error

14. Which of the following will delete key_value pair for key = "tiger" in
dictionary?
di = {"loin" : "wild", "tiger" : "wild", "cat": "domestic" : "dog" : "domestic"}
(a) del di["tiger"]
(b) di[“tiger”].delete()
(c) delete(di.["tiger"])
(d) del(di.["tiger])

15. Which of the following will give error if d1 is as shown below?


d1 = ("a" : 1, "b":2,"c":3)
(a) print(len(d1))
(b) print(d1.get("b"))
(c) d1["a"] = 5
(d) None of these

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


d1 = {"a" : 10, "b" : 2, "c" : 3}
str1 = ""
for i in d1:
str1 = str1 + str(d1[i])+ ""
str2 = str1[:-1]
print(str2[::-1])
(a) 3, 2
(b) 3, 2, 10
(c) 3, 2, 01
(d) Error
Fill In the Blanks
1. The keys of a dictionary must be of _____ types.
2. The order of a dictionary's elements is _____.
3. To delete an element using a key, _____ _____ is used.
4. To get all the keys of a dictionary, _____ method is used.
5. The ______ statement will raise an error if the given key is not found in the
dictionary.
Ans
1. immutable
2. undefined or unordered
3. del statement or pop() function
4. keys()
5. del
True / False
1. In Python, a dictionary can have two same keys with different values.
2. In Python, a dictionary can have two same values with different keys.
3. Dictionaries are unordered set of elements.
4. A dictionary can have duplicate keys.
5. In Python, a dictionary can have two same keys or same values but cannot
have two same key-value pair.
6. In Python a dictionary can neither have two same keys nor two same values.
7. Values of a dictionary can be string, integers or combination of both.
8. Key of a dictionary can be string, integer or combination of both.
9. The value of a dictionary can be accessed with help of indices.
10. A dictionary is immutable.
11. The del statement raises error if the given \ is not found in the dictionary.

Ans
1. False
2. True
3. True
4. False
5. False
6. False
7. True
8. True
9. False
10. False
11. True

You might also like