Question 1
How can you remove a specific key from a dictionary without raising an error if the key does not exist?
Question 2
What is the output of the following code?
my_dict = {"a": 1, "b": 2, "c": 3} items = my_dict.items() items[0] = ("d", 4) print(my_dict)
Question 3
How do you check if a key exists in a dictionary?
Question 4
What is a dictionary in python?
Question 5
How do you get the list of values in a dictionary?
Question 6
How can you combine two dictionaries into a new dictionary in Python?
Question 7
What is the output of the following code?
my_dict = {"a": 1, "b": 2, "c": 3} values = my_dict.values() values.append(4) print(my_dict)
Question 8
How do you access the value associated with a specific key in a dictionary?
Question 9
How do you sort a dictionary by its values in ascending order?
Question 10
How can you update the value associated with a specific key in a dictionary?
Question 11
What is the output of the following code?
import copy my_dict = {"a": [1, 2, 3], "b": [4, 5, 6]} new_dict = copy.copy(my_dict) new_dict["a"].append(99) print(my_dict["a"])
Question 12
How can you remove all key-value pairs from a dictionary?
Question 13
How can you create a dictionary with default values for missing keys using a lambda function?
Question 14
What is the output of the following code?
my_dict = {"b": 2, "a": 1, "c": 3} sorted_dict = sorted(my_dict.items(), key=lambda x: x[1]) print(sorted_dict)
Question 15
Which of the following is a valid way to create an empty dictionary in python?
Question 16
What method can you use to get a list of all keys in a dictionary?
Question 17
How can you create a copy of a dictionary in Python?
Question 18
What is the output of the following code?
keys = ["a", "b", "c"] values = [1, 2, 3] my_dict = dict(zip(keys, values)) print(my_dict)
Question 19
How can you find the number of key-value pairs in a dictionary?
Question 20
How can you iterate through all key-value pairs in a dictionary?
Question 21
What is the output of the following code?
my_dict = {"a": 1, "b": 2, "c": 3} if "b" in my_dict: print("Key found") else: print("Key not found")
Question 22
What is the output of the following code?
my_dict = {"a": 1, "b": 2, "c": 3} new_dict = my_dict.copy() new_dict["a"] = 99 print(my_dict["a"])
Question 23
What is the output of the following code?
my_dict = {"b": 2, "a": 1, "c": 3} sorted_dict = sorted(my_dict) print(sorted_dict)
Question 24
What is the output of the following code?
my_dict = {"a": 1, "b": 2, "c": 3} my_dict.pop("b") print(my_dict)
Question 25
What is the output of the following code?
my_dict = {} if not my_dict: print("Dictionary is empty") else: print("Dictionary is not empty")
Question 26
How can you check if a specific value exists in a dictionary?
Question 27
What happens if you try to add a key that already exists in a dictionary?
Question 28
How can you create a shallow copy of a dictionary?
Question 29
What is the output of the following code?
my_dict = {"a": 1, "b": 2, "c": 3} my_dict.clear() print(len(my_dict))
Question 30
What is the output of the following code?
dict1 = {"a": 1, "b": 2} dict2 = {"b": 3, "c": 4} combined_dict = dict1 + dict2 print(combined_dict)
Question 31
How do you remove a key-value pair from a dictionary?
Question 32
How can you check if a dictionary contains a specific key?
Question 33
What happens if you try to access a key that does not exist in a dictionary using square brackets [ ]?
Question 34
How can you check if a dictionary is empty?
Question 35
Which of the following is a valid use case for a dictionary in python?
Question 36
What is the output of the following code?
from collections import defaultdict my_dict = defaultdict(lambda: "N/A") my_dict["a"] = 1 print(my_dict["b"])
Question 37
How can you create a dictionary from two lists, one for keys and another for values?
Question 38
What is the difference between dict.keys() and dict.values()?
Question 39
What is the output of the following code?
my_dict = {"apple": 3, "banana": 2, "cherry": 5} print(my_dict["pear"])
Question 40
Which of the following is a valid way to add a new key-value pair to an existing dictionary?
Question 41
Which method can be used to merge two dictionaries in Python 3.9 and later?
Question 42
What is the output of the following code?
my_dict = {"a": 1, "b": 2, "c": 3} my_dict["b"] = 99 print(my_dict["b"])
Question 43
What is the output of the following code?
my_dict = {"a": 1, "b": 2, "c": 3} if 2 in my_dict.values(): print("Value found") else: print("Value not found")
Question 44
How can you create a deep copy of a dictionary?
Question 45
What method can be used to get a list of all key-value pairs in a dictionary?
Question 46
How do you create a dictionary with default values for keys that do not exist?
Question 47
What is the output of the following code?
my_dict = {"a": 1, "b": 2, "c": 3} for key in my_dict: print(key)
Question 48
In Python, are dictionary keys case-sensitive?
Question 49
What is the output of the following code?
import copy my_dict = {"a": [1, 2, 3], "b": [4, 5, 6]} new_dict = copy.deepcopy(my_dict) new_dict["a"].append(99) print(my_dict["a"])
Question 50
How do you sort a dictionary by its keys in ascending order?