Output of Python programs | Set 9 (Dictionary)
Last Updated :
06 Sep, 2024
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 dictionary.items():
print(key)
del dictionary;
for key, values in dictionary.items():
print(key)
a) Both b and d
b) Runtime error
c) GFG
facebook
d) facebook
GFG
Ans. (a)
Output:
facebook
GFG
Explanation: The statement: del dictionary; removes the entire dictionary, so iterating over a deleted dictionary throws a runtime error as follows:
Traceback (most recent call last):
File "cbeac2f0e35485f19ae7c07f6b416e84.py", line 12, in
for key, values in dictionary.items():
NameError: name 'dictionary' is not defined
2) What is the output of the following program?
Python
dictionary1 = {'Google' : 1,
'Facebook' : 2,
'Microsoft' : 3
}
dictionary2 = {'GFG' : 1,
'Microsoft' : 2,
'Youtube' : 3
}
dictionary1.update(dictionary2);
for key, values in dictionary1.items():
print(key, values)
a) Compilation error
b) Runtime error
c) ('Google', 1)
('Facebook', 2)
('Youtube', 3)
('Microsoft', 2)
('GFG', 1)
d) None of these
Ans. (c)
Explanation: dictionary1.update(dictionary2) is used to update the entries of dictionary1 with entries of dictionary2. If there are same keys in two dictionaries, then the value in second dictionary is used.
3) What is the output of the following program?
Python
dictionary1 = {'GFG' : 1,
'Google' : 2,
'GFG' : 3
}
print(dictionary1['GFG']);
a) Compilation error due to duplicate keys
b) Runtime time error due to duplicate keys
c) 3
d) 1
Ans. (c)
Explanation: Here, GFG is the duplicate key. Duplicate keys are not allowed in python. If there are same keys in a dictionary, then the value assigned mostly recently is assigned to that key.
4) What is the output of the following program?
Python
temp = dict()
temp['key1'] = {'key1' : 44, 'key2' : 566}
temp['key2'] = [1, 2, 3, 4]
for (key, values) in temp.items():
print(values, end = "")
a) Compilation error
b) {'key1': 44, 'key2': 566}[1, 2, 3, 4]
c) Runtime error
d) None of the above
Ans. (b)
Explanation: A dictionary can hold any value such as an integer, string, list, or even another dictionary holding key-value pairs.
5) What is the output of the following Python program?
Python
temp = {'GFG' : 1,
'Facebook' : 2,
'Google' : 3
}
for (key, values) in temp.items():
print(key, values, end = " ")
a) Google 3 GFG 1 Facebook 2
b) Facebook 2 GFG 1 Google 3
c) Facebook 2 Google 3 GFG 1
d) Any of the above
e) None of the above
Ans. (e)
Explanation for (e): Since python 3.7 dictionaries are ordered in the insertion order.
Similar Reads
Python MCQ (Multiple Choice Questions) with Answers Python is a free open-source, high-level and general-purpose with a simple and clean syntax which makes it easy for developers to learn Python. Python programming language (latest Python 3) is being used in web development, Machine Learning applications, along with all cutting-edge technology in Sof
3 min read
Output of Python Program | Set 1 Predict the output of following python programs: Program 1:Python r = lambda q: q * 2 s = lambda q: q * 3 x = 2 x = r(x) x = s(x) x = r(x) print (x) Output:24Explanation : In the above program r and s are lambda functions or anonymous functions and q is the argument to both of the functions. In firs
2 min read
Output of python program | Set 2 Difficulty level : IntermediatePredict the output of following Python Programs. Program 1: Python3 class Acc: def __init__(self, id): self.id = id id = 555 acc = Acc(111) print (acc.id) Output: 111 Explanation: Instantiation of the class "Acc" automatically calls the method __init__ and passes the o
2 min read
Output of Python Program | Set 3 Difficulty level : Intermediate Predict the output of following Python Programs. Program 1: Python3 class Geeks: def __init__(self, id): self.id = id manager = Geeks(100) manager.__dict__['life'] = 49 print (manager.life + len(manager.__dict__)) Output:51 Explanation : In the above program we are cr
2 min read
Output of Python Program | Set 4 Difficulty level : Intermediate Predict the output of the following Python Programs. Program 1: Python nameList = ['Harsh', 'Pratik', 'Bob', 'Dhruv'] print nameList[1][-1] Output: k Explanation: The index position -1 represents either the last element in a list or the last character in a String. In
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 6 (Lists) Prerequisite - Lists in Python Predict the output of the following Python programs. These question set will make you conversant with List Concepts in Python programming language. Program 1 Python list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print "list1[0]:
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 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 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