Predict the output of the following programs:
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.
1 1 1 1 2 1 3 2 3
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.
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 doest 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.
[0, 1] [3, 2, 1, 0, 1, 4] [0, 1, 0, 1, 4]
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.
This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Output of Python Program | Set 1
- Output of python program | Set 2
- Output of Python program | Set 17
- Output of Python Program | Set 3
- Output of Python Program | Set 4
- Output of python program | Set 15 (Modules)
- Output of python program | Set 14 (Dictionary)
- Output of Python program | Set 16 (Threads)
- Output of python program | Set 11(Lists)
- Output of Python program | Set 6 (Lists)
- Output of Python program | Set 15 (Loops)
- Output of python program | Set 13(Lists and Tuples)
- Output of python program | Set 12(Lists and Tuples)
- Output of C Program | Set 29
- Output of C Program | Set 28