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.
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 ratings0% found this document useful (0 votes)
6 views
Dictionary Paper Python
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.
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
You are on page 1/ 1
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) dict.append(key, value) b) dict[key] = value c) dict.add(key, value) d) dict.insert(key, value) 4. What will dict.keys() 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) dict.fetch() b) dict.get() c) dict.lookup() d) dict.search() 7. d = {'x': 10, 'y': 20, 'z': 30} print(d.values()) 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) dict.clear() 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) dict.keys() b) dict.values() c) dict.items() d) dict.pairs() 13. How do you delete a key-value pair from a dictionary? a) dict.remove(key) b) del dict[key] c) dict.pop(key) d) Both b and c 14. What will be the output of the following code? d = {1: 'A', 2: 'B', 3: 'C'} print(d.get(4, 'D')) 15. How can you merge two dictionaries in Python (Python 3.9+)? a) dict1 + dict2 b) dict1.update(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 dict.popitem() 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