NEW
Font size
WorksheetsPython Functions Quiz
Total questions: 45
Worksheet time: 23mins
What is the correct way to define a function in Python?
function myfunc():
def myfunc():
func myfunc():
define myfunc():
What will the following code print? def add(x, y=5): return x + y print(add(10))
15
10
5
Error
Which keyword is used to exit a function and return a value?
stop
exit
return
break
What is the default return value of a Python function if no return statement is specified?
0
None
False
Empty string
Which of the following is a valid function call?
myfunc[]
myfunc{}
myfunc()
myfunc<>
What will the following code output? def func(a, b=2, c=3): return a + b + c print(func(4, c=5))
11
12
14
Error
What will this function return? def f(x, y): pass print(f(2, 3))
0
None
2
Error
Which of these functions is built-in in Python?
sqrt()
print()
append()
main()
Which of these is NOT a valid function name?
_func
func1
1func
func_name
What will the output be? def calc(a, b):
return a * b
print(calc(2, 'Hi'))
HiHi
HiHiHiHi
2Hi
Error
Can a Python function return multiple values?
No
Yes, as a tuple
Only one
Only lists
Which type of arguments must be passed after keyword arguments?
Positional arguments
Default arguments
None
All of them
Which function is used to get user input?
read()
scan()
input()
enter()
What is the scope of a variable declared inside a function?
Global
Local
Module-wide
Class-only
What keyword is used to create an anonymous function?
lambda
def
func
anon
What will this code print? def f(x=[]): x.append(1) return x print(f(), f())
[1] [1]
[1] [1, 1]
[1, 1] [1, 1]
Error
A function that calls itself is known as:
Inline function
Recursive function
Lambda function
Generator
Which statement about default arguments is TRUE?
They must come after non-default arguments
They can come before positional arguments
They are mandatory
They cannot be used in Python
What is the keyword to specify function annotations?
:
->
@
#
Which of these creates a list in Python?
{1,2,3}
(1,2,3)
[1,2,3]
<1,2,3>
Lists in Python are:
Immutable
Mutable
Fixed-size
Constant
What will be the output? lst = [1, 2, 3] print(lst[1])
1
2
3
Error
Which method adds an item at the end of a list?
add()
insert()
append()
extend()
What is the output? lst = [1,2,3] lst.append([4,5]) print(lst)
[1,2,3,4,5]
[1,2,3,[4,5]]
[1,2,3,4,[5]]
Error
Which method is used to remove and return the last element?
remove()
del
pop()
discard()
What is the result of len([1, [2,3], 4])?
2
3
4
Error
What does list(range(3)) return?
[1,2,3]
[0,1,2]
[0,1,2,3]
Error
Which operator is used to concatenate lists?
+
*
&
concat
What will be the output? lst = [10,20,30] print(lst[::-1])
[10,20,30]
[30,20,10]
[20,30,10]
Error
Which method removes all items from a list?
delete()
clear()
remove()
pop()
Which method is used to add multiple elements at once?
extend()
append()
insert()
add()
What will this print? lst = [1,2,3,4] print(lst[1:3])
[1,2,3]
[2,3]
[2,3,4]
[1,2]
What happens when you do lst * 2 where lst = [1,2]?
[1,2,1,2]
[2,4]
Error
[1,2,2]
Which method inserts an element at a specific index?
add()
insert()
append()
extend()
Which of the following is TRUE about lists?
They allow duplicates
They are immutable
They are unordered
They cannot store different data types
Which statement deletes the element at index 2?
list.remove(2)
del list[2]
list.pop()
list.delete(2)
What will this print? lst = [1,2,3] print(max(lst))
1
2
3
Error
Which function sorts a list permanently?
sort()
sorted()
order()
arrange()
Which function returns a sorted copy of a list without modifying it?
sort()
sorted()
copy()
reverse()
What is the output? lst = [1,2,3] lst.remove(2) print(lst)
[1,3]
[2,3]
[1,2]
Error
What will be the output? lst = [1,2,3] print(sum(lst))
5
6
123
Error
Which method reverses a list in place?
reversed()
reverse()
[::-1]
invert()
What is the output of:
def f(x, y=5, z=10):
print(x, y, z)
f(1, z=20)
1 5 20
1 20 10
Error
1 10 20
What is the output?
nums = [3, 1, 2]
print(sorted(nums), nums)
[1, 2, 3] [3, 1, 2]
[1, 2, 3] [1, 2, 3]
Error
[3, 1, 2] [1, 2, 3]
What is the output?
x = [1, 2, 3]
y = x
y.reverse()
print(x, y)
[3, 2, 1] [1, 2, 3]
[1, 2, 3] [3, 2, 1]
[3, 2, 1] [3, 2, 1]
Error
