Output of Python Program - Dictionary (set 25)
Last Updated :
23 Jun, 2022
Prerequisite: Dictionaries in Python
These question sets will make you conversant with Dictionary Concepts in Python programming language.
Question 1: Which of the following is true about Python dictionaries?
A. Items are accessed by their position in a dictionary.
B. All the keys in a dictionary must be of the same type.
C. Dictionaries are mutable.
D. A dictionary can contain any object type except another dictionary.
Answer: C
Explanation: It means that you can change their content without changing their identity.
Question 2: Suppose we have a dictionary defined as :
Python3
Python = {'Geeks': 100, 'For': 200, 'Geeks': 300}
Python ['For':'Geeks']
What is the result of this statement:
Python3
A. [200, 300]
B. (200, 300)
C. It raises an exception.
D. 200 300
Answer: C
Explanation: Dictionaries are accessed by key, not by the position of the items. It doesn’t make sense to slice a dictionary.
Question 3: Which of the following could not be a valid dictionary key:
A. len
B. (5+7j)
C. ('Geeks','For')
D. ['Geeks','For']
Answer: D
Explanation: A list is a mutable data structure so it cannot be used as a key as it raises the risk of modification and thus, aren't hashable.
Question 4. Suppose you have the following dictionary defined as-
Python3
Python = {'Geeks': 100, 'For': 200, 'Geeks': 300}
What method call will delete the entry whose value is 100?
A. push()
B. pop()
C. append()
D. extend()
Answer: B
Explanation: The pop() is an inbuilt function in Python that removes the item from the dictionary provided the key as a parameter.
Question 5. Suppose you have a dictionary d1. Which of the following effectively creates a variable d2 which contains a copy of d1?
A. d2 = dict(d1.keys())
B. d2 = dict(d1.values())
C. d2 = d1
D. d2 = dict(d1.items())
Answer: D
Explanation: The d1 dictionary can be passed directly as an argument to dict() to create a new dictionary.
Question 6. What will be the output of the following code snippet?
Python3
y={16:"Geeks",25:"For",32:"Geeks"}
for i,j in y.items():
print(i,j,end=" ")
A. Geeks For Geeks
B. 16 Geeks 25 For 32 Geeks
C. 16 25 32
D. 16 :”Geeks” 25:”For” 32:”Geeks”
Answer: B
Explanation: Python’s print() function comes with a parameter called ‘end’. By default, the value of this parameter is ‘\n’, i.e. the new line character. You can end a print statement with any character/string using this parameter.
Question 7. What is the correct command to shuffle the following list?
Python3
d = {"Albert":70, "Suzan":85}
d["Albert"]
A. 85
B. "Albert"
C. 70
D. "Suzan"
Answer: C
Explanation: A key can be used to access the value in a dictionary.
Question 8. Which statement defined below can create a dictionary?
A. d = {“Computer”:100, “Programming”:95}
B. d = {100:” Computer”, 95:”Programming”}
C. d = { }
D. All the above.
Answer: D
Explanation: Since there are multiple methods to define dictionary ,all of which are defined above.
Question 9. Which of the following statements can be used for declaration of the dictionary?
A. {23: ‘Geeks’, 26: ‘ForGeeks’}
B. dict([[23,”Geeks”],[26,”ForGeeks”]])
C. {23,”Geeks”,26”ForGeeks”}
D. All the above
Answer: A
Explanation: Because a dictionary has a key and a value which should be defined as {key: value}
Question10. Let us assume d = {“Nobita”:70, “Doremon”:65} . Which command you will use to delete the entry for “Nobita”:
A. d.delete(“Nobita”:70)
B. d.delete(“Nobita”)
C. del d[“Nobita”]
D. del d(“Nobita”:70)
Answer: C
Explanation: The del keyword is used to delete objects. In Python everything is an object, so the del keyword can also be used to delete variables, lists, or parts of a list etc.
Similar Reads
Output of Python Programs | Set 24 (Dictionary)
Prerequisite : Python-Dictionary1. What will be the output?Python dictionary = {"geek":10, "for":45, "geeks": 90} print("geek" in dictionary) Options: 10FalseTrueErrorOutput:3. TrueExplanation: in is used to check the key exist in dictionary or not. 2. What will be the output?Python dictionary ={1:"
2 min read
Output of Python programs | Set 9 (Dictionary)
Prerequisite: Dictionary 1) What is the output of the following program? Python dictionary = {'GFG' : 'geeksforgeeks.org', 'google' : 'google.com', 'facebook' : 'facebook.com' } del dictionary['google']; for key, values in dictionary.items(): print(key) dictionary.clear(); for key, values in diction
3 min read
Output of Python Programs | (Dictionary)
Prerequisite: Dictionary Note: Output of all these programs is tested on Python3 1.What is the output of the following of code? Python3 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,
2 min read
Output of Python program | Set 5
Predict the output of the following programs: Program 1: Python def gfgFunction(): "Geeksforgeeks is cool website for boosting up technical skills" return 1 print (gfgFunction.__doc__[17:21]) Output:coolExplanation: There is a docstring defined for this method, by putting a string
3 min read
Output of Python program | Set 15 (Loops)
Prerequisite - Loops in Python Predict the output of the following Python programs. 1) What is the output of the following program? Python x = ['ab', 'cd'] for i in x: i.upper() print(x) Output:['ab', 'cd']Explanation: The function upper() does not modify a string in place, but it returns a new stri
2 min read
Output of Python Programs | Set 22 (Loops)
Prerequisite: LoopsNote: Output of all these programs is tested on Python3 1. What is the output of the following?Python mylist = ['geeks', 'forgeeks'] for i in mylist: i.upper() print(mylist) [âGEEKSâ, âFORGEEKSâ].[âgeeksâ, âforgeeksâ].[None, None].Unexpected Output: 2. [âgeeksâ, âforgeeksâ]Explana
2 min read
Output of Python programs | Set 8
Prerequisite - Lists in Python Predict the output of the following Python programs. Program 1 Python list = [1, 2, 3, None, (1, 2, 3, 4, 5), ['Geeks', 'for', 'Geeks']] print len(list) Output: 6Explanation: The beauty of python list datatype is that within a list, a programmer can nest another list,
3 min read
Output of Python programs | Set 7
Prerequisite - Strings in Python Predict the output of the following Python programs. These question set will make you conversant with String Concepts in Python programming language. Program 1Python var1 = 'Hello Geeks!' var2 = "GeeksforGeeks" print "var1[0]: ", var1[0] # statement 1 print "var2[1:5
3 min read
Output of Python Programs | Set 18 (List and Tuples)
1) What is the output of the following program? PYTHON L = list('123456') L[0] = L[5] = 0 L[3] = L[-2] print(L) a) [0, '2', '3', '4', '5', 0] b) ['6', '2', '3', '5', '5', '6'] c) ['0', '2', '3', '5', '5', '0'] d) [0, '2', '3', '5', '5', 0] Ans. (d) Explanation: L[0] is '1' and L[5] is '6', both of t
3 min read
Output of python program | Set 13(Lists and Tuples)
Prerequisite: Lists and Tuples1) What is the output of the following program? PYTHON List = [True, 50, 10] List.insert(2, 5) print(List, "Sum is: ", sum(List)) a) [True, 50, 10, 5] Sum is: 66 b) [True, 50, 5, 10] Sum is: 65 c) TypeError: unsupported operand type(s) for +: 'int' and 'str' d) [True, 5
3 min read