wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Functions and Modules

Total questions: 10

Worksheet time: 5mins

Name
Class
Date
1.

State the purpose of using args and *kwargs in Python functions?

a)

To specify required arguments

b)

To handle a variable number of arguments

c)

To define default arguments

d)

To restrict the number of arguments passed to a function

2.

In Python, what is the role of a lambda function?

a)
  • To define a function that can be called with any number of arguments

b)
  • To create a function without a name

c)
  • To handle exceptions in a program

d)

To import external modules into a Python script

3.

Which keyword is used to define a default argument in a Python function?

a)

default

b)

def

c)

var

d)

none

4.

What will be the output of the following

from import factorial

print(math.factorial(5))

a)

25

b)

10

c)

120

d)

error bcz factorial is not a module in math

5.
______ is a string literal denoted by triple quotes for providing the specifications of certain program elements.
a)
a) Interface
b)
b) Modularity
c)
c) Client
d)
d) Docstring
6.

Suppose a function called add() is defined in a module called adder.py. Which of the following code snippets correctly show how to import and use the add() function? Select all that apply.

a)

import add from adder

result = add(2, 3)

b)

from adder import add

result = add(2, 3)

c)

from adder import add

result = adder.add(2, 3)

d)

import add

result = adder.add(2, 3)

7.

How can you use a lambda function to sort a list of tuples based on the second element?


my_list = [(1, 3), (3, 2), (2, 1)]


a)
  • sorted(my_list, key=lambda x: x[0])

b)
  • sorted(my_list, key=lambda x: x[1])

c)
  • sorted(my_list, key=lambda x: x[2])

d)
  • sorted(my_list, key=lambda x: x)

8.

Predict the output of the following code

def func(a, b=[]):

b.append(a)

return b

result1 = func(1)

result2 = func(2, [])

result3 = func(3)

print(result1)

print(result2)

print(result3)

a)

[1] [2] [3]

b)

[1, 3] [2] [3]

c)

[1, 3] [2] [1, 3]

d)

[1] [2] [1, 3]

9.

Predict the output of the following code

def add_items(item, items=None):

if items is None:

items = []

items.append(item)

return items

list1 = add_items('apple')

list2 = add_items('banana', [])

list3 = add_items('orange')

print(list1)

print(list2)

print(list3)

a)

['apple', 'orange'] ['banana'] ['apple', 'orange']

b)

['apple', 'orange'] ['banana'] ['orange']

c)

['apple'] ['banana'] ['apple', 'orange']

d)

['apple'] ['banana'] ['orange']

10.

What will be the output of the following code?


def make_incrementor(n):

return lambda x: x + n

f = make_incrementor(2)

g = make_incrementor(6)

print(f(42), g(42))

a)

44 48

b)

44 42

c)

42 48

d)

Error