Test Series - python

Test Number 69/108

Q: What will be the output of the following Python code?

def f1():
    x=15
    print(x)
x=12
f1()
A. Error
B. 12
C. 15
D. 1512
Solution: In the code shown above, x=15 is a local variable whereas x=12 is a global variable. Preference is given to local variable over global variable. Hence the output of the code shown above is 15.
Q: What will be the output of the following Python code?

def f1():
    x=100
    print(x)
x=+1
f1()
A. Error
B. 100
C. 101
D. 99
Solution: The variable x is a local variable. It is first printed and then modified. Hence the output of this code is 100.
Q: What will be the output of the following Python code?

def san(x):
    print(x+1)
x=-2
x=4
san(12)
A. 13
B. 10
C. 2
D. 5
Solution: The value passed to the function san() is 12. This value is incremented by one and printed. Hence the output of the code shown above is 13.
Q: What will be the output of the following Python code?

def f1():
    global x
    x+=1
    print(x)
x=12
print("x")
A. Error
B. 13
C. 13 x
D. x
Solution: In the code shown above, the variable ‘x’ is declared as global within the function. Hence the output is ‘x’. Had the variable ‘x’ been a local variable, the output would have been:
13
x
Q: What will be the output of the following Python code?

def f1(x):
    global x
    x+=1
    print(x)
f1(15)
print("hello")
A. error
B. hello
C. 16
D. 16 hello
Solution: The code shown above will result in an error because ‘x’ is a global variable. Had it been a local variable, the output would be: 16
hello
Q: What will be the output of the following Python code?

x=12
def f1(a,b=x):
    print(a,b)
x=15
f1(4)
A. Error
B. 12 4
C. 4 12
D. 4 15
Solution: At the time of leader processing, the value of ‘x’ is 12. It is not modified later. The value passed to the function f1 is 4. Hence the output of the code shown above is 4 12.
Q: What will be the output of the following Python code?

def f():
    global a
    print(a)
    a = "hello"
    print(a) 
a = "world" 
f()
print(a)
A.  hello hello world
B.  world hello hello
C.  hello world world
D.  world hello world
Solution: Since the variable ‘a’ has been explicitly specified as a global variable, the value of a passed to the function is ‘world’. Hence the output of this code is:
world
hello
hello
Q: What will be the output of the following Python code?

def f1(a,b=[]):
    b.append(a)
    return b
print(f1(2,[3,4]))
A. [3,2,4]
B. [2,3,4]
C. Error
D. [3,4,2]
Solution: In the code shown above, the integer 2 is appended to the list [3,4]. Hence the output of the code is [3,4,2]. Both the variables a and b are local variables.
Q: What will be the output of the following Python code?

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)
A. 1 2 3 4
B. 5 10 15 4
C. 10 20 30 40
D. 5 10 15 40
Solution: The above code shows a combination of local and global variables. The output of this code is: 10 20 30 40
Q: What will be the output of the following Python code?

def f(x):
    print("outer")
    def f1(a):
        print("inner")
        print(a,x)
f(3)
f1(1)
A. outer error
B. inner error
C. outer inner
D. error
Solution: The error will be caused due to the statement f1(1) because the function is nested. If f1(1) had been called inside the function, the output would have been different and there would be no error.

You Have Score    /10