Python Programming Quiz

Quiz on Dictionary in Python

Time left: 300 seconds

Q.1. In a dictionary, the keys are mutable but values are immutable

Select Your Option

In a dictionary, keys are immutable and values are mutable.

Q.2. Are orders retained in a dictionary?

Select Your Option

As of Python version 3.7, dictionaries are ordered

Q.3. Output of following code
          
            d1 = {"k1":5, "k2":6}
            d2 = {"k2":6, "k1":5}
            print(d1 == d2)
          
          

Select Your Option

Both the dictionaries have same keys and values. Hence, they are equal.

Q.4. Which of the following is Incorrect way of creating a dictionary

Select Your Option

An empty dictionary can be created by either a or b methods but not c.

Q.5. What is the output of following program?
          
            d = {1:1, 2:4, 3:9, 4:16, 5:25}
            print(d.keys())
          
          

Select Your Option

keys() is a method in Python Dictionary which returns a view object that displays all a list of all the keys in the dictionary.

Q.6. Do all the keys in a dictionary must have same data type?

Select Your Option

It is not mandatory for all the keys in a dictionary to have the same data type. The keys must be of an immutable data type such as strings, numbers, or tuples.

Q.7. What is the output of following program?
          
            d = {1:1, 2:4, 3:9, 4:16, 5:25}
            d.pop(2)
            print(d)
          
          

Select Your Option

The pop() function removes the specified item from the dictionary. In the given snippet, the specified item is identified by the key number 2. Hence, it will be removed from the dictionary.

Q.8. What is the output of following program?
          
            d = {1:'apple',2:'mango',3:'banana',2:'pear'}
            print(d)
          
          

Select Your Option

A Python dictionary stores mappings of unique keys to values. Hence, a key cannot be repeated. If it is repeated, the old value of the key will be over-written by the new value. Hence, in given snippet, 'mango' is over-written by 'pear'.

Q.9. What is the output of the following code snippet?
          
            d = {[1,2]:'a',3:'b',4:'c'}
            print(d)
          
          

Select Your Option

A key in dictionary must be of an immutable data type such as strings, numbers, or tuples. A list is a mutable data type. Hence, we cannot use list to define a key in the dictionary.

Q.10. What is the output of the following code snippet?
          
            d = {2:'a',3:'b',4:'c'}
            d[1] = 'd'
            print(d)
          
          

Select Your Option

A new key (1) and value ('d') is inserted at the end of the dictionary.

Q.11. What is the output of the following code snippet?
          
            d = {1:1, 2:4, 3:9}
            for key,val in d.items():
                print(key,val, end=" ")
          
          

Select Your Option

All the keys and values present in the dictionary are printed, each followed with a space.

Q.12. What is the output of the following code snippet?
          
            d = {1: 'a', 2: 'b', 3: {'4': 'c', '5': 'd', '6': 'e'}}
            print(d)
          
          

Select Your Option

A key in dictionary must be of an immutable data type such as strings, numbers, or tuples. However, values can be either mutable or immutable. In the given snippet, the value of key 3 is another dictionary. This will not give an error as value are allowed to have mutable data types such as lists or dictionaries.

Q.13. What is the output of the following code snippet?
          
            d = {1:'apple',2:'mango',3:'banana',4:'pear'}
            print(d[2])
          
          

Select Your Option

The value for key = 2 in the dictionary will be printed.

Q.14. What is the output of the following code snippet?
          
            d = {1:1, 2:4, 3:9, 4:16, 5:25}
            d.popitem()
            d.popitem()
            print(d)
          
          

Select Your Option

popitem() is a method of Python dictionary which removes the last inserted item of the dictionary. Hence, the last 2 items will be popped when the function is called twice.

Q.15. Which of these about a dictionary is NOT true?

Select Your Option

Values of a dictionary can be accessed by using key but not vice versa.

Instructions:-

  1. This Quiz is based on Dictionary in Python
  2. Each correct answer will carry 2 points
  3. Once an option is selected you cannot select any other, So choose wisely
  4. Do not refresh the page as it will reset the quiz
  5. Once the time is up the quiz will automatically get submitted with no of Question responded
  6. Your total score alongwith your name will be shown after submission of the Quiz
  7. Wish you ALL THE BEST 👍👍

START

Results:-

Hi




Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post