Python Dictionaries MCQ Questions and Answers

Python dictionaries are a type of collection in Python. They store data in key-value pairs and are mutable, meaning you can add, modify, and delete their key-value pairs. Here we present 20 multiple-choice questions to test your knowledge of Python Dictionaries. Each MCQ has the correct answer with an explanation. 

1. What is a dictionary in Python?

a) An ordered collection of items
b) A collection of key-value pairs
c) A collection of unique items
d) A mutable sequence of characters

Answer:

b) A collection of key-value pairs

Explanation:

A dictionary in Python is a collection of key-value pairs, where each key is unique and is used to access its corresponding value.

2. How do you access the value associated with the key 'color' in a dictionary 'my_dict'?

a) my_dict[color]
b) my_dict.get(color)
c) my_dict('color')
d) my_dict.get('color')

Answer:

d) my_dict.get('color')

Explanation:

The get() method is used to access the value for a given key in a dictionary. It returns None instead of an error if the key doesn't exist.

3. How do you change the value associated with the key 'age' to 30 in the dictionary 'person'?

a) person['age'] = 30
b) person.set('age', 30)
c) person(age, 30)
d) person.update('age', 30)

Answer:

a) person['age'] = 30

Explanation:

To change or add a key-value pair in a dictionary, use the syntax dictionary[key] = value.

4. How do you add a new key-value pair 'grade': 'A' to a dictionary 'student'?

a) student.add('grade', 'A')
b) student['grade'] = 'A'
c) student.put('grade', 'A')
d) student.append('grade', 'A')

Answer:

b) student['grade'] = 'A'

Explanation:

Adding a new key-value pair to a dictionary is done by assigning a value to a new key in the dictionary.

5. How can you remove the key 'address' from a dictionary 'details'?

a) del details['address']
b) details.remove('address')
c) details.pop('address')
d) Both a and c

Answer:

d) Both a and c

Explanation:

The del statement or the pop() method can be used to remove a key-value pair from a dictionary.

6. What does the keys() method return in a dictionary?

a) A list of values
b) A list of keys
c) A list of tuples
d) A list of key-value pairs

Answer:

b) A list of keys

Explanation:

The keys() method returns a view object that displays a list of all the keys in the dictionary.

7. What is the correct way to copy a dictionary 'original_dict' to 'new_dict'?

a) new_dict = original_dict
b) new_dict = original_dict.copy()
c) new_dict = copy(original_dict)
d) new_dict = dict(original_dict)

Answer:

b) new_dict = original_dict.copy()

Explanation:

The copy() method creates a shallow copy of the dictionary.

8. How do you create a nested dictionary representing a family tree?

a) {'parent': {'child1': {}, 'child2': {}}}
b) ['parent', ['child1', 'child2']]
c) ('parent', ('child1', 'child2'))
d) {{'parent'}, {'child1', 'child2'}}

Answer:

a) {'parent': {'child1': {}, 'child2': {}}}

Explanation:

Nested dictionaries are created by defining a dictionary as a value within another dictionary.

9. Which method merges two dictionaries into one?

a) merge()
b) join()
c) update()
d) concatenate()

Answer:

c) update()

Explanation:

The update() method adds the key-value pairs from one dictionary into another.

10. How do you retrieve all values from a dictionary 'my_dict'?

a) my_dict.values()
b) my_dict.getValues()
c) my_dict.keys()
d) my_dict.items()

Answer:

a) my_dict.values()

Explanation:

The values() method returns a view object that displays a list of all the values in the dictionary.

11. How do you check if a key 'name' exists in a dictionary 'person'?

a) 'name' in person
b) person.has_key('name')
c) person.containsKey('name')
d) 'name' exists in person

Answer:

a) 'name' in person

Explanation:

The in operator is used to check if a key exists in a dictionary.

12. What is the output of len({'a': 1, 'b': 2, 'c': 3})?

a) 3
b) 6
c) 1
d) 2

Answer:

a) 3

Explanation:

The len() function returns the number of key-value pairs in a dictionary.

13. How do you remove all items from a dictionary 'data'?

a) data.remove()
b) data.clear()
c) del data
d) data.delete()

Answer:

b) data.clear()

Explanation:

The clear() method removes all items from a dictionary, leaving it empty.

14. What is the result of {'a': 1, 'b': 2} + {'c': 3} in Python?

a) {'a': 1, 'b': 2, 'c': 3}
b) TypeError
c) {'a': 1, 'b': 2}
d) {'c': 3}

Answer:

b) TypeError

Explanation:

The + operator is not defined for dictionaries in Python.

15. What does the popitem() method do on a dictionary?

a) Removes an arbitrary key-value pair
b) Removes the last added key-value pair
c) Pops the first key-value pair
d) Pops a specified key-value pair

Answer:

a) Removes an arbitrary key-value pair

Explanation:

The popitem() method removes and returns a (key, value) pair as a 2-tuple. Pairs are returned in LIFO order.

16. How do you get the value associated with the key 'age' or return 0 if the key does not exist?

a) my_dict['age'] or 0
b) my_dict.get('age', 0)
c) my_dict.fetch('age', 0)
d) my_dict.default('age', 0)

Answer:

b) my_dict.get('age', 0)

Explanation:

The get() method returns the value for the specified key or the second argument if the key doesn't exist.

17. What is the output of {'a': 1, 'b': 2}.get('c', 3)?

a) 1
b) 2
c) 3
d) None

Answer:

c) 3

Explanation:

Since 'c' is not a key in the dictionary, the get() method returns the default value, which is 3.

18. How do you create a dictionary from two lists - keys = ['a', 'b', 'c'] and values = [1, 2, 3]?

a) dict(keys, values)
b) zip(keys, values)
c) dict(zip(keys, values))
d) {keys: values}

Answer:

c) dict(zip(keys, values))

Explanation:

The zip() function is used to combine two lists into a single iterator of tuples, which can be converted into a dictionary using the dict() function.

19. What does the items() method in a dictionary return?

a) A list of tuples containing key-value pairs
b) A view of key-value pairs
c) A list of keys
d) A list of values

Answer:

b) A view of key-value pairs

Explanation:

The items() method returns a view object that displays a list of dictionary's (key, value) tuple pairs.

20. How do you check if a dictionary is empty?

a) len(my_dict) == 0
b) my_dict == {}
c) isEmpty(my_dict)
d) Both a and b

Answer:

d) Both a and b

Explanation:

You can check if a dictionary is empty by comparing it with an empty dictionary {} or checking if its length is zero.


Comments