Question 1:
What is a dictionary in Python?
a) A collection of ordered elements
b) A collection of unordered elements
c) A single element
d) A data type
Question 2:
How do you create an empty dictionary in Python?
a) empty_dict = {}
b) empty_dict = dict()
c) empty_dict = new dict()
d) Both a and b
Question 3:
How do you access the value associated with a specific key in a dictionary?
a) dictionary.value(key)
b) dictionary[key]
c) dictionary.get(key)
d) dictionary.retrieve(key)
Question 4:
What is the purpose of the len() function when used with a dictionary?
a) It returns the total number of key-value pairs in the dictionary
b) It returns the last key in the dictionary
c) It returns the length of each value in the dictionary
d) It returns the sum of all values in the dictionary
Question 5:
How do you add a new key-value pair to a dictionary?
a) dictionary.add(key, value)
b) dictionary[key] = value
c) dictionary.insert(key, value)
d) dictionary.append(key, value)
Question 6:
What is the key difference between a dictionary and a list in Python?
a) Dictionaries are ordered, while lists are unordered
b) Dictionaries are mutable, while lists are immutable
c) Dictionaries can contain only numeric elements
d) Dictionaries are unordered and do not allow duplicate keys
Question 7:
How do you check if a key is present in a dictionary?
a) key in dictionary
b) dictionary.contains(key)
c) dictionary.exists(key)
d) key.exists(dictionary)
Question 8:
What does the dictionary.keys() method return?
a) The values of the dictionary
b) The keys of the dictionary
c) The key-value pairs of the dictionary
d) The length of the dictionary
Question 9:
How do you remove a key-value pair from a dictionary?
a) dictionary.remove(key)
b) dictionary.discard(key)
c) dictionary.delete(key)
d) All of the above
Question 10:
Which method is used to retrieve the value associated with a key, and if the key is not present, it returns a default value?
a) dictionary.get(key, default)
b) dictionary.retrieve(key, default)
c) dictionary.value(key, default)
d) dictionary.fetch(key, default)
Question 11:
What is the purpose of the pop() method in Python dictionaries?
a) Adds an element to the dictionary
b) Removes the last element from the dictionary and returns its value
c) Removes the first occurrence of the specified element
d) Removes the key-value pair for a specified key
Question 12:
How do you update the value associated with a key in a dictionary?
a) dictionary.update(key, new_value)
b) dictionary[key] = new_value
c) dictionary.modify(key, new_value)
d) dictionary.change_value(key, new_value)
Question 13:
What is the output of the following code?
my_dict = {"a": 1, "b": 2, "c": 3}
del my_dict["b"]
print(my_dict)
a) {"a": 1, "b": 2, "c": 3}
b) {"a": 1, "c": 3}
c) {"a": 1, "b": 2}
d) Raises an error
Question 14:
How do you iterate over the keys of a dictionary?
a) for key in dictionary.keys():
b) for key in dictionary:
c) for key in dictionary.values():
d) for key in dictionary.items():
Question 15:
What is the purpose of the values() method in Python dictionaries?
a) Returns the keys of the dictionary
b) Returns the values of the dictionary
c) Returns the key-value pairs of the dictionary
d) Returns the length of the dictionary
Question 16:
Which method is used to clear all key-value pairs from a dictionary?
a) dictionary.clear()
b) dictionary.remove_all()
c) dictionary.delete()
d) dictionary.empty()
Question 17:
What is the purpose of the items() method in Python dictionaries?
a) Returns the keys of the dictionary
b) Returns the values of the dictionary
c) Returns the key-value pairs of the dictionary
d) Returns the length of the dictionary
Question 18:
What is the output of the following code?
my_dict = {"apple": 3, "banana": 5, "cherry": 2}
sorted_dict = dict(sorted(my_dict.items()))
print(sorted_dict)
a) {"apple": 3, "banana": 5, "cherry": 2}
b) {"cherry": 2, "apple": 3, "banana": 5}
c) {"banana": 5, "apple": 3, "cherry": 2}
d) Raises an error
Question 19:
How do you create a dictionary with keys as numbers from 1 to 5 and values as their squares?
a) squares = {i: i ** 2 for i in range(1, 6)}
b) squares = {i: i * i for i in range(1, 6)}
c) squares = {i: i ** 2 for i in [1, 2, 3, 4, 5]}
d) All of the above
Question 20:
What is the purpose of the copy() method in Python dictionaries?
a) Creates a shallow copy of the dictionary
b) Creates a deep copy of the dictionary
c) Returns the reversed dictionary
d) Appends a copy of the dictionary to itself
Answer Key:
- b) A collection of unordered elements
- d) Both a and b
- b) dictionary[key]
- a) It returns the total number of key-value pairs in the dictionary
- b) dictionary[key] = value
- d) Dictionaries are unordered and do not allow duplicate keys
- a) key in dictionary
- b) The keys of the dictionary
- d) All of the above
- a) dictionary.get(key, default)
- b) Removes the last element from the dictionary and returns its value
- b) dictionary[key] = new_value
- b) {"a": 1, "c": 3}
- b) for key in dictionary:
- b) Returns the values of the dictionary
- a) dictionary.clear()
- c) Returns the key-value pairs of the dictionary
- b) {"cherry": 2, "apple": 3, "banana": 5}
- a) squares = {i: i ** 2 for i in range(1, 6)}
- a) Creates a shallow copy of the dictionary
0 Comments:
Post a Comment