Select the correct answerWhat will be the output of the following Python code?ct = 20def demo(ct): print('ct is', ct) ct = 2 print('Changed local ct to', ct)func(ct)print('ct is now', ct)Optionsct is 20Changed local ct to 2ct is now 2ct is 20Changed local ct to 2ct is now 20ct is 20Changed local ct to 2ct is now 40None of the mentioned
Question
Select the correct answerWhat will be the output of the following Python code?ct = 20def demo(ct): print('ct is', ct) ct = 2 print('Changed local ct to', ct)func(ct)print('ct is now', ct)Optionsct is 20Changed local ct to 2ct is now 2ct is 20Changed local ct to 2ct is now 20ct is 20Changed local ct to 2ct is now 40None of the mentioned
Solution
The correct answer is:
ct is 20 Changed local ct to 2 ct is now 20
Here's the step by step explanation:
- The variable ct is initially set to 20.
- The function demo is defined, which takes an argument ct. Inside this function, it first prints the value of ct, then changes ct to 2 and prints this new value.
- The function demo is called with the argument ct (which is 20 at this point).
- Inside the function, ct is printed, so "ct is 20" is output.
- Then ct is changed to 2 inside the function, and "Changed local ct to 2" is output.
- After the function call, the original ct variable (outside the function) is printed. Since the change to ct inside the function does not affect the ct outside the function (because ct inside the function is a local variable), the original value of 20 is printed, so "ct is now 20" is output.
Similar Questions
What will be the output of the following Python code?x = 50def func(x): print('x is', x) x = 2 print('Changed local x to', x)func(x)print('x is now', x)x is 50Changed local x to 2x is now 100x is 50Changed local x to 2x is now 2None of the mentionedx is 50Changed local x to 2x is now 50
Select the correct answerWhat will be the output of the following Python code?a=5def ct_1(): global a a=a+2 ct_1()print(a)Options70Error5
Select the correct answerWhat will be the output of the following Python code?res=[]def change(x): if(x==0): return res temp=x%2 res.append(temp) change(x//2)change(6)res.reverse()for k in res: print(k,end="")Options1100113Infinite loop
Select the correct answerWhat will be the output of the following Python code?def ct(y): global y y+=2 print(y)ct(5)print("hi")Optionserror77hihi
Select the correct answerWhat will be the output of the following Python code?a=71def ct1(): global a a=47def ct2(): global a a=67print(a)Options7167Error47
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.