Q. 1

What is the output of the following code ?

def func(a, b=4, c=5):
    print(a, b, c)

func(1, 2)

Options:

Explanation

Correct answer is : B

We are passing 2 positional arguments, so 1 will be assigned to a, and 2 will be assigned to b
As there is a default value for c already in function definition, it will print 5 although we are not providing it in function call

Discuss It

Q. 2

What is the output of the following code ?

def func(x, y, z=5):
    print(x, y, z)

func(1, y=3, z=2)

Options:

Explanation

Correct answer is : D

The arguments will be assigned as follows
x => 1 # positional argument
y => 3 # keyword argument
z => 2 # overriding keyword argument

Discuss It

Q. 3

What is the output of the following code ?

def func(x, *args):
    print(x, args)

func(1, 2, 3)

Options:

Explanation

Correct answer is : C

1 is a positional argument, so will be assigned to x
2, 3 are arbitrary arguments will be assigned to args tuple

Discuss It

Q. 4

What is the output of the following code ?

def func(x, y, z=3, k=4): 
    print(x, y, z, k)
func(1, *(5, 6))

Options:

Explanation

Correct answer is : C

The 1 matches x by position,
5 and 6 match y and z by *name positional (6 overrides z’s default),
k defaults to 4 because it was not passed a value.

Discuss It

Q. 5

What is the output of the following code ?

def foo(p):
    p[0] = 1
q = [0]
foo(q)
print(q)

Options:

Explanation

Correct answer is : B

The arguments in python passed as reference,
Here list is passed as reference. So changing an inner object will change the list

Discuss It

Q. 6

What is the output of the following code ?

def foo(p):
    p = ['pass', 'except']
    return id(p)
e = ['None', 'True']
print(id(e) == foo(e))

Options:

Explanation

Correct answer is : B

Here p = ['pass', 'except']we are reassigning a new list to "p" so new object is created
So id() value will be changed

Discuss It

Q. 7

What is the output of the following code ?

foo = lambda a, b: a if a < b else b
print(foo(5, 10))

Options:

Explanation

Correct answer is : A

foo = lambda a, b: a if a < b else b is similar to


def foo(a, b):
    if a < b:
        return a
    else:
        return b

Discuss It

Q. 8

What is the output of the following code ?

x = 10
y = 20
def foo():
    global y
    x = 45
    y = 56
foo()
print(x, y)

Options:

Explanation

Correct answer is : A

global y will make the variable "y" to become global inside the function block and x remains local to the function
So any change to "y" will reflect in global scope and y becomes 56 after function call
The output will be 10 56

Discuss It

Q. 9

If there is no return statement in a function, the function returns

Options:

Explanation

Correct answer is : C

The function without a return statement returns a None value.

Discuss It

Q. 10

What is the output of the following code ?

x = 27
def foo(a, b=x):
    print(a,b)
x = 24
foo(7)

Options:

Explanation

Correct answer is : A

In foo(7)we are passing the value for "a" only, so a becomes 7 and b value will be same as value of "x" i.e. 27
So it will print 7 27

Discuss It

Q. 11

What is the output of the following code ?

x = 5

def foo():
    global x
    x = 4

def bar(a, b):
    global x
    return a + b + x
foo()
print(bar(7, 8))

Options:

Explanation

Correct answer is : B

The global variable "x" value will be changed to 4 when foo() is called
Inside function bar() , the global variable "x" value will be 4 and getting added to 7 and 8 in statement a + b + x, when bar() is called

Discuss It

Q. 12

What is the output of the following code ?

a, b = 1, 2
def foo():
    global c
    c = a + b
foo()
print(a, b, c)

Options:

Explanation

Correct answer is : B

"a" and "b" are global variables are defined outside function (at module namespace)
"c" is explicitly defined as global inside the function in this statement global c and assigning value 1 + 2 = 3 to in this statement c = a + b
So all the variables a, b and c are global and will be printed as 1 2 3

Discuss It

Q. 13

What happens If you already have a global variable with the same name and you again define a new variable with same name ?

Options:

Explanation

Correct answer is : A

If you already have a global variable with the same name and you again define a new variable with same name, due to the precedence global variable will be overridden

x = "python"
def foo():
    x = "cpyhon"
    print(x)
foo()

It prints "cpyhon"

Discuss It

Q. 14

Which an is the correct about recursive function ?

Options:

Explanation

Correct answer is : B

Recursive function is a function that calls itself

Discuss It

Q. 15

What is the output of the following code ?

L = []

def foo(y):
    if y == 0:
        return L
    num = y % 2
    L.append(num)
    foo(y//2)
foo(6)
L.reverse()
for i in L:
    print(i, end=" ")

Options:

Explanation

Correct answer is : C

Here foo() function calls it self until y value becomes zero
On every call it doe a floor division foo(y//2) and call it self

Discuss It
Submit Your Answers

Interview Questions

on
Functions

Tutorial

on
Functions

Programs

on
Functions
Click any Link
to navigate to certain page easily
Write a line to us
Your Email
Title
Description