Q. 1

How are the keyword arguments specified in the function heading or definition?

Options:

Explanation

Correct answer is : C

Keyword arguments are defined as below

def foo(**kwargs):
    pass

Discuss It

Q. 2

What is the output of the following code ?

def foo():
    var += 3
    return var
var = 12
print(foo())

Options:

Explanation

Correct answer is : D

The value global variable can not be changed without explicitly specifying with global keyword.
So the following code will print 15

def foo():
    global var
    var += 3
    return var
var = 12
print(foo())

Discuss It

Q. 3

What is the output of the following code ?

x = 34
def scope_test(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 are passing 34 to scopetest, so print('x == ', x) will print x == 34
  • But Inside the function body we are reassigning x to 2 and as we have not made x to global using global keyword x remain local to the function body.
    So print('X is changed to == ', x) will print X is changed to == 2
  • After the unction call print('x is at outer scope', x) will print 34 as x value is 34 in global scope

Discuss It

Q. 4

What is the output of the following code ?

def printme(msg, count=1):
    print(msg * count)

printme('Welcome')
printme('Pythoneasy', 2)

Options:

Explanation

Correct answer is : A

For function call printme('Welcome'), the value for count is not passed so it defaults to "1". So it will print "Welcome"
For function call printme('Pythoneasy', 2), count value will be overridden to "2" so print(msg * count) will be same as print("Pythoneasy" * 2) and will print "PythoneasyPythoneasy"

Discuss It

Q. 5

Which of the following is correct ?

Options:

Explanation

Correct answer is : D

A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition.
More about doc string -

Discuss It

Q. 6

Where you can define a function ?

Options:

Explanation

Correct answer is : D

A function can be defined anywhere inside a module, function and class

Discuss It

Q. 7

Functions defined inside a class is known as ?

Options:

Explanation

Correct answer is : B

The function that is defined inside a class is called as method

Discuss It

Q. 8

What is the output of the following code ?

def foo(x, y, z): return x + y + z
print(foo(10, 11, 12))

Options:

Explanation

Correct answer is : C

Functions can also be defined in a single line if the function block has one line

Discuss It

Q. 9

What is the output of the following code ?

def printer():
    t = 'to PythonEasy'
    fname = (lambda a: t + ' ' + a)
    return fname
foo = printer()
print(foo('Welcome'))

Options:

Explanation

Correct answer is : C

in this line foo = printer() , function printer returns lambda expression fname = (lambda a: t + ' ' + a) and it gets stored in foo.
When you pass an arrgument to functioon printer , lambda expression fname takes the argument and adds it to "t" value

Discuss It

Q. 10

What is the output of the following code ?

def foo(p=5, *args):
   print(type(args))
foo(5, 6, 7, 8)

Options:

Explanation

Correct answer is : D

All the extra positional arguments will be stored in tuple args except the first argument

Discuss It

Q. 11

What is the output of the following code ?

def rec(a, b):
    if a == 0:
        return b
    else:
        return rec(a - 1, a + b)
print(rec(8, 12))

Options:

Explanation

Correct answer is : A

function rec() is a recursive function which calls it self until a value becomes 0

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