Python Dictionary MCQ

Dictionaries are one of the most versatile and widely used data structures in Python. They store key-value pairs and are mutable. Here are 15 multiple-choice questions to test your knowledge about dictionaries in Python.

1. Which of the following creates an empty dictionary?

a) dict[]
b) {}
c) ()
d) []

Answer:

b) {}

Explanation:

The {} syntax is used to create an empty dictionary in Python.

2. What will be the output of dict([[1,2],[3,4]])?

a) {1: 2, 3: 4}
b) [[1,2],[3,4]]
c) {[1,2]: [3,4]}
d) Error

Answer:

a) {1: 2, 3: 4}

Explanation:

The dict() constructor builds dictionaries directly from sequences of key-value pairs.

3. Which of the following is a valid dictionary?

a) {1: ‘a’, 2: ‘b’}
b) {[1,2]: ‘a’}
c) {(1,2): ‘a’}
d) {1, 2: ‘a’, ‘b’}

Answer:

a) {1: ‘a’, 2: ‘b’} and c) {(1,2): ‘a’}

Explanation:

Dictionary keys must be of a type that is immutable. Tuples are immutable so they can be used as keys, unlike lists.

4. How can you add a new key-value pair to an existing dictionary d?

a) d.append(‘key’, ‘value’)
b) d(‘key’) = ‘value’
c) d[‘key’] = ‘value’
d) d.add(‘key’ = ‘value’)

Answer:

c) d[‘key’] = ‘value’

Explanation:

New key-value pairs can be added to dictionaries using the assignment operator.

5. How can you remove a key-value pair from a dictionary?

a) d.remove(‘key’)
b) del d[‘key’]
c) d.delete(‘key’)
d) d.pop(‘key’)

Answer:

b) del d[‘key’] and d) d.pop(‘key’)

Explanation:

Both del and the pop() method can be used to remove key-value pairs from dictionaries.

6. What method would you use to get the value of a key if it exists, otherwise return a default value?

a) d[‘key’] or ‘default’
b) d.get(‘key’, ‘default’)
c) d.value(‘key’, ‘default’)
d) d.key(‘key’, ‘default’)

Answer:

b) d.get(‘key’, ‘default’)

Explanation:

7. Which of the following is not true about dictionary keys?

a) They must be unique.
b) They are immutable.
c) They can be changed after creation.
d) They can be numbers or strings.

Answer:

c) They can be changed after creation.

Explanation:

Dictionary keys must be of a type that is immutable, meaning they cannot be changed after creation.

8. Which method would return a list of all the keys in a dictionary?

a) d.keys()
b) d.values()
c) d.items()
d) d.list()

Answer:

a) d.keys()

Explanation:

The keys() method of a dictionary returns a list of all the keys.

9. What will be the output of {1: ‘a’, 2: ‘b’}.values()?

a) [1, 2]
b) [a, b]
c) (‘a’, ‘b’)
d) [‘a’, ‘b’]

Answer:

d) [‘a’, ‘b’]

Explanation:

The values() method of a dictionary returns a list of all the values.

10. Which method can be used to get all key-value pairs in a dictionary as tuples?

a) d.keys()
b) d.values()
c) d.items()
d) d.all()

Answer:

c) d.items()

Explanation:

The items() method returns all key-value pairs in a dictionary as tuples.

11. If d = {1: ‘a’, 2: ‘b’}, what will d[3] return?

a) None
b) ‘c’
c) Error
d) 0

Answer:

c) Error

Explanation:

Accessing a key that doesn’t exist in the dictionary will raise a KeyError.

12. Dictionaries in Python are:

a) Ordered since Python 3.7
b) Unordered
c) Sorted by default
d) Only ordered if numbers are used as keys

Answer:

a) Ordered since Python 3.7

Explanation:

Starting from Python 3.7, dictionaries maintain the insertion order of their items.

13. What does d.setdefault(‘key’, ‘default’) do?

a) Always sets ‘key’ to ‘default’ value.
b) Only sets ‘key’ to ‘default’ if ‘key’ doesn’t exist.
c) Only sets ‘key’ to ‘default’ if ‘key’ exists.
d) Checks if ‘default’ exists in ‘key’.

Answer:

b) Only sets ‘key’ to ‘default’ if ‘key’ doesn’t exist.

Explanation:

The setdefault() method returns the value of a key if it exists, otherwise inserts the key with a specified value.

14. Which method can be used to copy a dictionary?

a) d.copy()
b) d.clone()
c) dict(d)
d) Both a and c

Answer:

d) Both a and c

Explanation:

Both the copy() method and the dict() constructor can be used to create a shallow copy of a dictionary.

15. Which of the following will clear all entries from the dictionary d?

a) d.remove()
b) d.delete()
c) d = {}
d) d.clear()

Answer:

d) d.clear()

Explanation:

The clear() method removes all items from the dictionary.


By challenging yourself with these questions, you’re on the right track to mastering dictionaries in Python. Remember, practice is key to deepening your understanding. Keep coding and exploring!

Leave a Comment

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

Scroll to Top