Dictionary Handling in Python

MCQ

1. A dictionary in Python is enclosed within:

a) []
b) ()
c) {}
d) ""
Answer: c) {}
Explanation: Dictionaries use curly braces {}.

2. A dictionary stores data in the form of:

a) Values only
b) Keys only
c) Key–Value pairs
d) Sequential data
Answer: c) Key–Value pairs

3. Which of the following is a valid dictionary?

a) {1: "A", 2: "B"}
b) [1, "A", 2, "B"]
c) (1: "A", 2: "B")
d) {"A", "B"}
Answer: a) {1: "A", 2: "B"}

4. Dictionaries in Python are:

a) Ordered and mutable
b) Ordered and immutable
c) Unordered and immutable
d) Static
Answer: a) Ordered and mutable
Explanation: From Python 3.7+, dictionaries maintain insertion order.

5. What is the output of type({})?

a) <class 'set'>
b) <class 'dict'>
c) <class 'list'>
d) <class 'tuple'>
Answer: b) <class 'dict'>
Explanation: {} creates an empty dictionary, not a set.

6. How can you create an empty dictionary?

a) empty = []
b) empty = {}
c) empty = dict()
d) Both b and c
Answer: d) Both b and c

7. Which of these is NOT allowed in dictionary keys?

a) String
b) Integer
c) Tuple
d) List
Answer: d) List
Explanation: Dictionary keys must be immutable.

8. Dictionary values can be:

a) Only integers
b) Only strings
c) Any data type
d) Only lists
Answer: c) Any data type

9. What is the output of:

a) a
b) 1
c) “a”:1
d) Error
Answer: b) 1

10. What will happen if you try to access a non-existing key?

a) Returns None
b) Returns 0
c) Raises KeyError
d) Returns empty string
Answer: c) Raises KeyError

11. Which method is used to safely access a key value?

a) fetch()
b) get()
c) find()
d) locate()
Answer: b) get()
Explanation: get() returns None if the key is not found.

12. What is the output of:

a) 10
b) 20
c) None
d) Error
Answer: c) None

13. What does the keys() method return?

a) All values
b) All keys
c) All key–value pairs
d) None
Answer: b) All keys

14. What does the values() method return?

a) Keys only
b) Values only
c) Keys and values
d) None
Answer: b) Values only

15. What does the items() method return?

a) Keys only
b) Values only
c) Key–Value pairs
d) None
Answer: c) Key–Value pairs

16. How do you remove a specific key from a dictionary?

a) del dict[key]
b) dict.remove(key)
c) dict.popitem(key)
d) dict.clear()
Answer: a) del dict[key]

17. Which method removes a key and returns its value?

a) remove()
b) pop()
c) delete()
d) clear()
Answer: b) pop()

18. What is the output of:

a) 1
b) 2
c) 3
d) Error
Answer: b) 2

19. What does clear() do in a dictionary?

a) Deletes the dictionary
b) Removes all items but keeps it empty
c) Removes one item
d) None
Answer: b) Removes all items but keeps it empty

20. Which method returns all items as a list of tuples?

a) pairs()
b) items()
c) all()
d) map()
Answer: b) items()

21. Which statement creates a copy of a dictionary?

a) new = dict.copy()
b) new = dict.clone()
c) new = dict.copy()
d) new = dict()
Answer: c) new = dict.copy()

22. What is the output of:

a) True
b) False
c) Error
d) None
Answer: a) True
Explanation: Membership test works on keys.

23. What is the output of:

a) True
b) False
c) None
d) Error
Answer: b) False

24. The update() method is used to:

a) Add key-value pairs
b) Remove all keys
c) Sort dictionary
d) Reverse dictionary
Answer: a) Add key-value pairs

25. What will d.popitem() do?

a) Remove the first item
b) Remove the last inserted item
c) Remove a random item
d) Delete all items
Answer: b) Remove the last inserted item

