Prerequisite: Dictionary
Note: Output of all these programs is tested on Python3
1.What is the output of the following of code?
a = {i: i * i for i in range(6)} print (a) |
Options:
a) Dictionary comprehension doesn’t exist
b) {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6:36}
c) {0: 0, 1: 1, 4: 4, 9: 9, 16: 16, 25: 25}
d) {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Ans. (d)
Explanation: The above piece of code written in curly braces generate the whole Dictionary.
2.What is the output of the following of code?
a ={} a.fromkeys(['a', 'b', 'c', 'd'], 98) print (a) |
Options:
a) Syntax error
b) {‘a’:98, ‘b’:98, ‘c’:98, ‘d’:98}
c) {}
d) {‘a’:None, ‘b’:None, ‘c’:None.’d’:None}
Ans. (c)
Explanation:fromkeys() create a new dictionary with keys from list given to it as an argument and set values of the key, the default value given in it as an argument.
Input:
a ={} dict = a.fromkeys(['a', 'b', 'c', 'd'], 98) print (a) print (dict) |
Output:
{}
{'d': 98, 'b': 98, 'a': 98, 'c': 98}
3.What is the output of the following of code?
dict ={} print (all(dict)) |
Options:
a) { }
b) False
c) True
d) An exception is thrown
Ans.(c)
Explanation:The all() method returns:
Input:
a = {} b = a.fromkeys([1, False, 3], 'True') print (all(a)) print (all(b)) |
Output:
True False
4.What is the output of the following of code?
a = {'geeks' : 1, 'gfg' : 2} b = {'geeks' : 2, 'gfg' : 1} print (a == b) |
a) True
b) False
c) Error
d) None
Ans. (b)
Explanation:If two dictionary are same it returns true, otherwise it returns false.
5.Which of these about a dictionary is false?
a) The values of a dictionary can be accessed using keys
b) The keys of a dictionary can be accessed using values
c) Dictionaries may or may not be ordered
d) None of the above
Ans.(b)
Explanation: The values of a dictionary can be accessed using keys but the keys of a dictionary can’t be accessed using values.
Recommended Posts:
- Output of Python programs | Set 9 (Dictionary)
- Output of Python Programs | Set 24 (Dictionary)
- Output of python program | Set 14 (Dictionary)
- Output of Python programs | Set 8
- Output of Python programs | Set 7
- Output of Python Programs | Set 24 (Sets)
- Output of Python Programs | Set 21 (Bool)
- Output of Python Programs | Set 20 (Tuples)
- Output of Python Programs | Set 19 (Strings)
- Output of Python Programs | Set 22 (Loops)
- Output of Python Programs | Set 23 (String in loops)
- Output of Python Programs | Set 18 (List and Tuples)
- Output of Python programs | Set 10 (Exception Handling)
- Output of C++ programs | Set 45
- Output of C programs | Set 29
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



