Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit series activities – click on the task to see the details of the activities.
Difficulty level: Difficulty Predict the output of the following programs:
A program:
def gfgFunction() :
"juejin is cool website for boosting up technical skills"
return 1
print (gfgFunction.__doc__[10:14])
Copy the code
Output:
cool
Copy the code
Description: This method defines a docstring by placing a string on the first line after the start of the function definition. You can use the doc attribute of a function to reference the docstring. So it prints the index string.
Program 2:
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)
Copy the code
Output:
1 1 1
1 2 1
3 2 3
Copy the code
Explanation: In Python, class variables are handled internally as dictionaries. If the variable name is not found in the dictionary of the current class, the class hierarchy (its parent class) is searched until the referenced variable name is found, and an error is thrown if the variable is not found. Therefore, in the above program, the first call to print() prints the initial value, which is 1. In the second call, since b.val is set to 2, the output is 1, 2, 1. The final output 3, 2, 3 May be surprising. Here, b. val reflects 2 instead of 3, instead of 3, 3, 3, because it was covered before.
Three procedures:
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)
Copy the code
Output:
12
Copy the code
Explanation: When we assign check1 to check2, we create a second reference to the same list. Changes to CHECK2 affect Check1. When we assign slices of all elements in Check1 to Check3, we create a full copy of Check1 that can be modified independently (that is, any changes in Check3 will not affect Check1). Therefore, when checking check1, the “code” gets a match and the count increases to 1, but Mcq will not match because it is only available in check3. Now check here that check2 also matches “code”, resulting in a count of 2. Finally, when checking check3 separated from check1 and check2, only Mcq matches and the count becomes 12.
Four procedures:
def gfg(x,l=[]) :
for i in range(x):
l.append(i*i)
print(l)
gfg(2)
gfg(3[3.2.1])
gfg(3)
Copy the code
Output:
[0.1]
[3.2.1.0.1.4]
[0.1.0.1.4]
Copy the code
Note: The first function call should be fairly obvious, with the loop appending 0 and 1 to the empty list L. L is the name of a variable that points to a list stored in memory. The second call starts by creating a new list in a new block of memory. L Then references the new list. It then appends 0, 1, and 4 to this new list. That would be great. The third function call is strange. It uses the raw list stored in the raw memory block. That’s why it starts at 0 and 1.
If you find anything wrong, let me know in the comments section below, learn from each other and make progress together!