Q. 1

What is the output of the following code ?

p = round(1.61) - round(-1.61)
e = round(0.5) - round(-0.5)
print(p, e)

Options:

Explanation

Discuss It

Q. 2

What is the output of the following code ?

def foo(arg, value):
    print(arg(value))
foo(max, [4, 5, 6])
foo(min, [4, 5, 6])

Options:

Explanation

Correct answer is : C

Function names are also be passed to another function.
So arg will be assigned to max and min for calls foo(max, [4, 5, 6]) and foo(min, [4, 5, 6])

Discuss It

Q. 3

What is the output of the following code ?

def foo():
    print("Welcome to pythoneasy.com")

foo()
foo()

Options:

Explanation

Correct answer is : B

"Welcome to pythoneasy.com" will be printed each time foo() is called

Discuss It

Q. 4

Python can create anonymous functions using keyword

Options:

Explanation

Correct answer is : B

Lambda expressions (sometimes called lambda forms) are used to create anonymous functions

Discuss It

Q. 5

What is the output of the following code ?

foo = [lambda x: x ** 2,
       lambda x: x ** 3,
       lambda x: x ** 4]

for f in foo:
    print(f(2))

Options:

Explanation

Correct answer is : B

Each lambda expression in foo list becomes an in line function and assigned to "f"in for loop

Discuss It

Q. 6

What is the output of the following code ?

k=0
def foo(k):
   k += 1
   return k
foo(k)
print(k)

Options:

Explanation

Correct answer is : C

The scope of the variable "k" is always local to the function block until you make it global using a global keyword

Discuss It

Q. 7

What is the output of the following code ?

def foo(p=5, **args):
   print(type(args))
foo(5, a='python', b='easy')

Options:

Explanation

Correct answer is : A

All the arbitrary keyword arguments will be assigned to **args as a type dict

Discuss It

Q. 8

What is the output of the following code ?

def foo():
    x = 25
    print(x)
x = 50
foo()

Options:

Explanation

Correct answer is : B

Inside function foo, the x in the statement x = 25is a local variable to the function
And x = 50 is a global variable

Discuss It

Q. 9

What is the output of the following code ?

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

Options:

Explanation

Correct answer is : A

The function foo(), is never called
So value of x will remain 12 at the outer scope as the following lines of code are only executed

x = 12
print(x)

Discuss It

Q. 10

What is the output of the following code ?

def foo():
    global x
    print(x)
    x = "hi"
    print(x) 
x = "welcome" 
foo()
print(x)

Options:

Explanation

Correct answer is : C

Inside the function the value of global variable "x" getting changed in statement x = "hi"
So the output will be

welcome
hi
hi

Discuss It

Q. 11

What is the output of the following code ?

def func(x, y, z):
    global s
    x = 10
    y = 20
    z = 30
    s = 40
    print(x, y, z, s)
x, y, z, s = 1, 2, 3, 4
func(5, 10, 15)

Options:

Explanation

Correct answer is : D

All the values of x, y, z, s are re-assigned inside the function by the following statements

x = 10
y = 20
z = 30
s = 40

So print(x, y, z, s) prints 10 20 30 40

Discuss It

Q. 12

What is the output of the following code ?

x = 200

def foo():
    global x
    x = 50

def boo():
    global x
    x = 67
print(x)

Options:

Explanation

Correct answer is : A

Both the functions foo() and bar() are changing the global variable value "x"
But none of these functions is called, so the value of "x" will remain 200

Discuss It

Q. 13

Which data type do the function globals() and locals() returns

Options:

Explanation

Correct answer is : C

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. 14

What is the output of the following code ?

x = 45
globals()['x'] = 95
print(x)

Options:

Explanation

Correct answer is : B

globals() - return a dictionary representing the current global symbol table
so changing the value for the key 'x' (which is global variable ) will change the value of "x"

Discuss It

Q. 15

What is the output of the following code ?

def factorial(number):
    if number == 0:
        return 1
    else:
        return number*factorial(number-1)

print(factorial(5))

Options:

Explanation

Correct answer is : A

This is an example for a recursive function.
factorial function calls itself and generates the factorial of the number given

Discuss It

Q. 16

What is the output of the following code ?

def foo(n):
    if n > 90:
        return n - 4
    return foo(foo(n + 10))

print(foo(45))

Options:

Explanation

Correct answer is : C

foo() is a recursive function which call itself here foo(foo(n + 10)) until n >90

Discuss It

Q. 17

Which one is incorrect about recursive function ?

Options:

Explanation

Correct answer is : C

Recursive functions are hard to debug

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