Q. 1

Which one is the out of the following code ?

def foo(arg):
    arg += '3'
    arg *= 3
    return arg

print(foo('pythoneasy'))

Options:

Explanation

Correct answer is : B

arg += '3' will produce pythoneasy3
arg *= 3 will be "pythoneasy3" * 3
So the output will be pythoneasy3pythoneasy3pythoneasy3

Discuss It

Q. 2

What is the output of the following code ?

p = [23, 2, 0, 0, 'welcome', '', {}]
print(list(filter(bool, p)))

Options:

Explanation

Correct answer is : B

filter(function, iterable) - Construct an iterator from those elements of iterable for which function returns true
All the elements of p will be input to bool as bool(23), bool(2), bool(0) and if it is True it prints the element.
For thes values 23, 2, 'welcome' , bool()will be True

Discuss It

Q. 3

What is the output of the following code ?

def find_maximum(x, y):
    if x > y:
        print(x, 'is maximum')
    elif x == y:
        print(x, 'is equal to', y)
    else:
        print(y, 'is maximum')

find_maximum(10, 2)

Options:

Explanation

Correct answer is : B

In function findmaximum 10 will be assigned to x and 2 will be assigned to y
Then we have simple if and else conditions to check which is max among x and y

Discuss It

Q. 4

What is the output of the following code ?

def add(*args):
    '''Function returns the sum of all values passed to it'''
    r = 0
    for i in args:
        r += i
    return r

print(add.__doc__)
print(add(7, 8, 9))
print(add(7, 8, 9, 10, 11))

Options:

Explanation

Correct answer is : C

functionname.\_doc__ - provides the docstring of the function
All the positional arguments are assigned to the tuple args

Discuss It

Q. 5

What is the output of the following code ?

foo = lambda x: x ** 3
print(foo(3))

Options:

Explanation

Correct answer is : A

foo = lambda x: x ** 3 is same as

 def foo(x):
     return  x ** 3

So function call print(foo(3)), prints 27

Discuss It

Q. 6

What is the output of the following code ?

def foo():
    x = 89
    print(x)
x += 21
foo()

Options:

Explanation

Correct answer is : D

At line x += 21, "x" is not defined but we are trying to add 21 to it.
Please check here, the function callfoo() is after the line x += 21 , which encounters error and breaks the program

Discuss It

Q. 7

What is the output of the following code ?

def foo():
    global x
    x += 1
    print(x)
x = 12
foo()
print(x)

Options:

Explanation

Correct answer is : B

Inside the function foo() we are changing the value (12) of the global variable "x" (adding 1 to it), which becomes 13
So print(x) will print 13 everywhere as it is global variable

Discuss It

Q. 8

What is the output of the following code ?

def bar(x, y=[]):
    y.append(x)
    return y
print(bar(7, [8, 9]))

Options:

Explanation

Correct answer is : A

When function is called here print(bar(7, [8, 9])), the x value will be 7 and y value will be [8, 9].
Then y.append(x) makes y to [8, 9, 7] and it returns from function and gets printed

Discuss It

Q. 9

What is the output of the following code ?

def foo(x):
    print("outer function")
    def bar(a):
        print("inner function")
        print(a,x)
foo(3)
bar(1)

Options:

Explanation

Correct answer is : D

The bar() function is a nested and present inside foo().
The bar() function is not available for call, so it will raise an error

Discuss It

Q. 10

Which of the following variables are global ?

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

Options:

Explanation

Correct answer is : C

"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
SO all the variables a, b and c are global

Discuss It

Q. 11

What is the output of the following code ?

x = "python"
def foo(k): print(k) + x
foo("cpython")

Options:

Explanation

Correct answer is : D

It will print "cpython" first for the statement print(k)
Here in print(k) + x, we are trying to add the return value of print(k) to "x".
but print() returns None and type of "x" is string.
So it will print error as

TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
Discuss It

Q. 12

_________ returns a dictionary which contains module namespace.
_________ returns a dictionary which contains current namespace.

Options:

Explanation

Correct answer is : A

locals(): returns a dictionary representing the current local symbol table
globals(): returns a dictionary representing the current global symbol table
More info - https://docs.python.org/3/library/functions.html#globals

Discuss It

Q. 13

Which of the following is not correct ?

Options:

Explanation

Correct answer is : C

Generally recursive function is slower than non -recursive function

Discuss It

Q. 14

In a recursive function, what happens if the base condition is not defined ?

Options:

Explanation

Correct answer is : D

Program will get into an infinite loop until the system gets out of memory

Discuss It

Q. 15

What is the output of the following code ?

def foo(x):
    if x == 0:
        return 0
    elif x == 1:
        return 1
    else:
        return foo(x-1)+foo(x-2)
for i in range(0, 6):
    print(foo(i), end=" ")

Options:

Explanation

Correct answer is : A

foo() is a recursive function which calls itself.
This foo() function prints fibonacci series

Discuss It
Submit Your Answers

Interview Questions

on
Functions

Tutorial

on
Functions

Programs

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