Q. 1

What is the output of the following code ?

print(round(234.56789))

Options:

Explanation

Correct answer is : A

round(number[, ndigits]) - Return number rounded to ndigits precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input.

Discuss It

Q. 2

What is the output of the following code ?

print(bin(37))

Options:

Explanation

Correct answer is : D

bin()- Convert an integer number to a binary string.

Discuss It

Q. 3

How many numbers of keyword arguments can be passed to a function in a single function call ?
Example:

def foo(**kwargs):
    pass

Options:

Explanation

Correct answer is : D

Keyword argument **kwargs can take 0 or more number of arguments in a dictionary

Discuss It

Q. 4

What is the output of the following code ?

def foo():
    return var + 1
var = 2
print(foo())

Options:

Explanation

Correct answer is : A

var is a global variable. so can be read inside the function (Only read)
Adding 1 to it will change the value of var in the global scope.

Discuss It

Q. 5

What is the output of the following code ?

def f(a, L=[]):
    L.append(a)
    return L

for i in range(3):
    print(f(i))

Options:

Explanation

Correct answer is : A

The default value is evaluated only once
So here same list will be executed every time function is called

Discuss It

Q. 6

Which of the following is correct ?

Options:

Explanation

Correct answer is : B

Functions are reusable pieces of codes

Discuss It

Q. 7

Which keyword is used to define a function ?

Options:

Explanation

Correct answer is : B

def keyword is used to define a function.

def foo():
    print("Python Functions")

Discuss It

Q. 8

What is the output of the following code ?

x = 100
def scope_test():
    global x
    print('x == ', x)
    x = 2
    print('X is changed to == ', x)
scope_test()
print('x is at outer scope', x)

Options:

Explanation

Correct answer is : A

Inside the function we are making x to global using global keyword and changing x value to change will change all value of x to 2

Discuss It

Q. 9

What is the output of the following code ?

x = 100
def scope_test(x):
    global x
    print('x == ', x)
    x = 2
    print('X is changed to == ', x)
scope_test(x)
print('x is at outer scope', x)

Options:

Explanation

Correct answer is : D

We can not override the parameter x to global variable
Refer to - https://docs.python.org/3/tutorial/classes.html#python-scopes-and-namespaces

Discuss It

Q. 10

What is the output of the following code ?

def foo(x, y=13, z=35):
    print('x = ', x, 'y = ', y, 'z = ', z)

foo(7, 9)
foo(12, z=83)
foo(z=78, x=300)

Options:

Explanation

Correct answer is : C

For function call foo(7, 9 - the arguments are passed as positional so x will be 7 and y will be 9 and z will be default
For foo(12, z=83), 12 is passed as positional so will be assigned to x and 83 to and y will be default
For foo(z=78, x=300), keyword arguments are passed for x and z and y value will be default

Discuss It

Q. 11

What is the output of the following code ?

def foo(a):
    return a * a * a
x = foo(2)
print(x)

Options:

Explanation

Correct answer is : B

As 2 is passed to the function, the expression a * a * a becomes 2 * 2 * 2 i.e 8 and this value is returned from function

Discuss It

Q. 12

What does lambda function/expression returns ?

Options:

Explanation

Correct answer is : D

Lambda returns expression what it contains
For example lambda a, b: a+b returns the expression a + b

Discuss It

Q. 13

A variable that is defined outside of a function is generally referred as

Options:

Explanation

Correct answer is : A

Generally the global variables are defined outside of the function and can be used by other functions

Discuss It

Q. 14

A variable that is declared inside a function is know as ?

Options:

Explanation

Correct answer is : D

A variable that is declared inside a function is know as local variable and its scope is within the function

Discuss It

Q. 15

What is the output of the following code ?

def foo(x):
    x += [5]
d = [1, 2, 3, 4]
foo(d)
print(len(d))

Options:

Explanation

Correct answer is : B

The arguments are passed to function as reference
Any change to mutable type of object inside function will change the object.
So "d" will become [1, 2, 3, 4, 5] and len(d) will print "5"

Discuss It

Q. 16

What is the output of the following code ?

def foo(p=5, e=6):
    p += e
    e += 1
    print(p, e)
foo(e=8, p=9)

Options:

Explanation

Correct answer is : D

As the values for e and p is passed as keyword arguments, result of p += e will be 17 and e += 1 will be 9

Discuss It

Q. 17

What is the output of the following code ?

def foo(x):
    print(x+2)
x = -5
x = 10
foo(12)

Options:

Explanation

Correct answer is : C

Here print(x+2), the value of x is 12 which is passed by foo(12).
So it prints 12 + 2 = 14

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