26. Can dictionary keys be repeated?

a) Yes
b) No
c) Sometimes
d) Only for strings
Answer: b) No
Explanation: Keys must be unique.

27. If a key is repeated, which value is stored?

a) First one
b) Last one
c) Both
d) None
Answer: b) Last one

28. How do you get the number of items in a dictionary?

a) length()
b) len()
c) count()
d) total()
Answer: b) len()

a) {"a":1, "b":2}
b) {"a":1, "b":2, "c":3}
c) {"c":3}
d) Error
Answer: b) {"a":1, "b":2, "c":3}

30. Which of these methods deletes all elements?

a) clear()
b) delete()
c) remove()
d) discard()
Answer: a) clear()

31. Which function converts a list of tuples to a dictionary?

a) list()
b) dict()
c) tuple()
d) map()
Answer: b) dict()

32. What will dict([(1,'a'),(2,'b')]) return?

a) {1:'a', 2:'b'}
b) [(1,'a'),(2,'b')]
c) (1:'a',2:'b')
d) Error
Answer: a) {1:'a', 2:'b'}

33. What happens when we execute:

a) Adds key ‘x’ with value 5
b) Adds value only
c) Error
d) Deletes dictionary
Answer: a) Adds key ‘x’ with value 5

34. Which function returns all keys as a list?

a) keys()
b) list(keys())
c) list(d.keys())
d) Both b and c
Answer: d) Both b and c

35. Which function removes and returns a key’s value?

a) remove()
b) pop()
c) del()
d) discard()
Answer: b) pop()

36. Which of the following is true about dictionaries?

a) Keys can be mutable
b) Values can be mutable
c) Both must be immutable
d) Both must be mutable
Answer: b) Values can be mutable

37. What will be output of:

a) 10
b) 20
c) 30
d) Error
Answer: c) 30

38. What will happen if we use len(d) on an empty dictionary?

a) 0
b) Error
c) 1
d) None
Answer: a) 0

39. Can dictionary values be another dictionary?

a) Yes
b) No
c) Only for integers
d) Only for strings
Answer: a) Yes
Explanation: Nested dictionaries are allowed.

40. What will this output?

a) a
b) 2
c) {"b":2}
d) Error
Answer: b) 2

41. How do you get all keys from dictionary d as a list?

a) list(d.keys())
b) d.keys()
c) keys(d)
d) d.list()
Answer: a) list(d.keys())

42. What does sorted(d) return?

a) Sorted keys
b) Sorted values
c) Sorted pairs
d) Error
Answer: a) Sorted keys

43. What is the output of:

a) ['a', 'z']
b) ['z', 'a']
c) ['1', '2']
d) Error
Answer: a) ['a', 'z']

44. Which of these functions merges two dictionaries?

a) merge()
b) update()
c) join()
d) extend()
Answer: b) update()

45. How can we delete the entire dictionary?

a) del d
b) d.clear()
c) remove(d)
d) discard(d)
Answer: a) del d

46. Which statement is true about dictionary keys?

a) Must be unique
b) Can be duplicated
c) Are stored as values
d) Are optional
Answer: a) Must be unique

47. What will be the output?

a) {"a":1,"b":2}
b) {"a":1,"b":2,"c":3}
c) {"c":3}
d) Error
Answer: b) {"a":1,"b":2,"c":3}

48. Which method is used to return a copy of the dictionary?

a) duplicate()
b) copy()
c) clone()
d) new()
Answer: b) copy()

49. What is the result of:

 

a) 3
b) None
c) Error
d) “c”
Answer: a) 3
Explanation: Adds new key with default value if not present.

50. What is the output of:

a) 1
b) 5
c) None
d) Error
Answer: a) 1
Explanation: setdefault() doesn’t overwrite existing keys.

Leave a Reply

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

sign up!

We’ll send you the hottest deals straight to your inbox so you’re always in on the best-kept software secrets.