You are on page 1of 5

IP MCQs

Q1. Dictionaries are set of elements.


(a) sorted
(b) ordered
(c) unordered
(d) random

Q2. Dictionaries are also called _____.


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

Q3. Dictionaries are _____ data types of Python.


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

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

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

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

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

Q8. What would the following code print?


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

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

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

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

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

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

Q14. 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])

Q15. 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
Q16. 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

Q17. Which of the following will add a key to the dictionary only if it does not already exist in the
dictionary?
(a) fromkeys()
(b) update()
(c) setdefault()
(d) all of these

Q18. Which of the following will create a dictionary with given keys and a common value?
(a) fromkeys()
(b) update()
(c) setdefault()
(d) all of these

Q19. Which value is assigned to keys, if no value is specified with the fromkeys() method ?
(a) 0
(b) 1
(c) None
(d) any of these

Q20. Which of the following will raise an error if the given dictionary is empty?
(a) del statement
(b) pop()
(c) popitem()
(d) all of these

Q21. A copy of the dictionary where only the copy of the keys is created for the new dictionary, is
called _____ copy.
(a) key copy
(b) shallow copy
(c) deep copy
(d) partial copy

Q22. A copy of the dictionary where the copy of the keys as well as the values is created for the
new dictionary, is called _____ copy.
(a) key copy
(b) shallow copy
(c) deep copy
(d) partial copy

Q23. Which of the following Python codes will give the same output if
dict = {"diary":1, "book":3, "novel":5}
(i) dict.pop("book")
(ii) del dict["book"]
(iii) dict.update({"diary":1,"novel":5})
(a) (i), (ii), (iii)
(b) (i), (ii)
(c) (i), (iii)
(d) (ii), (iii)

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


d1 = {"a":10, "b":2, "c":3}
str1=""
for i in d1:
str1=strl + str(d1[i]) + " "
str2 = str1[:-1]
print (str2[::-1])
(a) 3, 2
(b) 3, 2, 10
(c) 3, 2, 01
(d) Error

Q25. Running the code sorted(my_dictionary, reverse = True) on a dictionary named my_dictionary
will return results sorted in what order ?
(a) Ascending order (A-Z), by key
(b) Ascending order (A-Z), by value
(c) Descending order (Z-A), by key
(d) Descending order (Z-A), by value

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

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

Q28. 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()

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

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

You might also like