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