Search Tutorials


Python Dictionary MCQ Questions and Answers | JavaInUse

Python Dictionary MCQ Questions and Answers

Q. What is the correct syntax to create an empty dictionary in Python?

A. dict()
B. {}
C. []
D. None of the above

Q. How can you access the value associated with the key "name" in the dictionary "person"?

A. person.get("name")
B. person["name"]
C. person.value("name")
D. person.fetch("name")

Q. What will be the output of this code: colors = {"red": 2, "green": 4, "blue": 6}; print(colors.values())

A. dict_values[2, 4, 6]
B. [2, 4, 6]
C. dict_values{2: "red", 4: "green", 6: "blue"}
D. None of the above

Q. What is the output of this code: grades = {"Alice": 85, "Bob": 92, "Charlie": 78}; print(grades.items())

A. dict_items[("Alice", 85), ("Bob", 92), ("Charlie", 78)]
B. [("Alice", 85), ("Bob", 92), ("Charlie", 78)]
C. dict_items{"Alice": 85, "Bob": 92, "Charlie": 78}
D. None of the above

Q. What will be the output of this code: scores = {("Math": 90), ("Science": 85), ("History": 78)}; print(scores)

A. {'Math': 90, 'Science': 85, 'History': 78}
B. {('Math', 90), ('Science', 85), ('History', 78)}
C. {'Math': ('Science', 85), 'History': 78}: 90
D. None of the above

Q. How can you add a new key-value pair to an existing dictionary?

A. Using the assignment operator =
B. Using the append method (append())
C. Using the update method (update())
D. None of the above

Q. What will be the output of this code: inventory = {"apples": 5, "bananas": 3, "oranges": 8}; del inventory["bananas"]; print(inventory)

A. {"apples": 5, "oranges": 8}
B. {"apples": 5}
C. {"bananas": 3, "oranges": 8}
D. None of the above

Q. What is the purpose of the get() method in a dictionary?

A. To retrieve the value associated with a key
B. To add a new key-value pair
C. To remove a key-value pair
D. To update the value of a key

Q. What will be the output of this code: info = {"name": "Alice", "age": 25}; print(info.keys())

A. dict_keys["name", "age"]
B. ["name", "age"]
C. dict_keys{"name": "Alice", "age": 25}
D. None of the above

Q. How can you check if a specific key exists in a dictionary?

A. Using the in operator
B. Using the has_key() method
C. Using the contains() method
D. None of the above

Q. What will be the output of this code: data = {"a": 1, "b": 2, "c": 3}; sorted_data = sorted(data); print(sorted_data)

A. ['a', 'b', 'c']
B. [1, 2, 3]
C. [('a', 1), ('b', 2), ('c', 3)]
D. None of the above

Q. What is the output of this code: student = {"name": "Emma", "age": 16, "grade": "A"}; print(student.get("grade", "N/A"))

A. A
B. Emma
C. 16
D. N/A

Q. What will be the output of this code: numbers = {1: "one", 2: "two", 3: "three"}; print(numbers[2])

A. 2
B. "two"
C. "one"
D. None of the above

Q. How can you merge two dictionaries into one?

A. Using the + operator
B. Using the update() method
C. Using the merge() method
D. None of the above

Q. What will be the output of this code: colors = {"red", "green", "blue"}; print(colors)

A. {'red', 'green', 'blue'}
B. ['red', 'green', 'blue']
C. ("red", "green", "blue")
D. None of the above

Q. What is a Python dictionary?

A. A data structure that stores data in key-value pairs
B. A type of function that operates on key-value pairs
C. A method for sorting data based on keys
D. A data structure similar to a list, but with faster lookup times

Q. How do you create an empty dictionary in Python?

A. my_dict = {}
B. my_dict = []
C. my_dict = ()
D. my_dict = ''

Q. How do you access the value associated with a key in a dictionary?

A. my_dict.key
B. my_dict[key]
C. my_dict.get(key)
D. my_dict * key

Q. What is the output of the following code snippet?

my_dict = {'a': 1, 'b': 2, 'c': 3}
print(my_dict['a'])
A. 1
B. 2
C. 3
D. Error: 'a' is not a valid key

Q. What happens if you try to access a non-existent key in a dictionary?

A. The program returns a default value
B. The program raises a KeyError exception
C. The program prints "None"
D. The program terminates

Q. How can you add or modify a key-value pair in a dictionary?

A. my_dict.add(key, value)
B. my_dict[key] = value
C. my_dict.update(key, value)
D. my_dict.set(key, value)

Q. What is the output of the following code snippet?

my_dict = {'a': 1, 'b': 2}
my_dict['b'] = 3
print(my_dict)
A. {'a': 1, 'b': 2}
B. {'a': 1, 'b': 3}
C. {'a': 1}
D. {'b': 3}

Q. How can you remove a key-value pair from a dictionary?

A. my_dict.pop(key)
B. del my_dict[key]
C. my_dict.remove(key)
D. my_dict.discard(key)

Q. What is the output of the following code snippet?

my_dict = {'a': 1, 'b': 2, 'c': 3}
del my_dict['b']
print(my_dict)
A. {'a': 1, 'c': 3}
B. {'a': 1}
C. {'b': 2, 'c': 3}
D. {'a': 1, 'b': None}

Q. How can you get the keys from a dictionary?

A. my_dict.keys()
B. my_dict.get_keys()
C. my_dict.key_list()
D. my_dict[:]

Q. What is the output of the following code snippet?

my_dict = {'a': 1, 'b': 2, 'c': 3}
keys = my_dict.keys()
print(keys)
A. dict_keys['a', 'b', 'c']
B. ['a', 'b', 'c']
C. {'a', 'b', 'c'}
D. Error: cannot print keys directly

Q. How can you get the values from a dictionary?

A. my_dict.values()
B. my_dict.get_values()
C. my_dict.value_list()
D. my_dict.values = []

Q. What is the output of the following code snippet?

my_dict = {'a': 1, 'b': 2, 'c': 3}
values = my_dict.values()
print(values)
A. dict_values[1, 2, 3]
B. [1, 2, 3]
C. {1, 2, 3}
D. Error: cannot print values directly

Q. How can you check if a key exists in a dictionary?

A. 'key' in my_dict
B. my_dict.has_key('key')
C. my_dict.contains('key')
D. my_dict.find('key')

Q. What is the output of the following code snippet?

my_dict = {'a': 1, b': 2}
print('a
in my_dict)
A. True
B. False
C. Error: invalid syntax
D. None