Multiple-Choice Quiz on Python Dictionaries
What is the primary structure used to store data in a Python dictionary?
A) List
B) Set
C) Key-Value pairs
D) TupleWhich of the following is true about the keys in a Python dictionary?
A) They can be mutable data types
B) They must be unique
C) They can be of any data type, including lists
D) They are automatically sortedHow do you access the value associated with a key in a Python dictionary?
A) Using the index number
B) Using the key itself in square brackets
C) Using the key in parentheses
D) Using the key with a dot notationWhat will be the output of the following code:
d = {'a': 1, 'b': 2}; print(d['b'])
?
A) 1
B) ‘b’
C) 2
D) KeyErrorWhich method would you use to add a new key-value pair to an existing dictionary?
A) append()
B) add()
C) update()
D) insert()If you want to remove a key-value pair from a dictionary, which method would you typically use?
A) delete()
B) pop()
C) remove()
D) discard()What will happen if you try to access a key that does not exist in a dictionary?
A) It returns None
B) It creates a new key with a default value
C) It raises a KeyError
D) It returns an empty stringWhich of the following methods can be used to retrieve all keys in a dictionary?
A) keys()
B) get_keys()
C) list_keys()
D) all_keys()How can you check if a specific key exists in a Python dictionary?
A) Using the in operator
B) Using the exists() method
C) Using the check() method
D) Using the includes() methodIn Python dictionaries, what is the time complexity for accessing a value by key?
A) O(n)
B) O(log n)
C) O(1)
D) O(n^2)
Answers:
- C
- B
- B
- C
- C
- B
- C
- A
- A
- C
0 Comments:
Post a Comment
Note: only a member of this blog may post a comment.
<< Home