# 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.

1. Set
2. Map
3. Dictionary
4. Tuple

Explanation:

Dictionary is used to store the values in keys and pairs.

2. In the dictionary key and pair which part holds the unique elements only?

1. Key
2. Pair

Explanation:

In the dictionary, keys are always unique.

3. How do you store dictionary elements?

1. ()
2. {}
3. []

Explanation:

Dictionary elements are stored in curly brackets {}.

4. In the dictionary, which symbol is used to separate keys and pairs?

1. ,
2. ::
3. :
4. ""

Explanation:

In Dictionary, keys and values are separated by using a colon.

5. In the dictionary, which symbol is used to separate different elements?

1. ,
2. ::
3. :
4. ""

Explanation:

The comma (, is used to separate the different dictionary elements.

6. From the given syntax, which of the following is the correct syntax to create a dictionary?

1. Dict=[ 1:"hi",2:"hello"]
2. Dict={ 1:"hi",2:"hello"}
3. Dict={ 1:"hi",2:"hello"}
4. 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.

7. Can you make an empty dictionary in Python?

1. YES
2. NO

Explanation:

In Python, we can make an empty dictionary.

8. From the given syntax will create an empty dictionary?

1. DICT()
2. DICT{}
3. DICT=()
4. DICT={}

Explanation:

DICT={} will create an empty dictionary, where DICT is the name of the dictionary.

9. Can you create a dictionary using the dict() function?

1. YES
2. NO

Explanation:

Yes, we can create a dictionary using the dict() inbuilt function.

10. From the following given syntax, which of them is the correct syntax to make a dictionary using the dict () inbuilt function?

1. Dict = dict{1: 'hello', 2: 'everyone'}
2. Dict = dict(1: 'hello', 2: 'everyone')
3. 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.

11. Is it always important to write pairs in an inverted comma?

1. Yes
2. No

Explanation:

Yes, pairs are always written in inverted commas, if it is a string.

12. Can you include lists and tuples as pairs in the dictionary?

1. Yes
2. No

Explanation:

Yes, we can include lists and tuples as pairs in the dictionary.

13. What will be the output of the following code?

```D={1:7,2:"everyone"}
print(D)
```
1. 7
2. 1
3. 2
4. 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 states the index 2 which means it will give you the pair value of the second key.

14. Apart from indexing which alternative function is used to access the pair value?

1. pair()
2. key()
3. get()
4. None

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.

15. How do you use the get() function to access the pair values?

1. D={1:7,2:"everyone"}
print(D.get(2))
2. D={1:7,2:"everyone"}
print(D=get(2))
3. D={1:7,2:"everyone"}
print(get(2))

print(D.get(2))

Explanation:

The correct syntax to use the get() function is:

```D = {1:7,2:"everyone"}
print(D.get(2))
```

16. Can you add items to the existing dictionary?

1. Yes
2. No

Explanation:

Yes, you can easily add the items to the already existing dictionary as dictionaries are mutable.

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'}
```
1. Dict = 'welcome'
Dict = 'to'
2. Dict = 'include help'
 = 'welcome'
 = 'to'
 = 'include help'
3. Dict = [0:'welcome']
Dict = [2:'to']
Dict =[3: 'include help']

Dict = 'to'

Explanation:

Option A would be the correct syntax to add the elements.

18. Which keyword is used to delete/ remove the element from the dictionary

1. remove()
2. delete()
3. del
4. remove

Explanation:

The del keyword is used to delete the element from the dictionary.

19. If you delete the key will the associated pair with the key will be also get deleted?

1. YES
2. NO

Explanation:

In the dictionary when you delete the key the associated pair with the key will also get deleted.

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"}
```
1. del dict ["2"]
2. del dict=["2"]
3. del dict(2)

Explanation:

The del dict ["2"] will delete the second element, where 2 represents the key name.

21. Which method is used to remove the entire dictionary?

1. pop()
2. delete()
3. delete_all()
4. clear()

Explanation:

The clear() method is used to delete the entire dictionary.

22. Which method is an alternative method to remove the particular item from python?

1. pop()
2. delete()
3. delete_all()
4. clear()

Explanation:

The pop() can be also used to delete a particular element.

23. Can you make a list as a key in the dictionary?

1. Yes
2. No

Explanation:

No, in the dictionary keys can never be listed. It can only be a single element like a number, string, character, etc.

24. Which function gives the total number of elements in the list?

1. total()
2. sum()
3. len()
4. keys()

Explanation:

The len() functions help you to get the total number of elements in the list.

25. What does the dict.items() method do?

1. It returns all the keys of the dictionary as a tuple
2. It returns all the keys and pairs of the dictionary as a tuple
3. 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.

26. What does the dict.values() method do?

1. It returns all the keys of the dictionary as a tuple
2. It returns all the keys and pairs of the dictionary as a tuple
3. 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.

27. Suppose you want to modify a particular key or value which of the following function you will use?

1. modify()
2. update()
3. change()
4. new()

Explanation:

The update() function is used to update the key value.

28. What does the dict.keys() method do?

1. It returns all the keys of the dictionary as a tuple
2. It returns all the keys and pairs of the dictionary as a tuple
3. 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.

29. Which keyword is used to check the particular key in the dictionary?

1. is
2. in
3. check if
4. None

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.

30. What will be the output of the following code?

```d = {'x': 1, 'y': 2, 'z': 3}
a = d.pop('y')
print(a, d)
```
1. 2 {'x': 1, 'z': 3}
2. 0 {'x': 1, 'z': 3}
3. 2 {'x': 1, 'y' : 2, 'z': 3}
4. 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}
```