Programming and Problem Solving Through Python Language Theory Question

Dictionary In Python - Set 2

  1. What is the output of the following code?
    dict={"Joey":1,"Rachel":2}
    dict.update({"Phoebe":2})
    print(dict)
    1. {"Joey":1,"Rachel":2,"Phoebe":2}
    2. {"Joey":1,"Rachel":2}
    3. {"Joey":1,"Rachel":2,"Phoebe":2}
    4. Error
    Answer : A
    Explain : The dictionary update with the data given.
  2. What is a dictionary in Python ?
    1. A collection of ordered elements.
    2. A collection of elements with unique keys.
    3. A collection of elements that cannot be modified.
    4. A collection of elements with duplicate values.
    Answer : B
    Explain : Dictionary is defined as key and value.
  3. How do you create an empty dictionary in Python?
    1. my_dict = {}
    2. my_dict = []
    3. my_dict = ()
    4. my_dict = {None}
    Answer : A
    Explain : To create an empty dictionary used curly brackets {}.
  4. Which of the following is a valid way to access a value in a dictionary by its key ?
    1. my_dict.get(key)
    2. my_dict[key]
    3. my_dict.value(key)
    4. my_dict.access(key)
    Answer : B
    Explain : To get value use 'dictionary_name[key]' it gives the value which is related to key.
  5. What happens when you try to access a key that doesn't exist in a dictionary in Python ?
    1. Error
    2. None is returned.
    3. An empty string is returned.
    4. The dictionary is modified to include the new key.
    Answer : A
    Explain : If we try to access a that does not exist it give key error.
  6. What is the purpose of the keys() method for dictionaries in Python ?
    1. It returns a list of all the values in the dictionary.
    2. It returns a list of all the keys in the dictionary.
    3. It returns a list of key-value pairs in the dictionary.
    4. It removes a key from the dictionary.
    Answer : B
    Explain : keys() ('use it - dictionary_name.keys()')function give all the keys in form of dict_key([keys,keys,...]) and values() gives values of dictionary.
  7. Which method can be used to remove a key-value pair from a dictionary in Python?
    1. remove()
    2. delete()
    3. pop()
    4. discard()
    Answer : C
    Explain : pop() method removes the key and value pair from the dictionary. Syntax : dict_name.pop("Key").
  8. In Python, can the keys in a dictionary be of different data types?
    1. Yes, all data types are allowed.
    2. No, keys must be integers.
    3. Yes, but they must all be of the same data type.
    4. No, keys must be strings.
    Answer : A
    Explain : Dictionary is an unordered collection of items where each item consist of key and value, we can store different data type in it.
  9. Keys of dictionary must be ____
    1. must be integer only
    2. must be letter
    3. unique
    4. Must be combination of integer and word
    Answer : C
    Explain : Keys of dictionary must be uniques, one keys cannot use for two values.
  10. In dictionary keys and value are seperated by ?
    1. :
    2. -
    3. ;
    4. .
    Answer : A
    Explain : We can seperate key and value by : (semi colon).
  11. What A and B repersent in dictionary - {"A":20,"B":30} ?
    1. Values
    2. Keys
    3. Items
    4. String
    Answer : B
    Explain : In dict. A and B repersent keys and 20 or 30 repersent value.
  12. In a dictionary, can two keys have the same value ?
    1. Yes, as long as the keys are of the same data type.
    2. Yes, as long as the values are integers.
    3. No, each key must have a unique value.
    4. No, keys cannot have values.
    Answer : C
    Explain : Each key is unique in dictionary, we cannot assign one key two different values.
  13. What is the result of the following code?
    my_dict = {"apple": 3, "banana": 2, "cherry": 5}
    result = my_dict.pop("banana")
    print(result)
    1. {"apple": 3, "cherry": 5}
    2. {"apple": 3, "banana": 2, "cherry": 5}
    3. 2
    4. 5
    Answer : C
    Explain : result variable store deleted value and then this value print which give 2.
  14. What is the purpose of the update() method for dictionaries in Python ?
    1. It updates the values of the dictionary with new values.
    2. It adds a new key-value pair to the dictionary.
    3. It removes a key-value pair from the dictionary.
    4. It returns the number of key-value pairs in the dictionary.
    Answer : A
    Explain : update () is used to update the dictionary with new dictionary.
  15. Which method can be used to remove all key-value pairs from a dictionary in Python, leaving it empty ?
    1. empty()
    2. clear()
    3. remove_all()
    4. delete_all()
    Answer : B
    Explain : clear() method is used to delete all items in dictionary and return empty dictionary.

Next Set

1