0% found this document useful (0 votes)
181 views1 page

Python Dictionary MCQ Guide

This document contains a multiple-choice questionnaire about dictionaries in Python, covering topics such as syntax, methods, and properties of dictionaries. It includes questions on defining empty dictionaries, accessing keys, adding key-value pairs, and handling errors. The document serves as a quiz for students at the Brilliant Institute of Technology to assess their understanding of Python dictionaries.

Uploaded by

kavitabisht133
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
181 views1 page

Python Dictionary MCQ Guide

This document contains a multiple-choice questionnaire about dictionaries in Python, covering topics such as syntax, methods, and properties of dictionaries. It includes questions on defining empty dictionaries, accessing keys, adding key-value pairs, and handling errors. The document serves as a quiz for students at the Brilliant Institute of Technology to assess their understanding of Python dictionaries.

Uploaded by

kavitabisht133
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

DICTIONARY MCQ BRILLIANT INSTITUTE OF TECHNOLOGY

1. What is the correct syntax to define an empty dictionary?


a) dict = {} b) dict = [] c) dict = () d) dict = ""
2. What will be the output of the following code?
d = {'a': 1, 'b': 2, 'c': 3} and print(d['b'])
3. How can you add a new key-value pair to an existing dictionary?
a) [Link](key, value) b) dict[key] = value
c) [Link](key, value) d) [Link](key, value)
4. What will [Link]() return?
a) A list of keys b) A dictionary object c) A set of keys d) A dict_keys object
5. What will happen if we try to access a key that does not exist in a dictionary using dict[key]?
a) Returns None b) Raises a KeyError
c) Creates a new key with None as value d) Ignores the operation
6. Which of the following methods is used to safely get a value from a dictionary without raising an
error if the key does not exist?
a) [Link]() b) [Link]() c) [Link]() d) [Link]()
7. d = {'x': 10, 'y': 20, 'z': 30}
print([Link]())
a) dict_values([10, 20, 30]) b) [10, 20, 30]
c) dict_keys([10, 20, 30]) d) Error
8. Which of the following is true for dictionary keys?
a) They must be mutable b) They must be unique
c) They can be changed after creation d) They must be integers
9. if d = {1: 'apple', 2: 'banana', 3: 'cherry'}
print(len(d))
a) 1 b) 2 c) 3 d) Error
10. Which method is used to remove all elements from a dictionary?
a) dict.delete_all() b) [Link]() c) dict.remove_all() d) dict.pop_all()
11. What will happen if we try to use a list as a key in a dictionary?
a) The dictionary will work fine
b) The list will be converted into a tuple and used as a key
c) A TypeError will occur d) The list will be ignored
12. Which method is used to get all key-value pairs as a list of tuples?
a) [Link]() b) [Link]() c) [Link]() d) [Link]()
13. How do you delete a key-value pair from a dictionary?
a) [Link](key) b) del dict[key] c) [Link](key) d) Both b and c
14. What will be the output of the following code?
d = {1: 'A', 2: 'B', 3: 'C'}
print([Link](4, 'D'))
15. How can you merge two dictionaries in Python (Python 3.9+)?
a) dict1 + dict2 b) [Link](dict2) c) dict1 | dict2 d) dict1 & dict2
16. What is the time complexity of retrieving a value from a dictionary?
a) O(1) b) O(n) c) O(log n) d) O(n log n)
17. What does [Link]() do?
a) Removes and returns an arbitrary key-value pair
18. if d = {1: 'one', 2: 'two', 3: 'three'}
print(2 in d)
a) True b) False c) None d) Error
19. What does the setdefault() method do?
b) Returns the value of a key,& if the key is not found, inserts the key with a specified default value
20. Which of the following can be used as a key in a Python dictionary?
a) A list b) A dictionary c) A tuple d) A set

Common questions

Powered by AI

The pop() method removes a specified key and returns its value, enabling targeted deletions, while popitem() removes and returns an arbitrary key-value pair, typically used when the specific pair to remove is not essential .

The get() method provides error handling by returning a specified default value if a key is not found, instead of raising a KeyError like direct key lookup operations, thus allowing for safe access in cases of uncertain key existence .

The dict.items() method returns a view object that displays a list of a dictionary's key-value tuple pairs. This is useful for iteration over the dictionary or converting the items to a list or other data structures for further processing .

Dictionary keys must be unique and immutable, which ensures data integrity by preventing accidental modification of keys and maintaining consistent access and reference to values within the dictionary .

To define an empty dictionary in Python, the correct syntax is 'dict = {}'. This contrasts with other data structures such as a list (defined by '[]'), a tuple (defined by '()'), and a string (defined by '""').

Dictionary keys, by being unique and immutable, allow for efficient retrieval through hash table implementations, achieving an average time complexity of O(1) for lookups .

The setdefault() method simplifies handling missing keys by returning the value if the key exists, or inserting the key with a specified default value if it does not, thus avoiding KeyErrors and allowing for default value assignments .

dict.update() incorporates all key-value pairs from another dictionary into the current one, only adding or updating the existing keys. The '|' operator merges two dictionaries into a new one from Python 3.9+, while dict.clear() removes all elements from the dictionary, useful for completely resetting the dictionary content .

Using a list or dictionary as a key in a Python dictionary will raise a TypeError. This is because both lists and dictionaries are mutable, and Python requires that dictionary keys be immutable data types, such as tuples .

Attempting to access a non-existent key in a Python dictionary using the subscript notation, as in dict[key], raises a KeyError . To avoid this, you can use the get() method which allows you to specify a default value instead of throwing an exception if the key is not found .

You might also like