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