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

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?

  1. Key
  2. Pair

Answer: A) Key

Explanation:

In the dictionary, keys are always unique.

Discuss this Question


3. How do you store dictionary elements?

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

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?

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

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?

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

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?

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

  1. YES
  2. 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?

  1. DICT()
  2. DICT{}
  3. DICT=()
  4. 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?

  1. YES
  2. 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?

  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.

Discuss this Question


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

  1. Yes
  2. 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?

  1. Yes
  2. 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])
  1. 7
  2. 1
  3. 2
  4. 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?

  1. pair()
  2. key()
  3. get()
  4. 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?

  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))

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?

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

  1. remove()
  2. delete()
  3. del
  4. 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?

  1. YES
  2. 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"}
  1. del dict ["2"]
  2. del dict=["2"]
  3. 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?

  1. pop()
  2. delete()
  3. delete_all()
  4. 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?

  1. pop()
  2. delete()
  3. delete_all()
  4. 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?

  1. Yes
  2. 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?

  1. total()
  2. sum()
  3. len()
  4. 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?

  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.

Discuss this Question


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.

Discuss this Question


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()

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?

  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.

Discuss this Question


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

  1. is
  2. in
  3. check if
  4. 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)
  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}

Discuss this Question


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.