What will be the output of the following Python code?
def san(x): print(x+1) x=-2 x=4 san(12)
13
10
2
5
def f1(): global x x+=1 print(x) x=12 print("x")
Error
13 x
x
def f1(x): global x x+=1 print(x) f1(15) print("hello")
error
hello
16
16 hello
def f1(a,b=[]): b.append(a) return b print(f1(2,[3,4]))
[3,2,4]
[2,3,4]
[3,4,2]
def f(p, q, r): global s p = 10 q = 20 r = 30 s = 40 print(p,q,r,s) p,q,r,s = 1,2,3,4 f(5,10,15)
1 2 3 4
5 10 15 4
10 20 30 40
5 10 15 40
x = 5 def f1(): global x x = 4 def f2(a,b): global x return a+b+x f1() total = f2(1,2) print(total)
7
8
15
x=100 def f1(): global x x=90 def f2(): global x x=80 print(x)
100
90
80
Read the following Python code carefully and point out the global variables?
y, z = 1, 2 def f(): global x x = y+z
y and z
x, y and z
Neither x, nor y, nor z
list
set
dictionary
tuple
x=1 def cg(): global x x=x+1 cg() x
1
0
The local variable is shadowed
Undefined behavior
The global variable is shadowed
a=10 globals()['a']=25 print(a)
25
Junk value
def f(): x=4 x=1 f() x
4
A function that calls itself
A function execution instance that calls another execution instance of the same function
A class method that calls another class method
An in-built method that is automatically called
Recursive function can be replaced by a non-recursive function
Recursive functions usually take more memory space than non-recursive function
Recursive functions run faster than non-recursive function
Recursion makes programs easier to understand
Fill in the line of the following Python code for calculating the factorial of a number.
def fact(num): if num == 0: return 1 else: return _____________________
num*fact(num-1)
(num-1)*(num-2)
num*(num-1)
fact(num)*fact(num-1)
def test(i,j): if(i==0): return j else: return test(i-1,i+j) print(test(4,7))
Infinite loop
17
l=[] def convert(b): if(b==0): return l dig=b%2 l.append(dig) convert(b//2) convert(6) l.reverse() for i in l: print(i,end="")
011
110
3
A recursive function that has two base cases
A function where the recursive functions leads to an infinite loop
A recursive function where the function doesn’t return anything and just prints the values
A function where the recursive call is the last thing executed by the function
Observe the following Python code?
def a(n): if n == 0: return 0 else: return n*a(n - 1) def b(n, tot): if n == 0: return tot else: return b(n-2, tot-2)
Both a() and b() aren’t tail recursive
Both a() and b() are tail recursive
b() is tail recursive but a() isn’t
a() is tail recursive but b() isn’t