Python Dictionary Quiz - MCQ Questions and Answers

Welcome to this Python Dictionary Quiz designed for beginners! This quiz covers the basics of dictionaries in Python, a powerful and versatile data structure essential for storing and managing data pairs. Whether you're just starting out or looking to refresh your knowledge, these questions will help you understand the fundamentals of Python dictionaries. Let's get started!

1. What is a Python dictionary?

a) A list of numbers
b) A collection of key-value pairs
c) A type of Python function
d) A sequence of characters

Answer:

b) A collection of key-value pairs

Explanation:

A Python dictionary is a collection of key-value pairs where each key is unique and is used to store and retrieve values.

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

a) dict[key]
b) dict(value)
c) dict.key
d) dict.value(key)

Answer:

a) dict[key]

Explanation:

In Python dictionaries, values are accessed by specifying the key in square brackets, like dict[key].

3. Which of the following is the correct way to create an empty dictionary?

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

Answer:

b) dict = {}

Explanation:

An empty dictionary in Python is created using curly braces {}, not square brackets [] or parentheses ().

4. What will be the output of the following code?

my_dict = {'a':1, 'b':2, 'c':3}    
  print(my_dict['b'])
a) 1
b) 2
c) 3
d) 'b'

Answer:

b) 2

Explanation:

This code accesses the value associated with the key 'b' in the dictionary, which is 2.

5. What is the result of the following operation?

 'a' in {'a': 1, 'b': 2}
a) True
b) False
c) Error
d) None

Answer:

a) True

Explanation:

The 'in' operator checks if the key 'a' is present in the dictionary, which it is, so it returns True.

6. How do you add a new key-value pair to a dictionary?

a) dict.add(key, value)
b) dict[key] = value
c) dict.append(key: value)
d) dict.insert(0, key: value)

Answer:

b) dict[key] = value

Explanation:

A new key-value pair is added to a dictionary by assigning a value to a new key using dict[key] = value.

7. What will be the output of the following code?

  my_dict = {'a':1, 'b':2, 'c':3}    
  print(len(my_dict))
a) 3
b) 6
c) 9
d) 12

Answer:

a) 3

Explanation:

The len() function returns the number of key-value pairs in the dictionary, which is 3 in this case.

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

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

Answer:

b) del dict[key]

Explanation:

The del statement is used to remove a key-value pair from a dictionary by specifying the key.

9. Which method is used to get the value for a key or return a default if the key is not found?

a) get()
b) find()
c) lookup()
d) search()

Answer:

a) get()

Explanation:

The get() method is used to retrieve the value of a specified key or return a default value if the key is not found.

10. What will be the output of the following code?

my_dict = {'a':1, 'b':2, 'c':3}     
print(my_dict.get('d', 4))
a) 1
b) 2
c) 3
d) 4

Answer:

d) 4

Explanation:

Since 'd' is not a key in the dictionary, the get() method returns the default value 4.

11. What type of keys can be used in a Python dictionary?

a) Only strings
b) Only numbers
c) Any immutable type
d) Any type

Answer:

c) Any immutable type

Explanation:

Keys in a Python dictionary can be any immutable type, such as strings, numbers, and tuples.

12. What will be the output of the following code?

my_dict = {'a': 1, 'b': 2}     
  my_dict['a'] = 10     
  print(my_dict) 
a) {'a': 1, 'b': 2}
b) {'a': 10, 'b': 2}
c) {'b': 2}
d) Error

Answer:

b) {'a': 10, 'b': 2}

Explanation:

Assigning a new value to an existing key in a dictionary updates the value associated with that key.

13. How do you get a list of all keys in a dictionary?

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

Answer:

a) dict.keys()

Explanation:

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

14. What will happen if you try to access a key that doesn’t exist in a dictionary?

a) The key will be added automatically.
b) A default value is returned.
c) An error is raised.
d) Nothing happens.

Answer:

c) An error is raised.

Explanation:

Accessing a non-existent key in a dictionary raises a KeyError.

15. What does the pop() method do in a dictionary?

a) Adds a new key-value pair
b) Removes and returns an element based on the key
c) Clears the entire dictionary
d) Returns the number of elements

Answer:

b) Removes and returns an element based on the key

Explanation:

The pop() method removes the key-value pair with the specified key and returns its value.

16. Which method is used to remove all items from a dictionary?

a) clear()
b) delete()
c) removeall()
d) empty()

Answer:

a) clear()

Explanation:

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

17. How are duplicate keys handled in a Python dictionary?

a) They are allowed.
b) They overwrite existing entries.
c) An error is raised.
d) They are ignored.

Answer:

b) They overwrite existing entries.

Explanation:

If a duplicate key is used in a dictionary, the new value overwrites the existing value associated with that key.

18. What is the output of the following code?

my_dict = {'a': 1, 'b': 2, 'c': 3}     
  print(my_dict['d'])  
a) 1
b) 2
c) 3
d) KeyError

Answer:

d) KeyError

Explanation:

Trying to access a key that doesn't exist in the dictionary ('d' in this case) results in a KeyError.

19. What will be the output of the following code?

  my_dict = {'a': 1, 'b': 2, 'c': 3}     
  my_dict.update({'c': 4, 'd': 5})     
  print(my_dict)   
a) {'a': 1, 'b': 2, 'c': 3, 'd': 5}
b) {'a': 1, 'b': 2, 'c': 4, 'd': 5}
c) {'c': 4, 'd': 5}
d) Error

Answer:

b) {'a': 1, 'b': 2, 'c': 4, 'd': 5}

Explanation:

The update() method updates the dictionary with elements from another dictionary, adding new elements and updating existing ones.

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

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

Answer:

c) A list of tuples containing key-value pairs

Explanation:

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

Comments