Home »
Python
Python Dictionary MCQs
Python Dictionary MCQs: This section contains multiple-choice questions and answers on Python Dictionary. These MCQs are written for beginners as well as advanced, practice these MCQs to enhance and test the knowledge of Python Dictionary.
List of Python Dictionary MCQs
1. ____ are used to store the values in keys and pairs.
- Set
- Map
- Dictionary
- Tuple
Answer: C) Dictionary
Explanation:
Dictionary is used to store the values in keys and pairs.
Discuss this Question
2. In the dictionary key and pair which part holds the unique elements only?
- Key
- Pair
Answer: A) Key
Explanation:
In the dictionary, keys are always unique.
Discuss this Question
3. How do you store dictionary elements?
- ()
- {}
- []
Answer: B) {}
Explanation:
Dictionary elements are stored in curly brackets {}.
Discuss this Question
4. In the dictionary, which symbol is used to separate keys and pairs?
- ,
- ::
- :
- ""
Answer: C) :
Explanation:
In Dictionary, keys and values are separated by using a colon.
Discuss this Question
5. In the dictionary, which symbol is used to separate different elements?
- ,
- ::
- :
- ""
Answer: A) ,
Explanation:
The comma (, is used to separate the different dictionary elements.
Discuss this Question
6. From the given syntax, which of the following is the correct syntax to create a dictionary?
- Dict=[ 1:"hi",2:"hello"]
- Dict={ 1:"hi",2:"hello"}
- Dict={ 1:"hi",2:"hello"}
- Dict=[ 1:"hi",2:"hello"]
Answer: C) Dict={ 1:"hi",2:"hello"}
Explanation:
Dict={1:"hi",2:"hello"} is the correct syntax to create a dictionary, where 1 and 2 are the unique keys and HI and HELLO are the pairs.
Discuss this Question
7. Can you make an empty dictionary in Python?
- YES
- NO
Answer: A) YES
Explanation:
In Python, we can make an empty dictionary.
Discuss this Question
8. From the given syntax will create an empty dictionary?
- DICT()
- DICT{}
- DICT=()
- DICT={}
Answer: D) DICT={}
Explanation:
DICT={} will create an empty dictionary, where DICT is the name of the dictionary.
Discuss this Question
9. Can you create a dictionary using the dict() function?
- YES
- NO
Answer: A) YES
Explanation:
Yes, we can create a dictionary using the dict() inbuilt function.
Discuss this Question
10. From the following given syntax, which of them is the correct syntax to make a dictionary using the dict () inbuilt function?
- Dict = dict{1: 'hello', 2: 'everyone'}
- Dict = dict(1: 'hello', 2: 'everyone')
- Dict = dict({1: 'hello', 2: 'everyone'})
Answer: C) Dict = dict({1: 'hello', 2: 'everyone'})
Explanation:
Dict = dict({1: 'hello', 2: 'everyone'}) is the correct syntax to make a dictionary using dict() inbuilt function.
Discuss this Question
11. Is it always important to write pairs in an inverted comma?
- Yes
- No
Answer: A) Yes
Explanation:
Yes, pairs are always written in inverted commas, if it is a string.
Discuss this Question
12. Can you include lists and tuples as pairs in the dictionary?
- Yes
- No
Answer: A) Yes
Explanation:
Yes, we can include lists and tuples as pairs in the dictionary.
Discuss this Question
13. What will be the output of the following code?
D={1:7,2:"everyone"}
print(D[2])
- 7
- 1
- 2
- everyone
Answer: D) everyone
Explanation:
Whenever you will write an index it will start from 1 and according to the index provided it will provide you the respected pair value. For example, in the above code, D[2] states the index 2 which means it will give you the pair value of the second key.
Discuss this Question
14. Apart from indexing which alternative function is used to access the pair value?
- pair()
- key()
- get()
- None
Answer: C) Get()
Explanation:
The get() function is the alternative method to access the pair values, it will provide us the same result as we get from the indexing method.
Discuss this Question
15. How do you use the get() function to access the pair values?
- D={1:7,2:"everyone"}
print(D.get(2))
- D={1:7,2:"everyone"}
print(D=get(2))
- D={1:7,2:"everyone"}
print(get(2))
Answer: A) D={1:7,2:"everyone"}
print(D.get(2))
Explanation:
The correct syntax to use the get() function is:
D = {1:7,2:"everyone"}
print(D.get(2))
Discuss this Question
16. Can you add items to the existing dictionary?
- Yes
- No
Answer: A) Yes
Explanation:
Yes, you can easily add the items to the already existing dictionary as dictionaries are mutable.
Discuss this Question
17. How will you add these items to the already created empty dictionary? Suppose I want to have the following output:
{0: 'welcome', 2: 'to', 3: 'include help'}
- Dict[0] = 'welcome'
Dict[2] = 'to'
- Dict[3] = 'include help'
[0] = 'welcome'
[2] = 'to'
[3] = 'include help'
- Dict[0] = [0:'welcome']
Dict[1] = [2:'to']
Dict[2] =[3: 'include help']
Answer: A) Dict[0] = 'welcome'
Dict[2] = 'to'
Explanation:
Option A would be the correct syntax to add the elements.
Discuss this Question
18. Which keyword is used to delete/ remove the element from the dictionary
- remove()
- delete()
- del
- remove
Answer: C) Del
Explanation:
The del keyword is used to delete the element from the dictionary.
Discuss this Question
19. If you delete the key will the associated pair with the key will be also get deleted?
- YES
- NO
Answer: A) YES
Explanation:
In the dictionary when you delete the key the associated pair with the key will also get deleted.
Discuss this Question
20. How will you do this? Select the correct syntax to remove the element from the dictionary.
Suppose you have the following dictionary And you want to remove the 2nd element.
dict = {1:"include helps",2:"welcomes",3:"you"}
- del dict ["2"]
- del dict=["2"]
- del dict(2)
Answer: A) del dict ["2"]
Explanation:
The del dict ["2"] will delete the second element, where 2 represents the key name.
Discuss this Question
21. Which method is used to remove the entire dictionary?
- pop()
- delete()
- delete_all()
- clear()
Answer: D) clear()
Explanation:
The clear() method is used to delete the entire dictionary.
Discuss this Question
22. Which method is an alternative method to remove the particular item from python?
- pop()
- delete()
- delete_all()
- clear()
Answer: A) pop()
Explanation:
The pop() can be also used to delete a particular element.
Discuss this Question
23. Can you make a list as a key in the dictionary?
- Yes
- No
Answer: B) No
Explanation:
No, in the dictionary keys can never be listed. It can only be a single element like a number, string, character, etc.
Discuss this Question
24. Which function gives the total number of elements in the list?
- total()
- sum()
- len()
- keys()
Answer: C) len()
Explanation:
The len() functions help you to get the total number of elements in the list.
Discuss this Question
25. What does the dict.items() method do?
- It returns all the keys of the dictionary as a tuple
- It returns all the keys and pairs of the dictionary as a tuple
- It returns all the pairs of the dictionary as a tuple
Answer: B) It returns all the keys and pairs of the dictionary as a tuple
Explanation:
The dict.items() returns all the keys and pairs of the dictionary as tuple.
Discuss this Question
26. What does the dict.values() method do?
- It returns all the keys of the dictionary as a tuple
- It returns all the keys and pairs of the dictionary as a tuple
- It returns all the pairs of the dictionary as a tuple
Answer: C) It returns all the pairs of the dictionary as a tuple
Explanation:
The dict.values() returns all pairs of the dictionary as a tuple.
Discuss this Question
27. Suppose you want to modify a particular key or value which of the following function you will use?
- modify()
- update()
- change()
- new()
Answer: B) update()
Explanation:
The update() function is used to update the key value.
Discuss this Question
28. What does the dict.keys() method do?
- It returns all the keys of the dictionary as a tuple
- It returns all the keys and pairs of the dictionary as a tuple
- It returns all the pairs of the dictionary as a tuple
Answer: A) It returns all the keys of the dictionary as a tuple
Explanation:
The dict.keys() returns all keys of the dictionary as a tuple.
Discuss this Question
29. Which keyword is used to check the particular key in the dictionary?
- is
- in
- check if
- None
Answer: B) in
Explanation:
The in keyword is used to check whether the particular element is in the key or not. It returns true if the key is there in the list else it returns false.
Discuss this Question
30. What will be the output of the following code?
d = {'x': 1, 'y': 2, 'z': 3}
a = d.pop('y')
print(a, d)
- 2 {'x': 1, 'z': 3}
- 0 {'x': 1, 'z': 3}
- 2 {'x': 1, 'y' : 2, 'z': 3}
- None
Answer: A) 2 {'x': 1, 'z': 3}
Explanation:
The pop() method will remove the element, and returns the value. Here, we are removing the element whose key is 'y'. The method will return it value which is 2. Thus, the output of the above code will be:
2 {'x': 1, 'z': 3}
Discuss this Question