Output of Python program | Set 5
Last Updated :
06 Sep, 2024
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:
cool
Explanation: There is a docstring defined for this method, by putting a string on the first line after the start of the function definition. The docstring can be referenced using the __doc__ attribute of the function. And hence it prints the indexed string.
Program 2:
Python
class A(object):
val = 1
class B(A):
pass
class C(A):
pass
print (A.val, B.val, C.val)
B.val = 2
print (A.val, B.val, C.val)
A.val = 3
print (A.val, B.val, C.val)
Output:
1 1 1
1 2 1
3 2 3
Explanation: In Python, class variables are internally handled as dictionaries. If a variable name is not found in the dictionary of the current class, the class hierarchy (i.e., its parent classes) are searched until the referenced variable name is found, if the variable is not found error is being thrown. So, in the above program the first call to print() prints the initialized value i.e, 1. In the second call since B. val is set to 2, the output is 1 2 1. The last output 3 2 3 may be surprising. Instead of 3 3 3, here B.val reflects 2 instead of 3 since it is overridden earlier.
Program 3:
Python
check1 = ['Learn', 'Quiz', 'Practice', 'Contribute']
check2 = check1
check3 = check1[:]
check2[0] = 'Code'
check3[1] = 'Mcq'
count = 0
for c in (check1, check2, check3):
if c[0] == 'Code':
count += 1
if c[1] == 'Mcq':
count += 10
print (count)
Output:
12
Explanation: When assigning check1 to check2, we create a second reference to the same list. Changes to check2 affects check1. When assigning the slice of all elements in check1 to check3, we are creating a full copy of check1 which can be modified independently (i.e, any change in check3 will not affect check1). So, while checking check1 'Code' gets matched and count increases to 1, but Mcq doesn't gets matched since its available only in check3. Now checking check2 here also 'Code' gets matched resulting in count value to 2. Finally while checking check3 which is separate than both check1 and check2 here only Mcq gets matched and count becomes 12.
Program 4:
Python
def gfg(x,l=[]):
for i in range(x):
l.append(i*i)
print(l)
gfg(2)
gfg(3,[3,2,1])
gfg(3)
Output:
[0, 1]
[3, 2, 1, 0, 1, 4]
[0, 1, 0, 1, 4]
Explanation: The first function call should be fairly obvious, the loop appends 0 and then 1 to the empty list, l. l is a name for a variable that points to a list stored in memory. The second call starts off by creating a new list in a new block of memory. l then refers to this new list. It then appends 0, 1 and 4 to this new list. So that's great. The third function call is the weird one. It uses the original list stored in the original memory block. That is why it starts off with 0 and 1.
Similar Reads
Python Quiz These Python quiz questions are designed to help you become more familiar with Python and test your knowledge across various topics. From Python basics to advanced concepts, these topic-specific quizzes offer a comprehensive way to practice and assess your understanding of Python concepts. These Pyt
3 min read
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