Playing as a guest? to earn XP and track your progress.
⚙️ Working with Python Function Quiz
⚙️ Working with Python Function
What keyword is used to define a function in Python?
function
define
def
fun
The 'def' keyword is used to define a function.
What is the output?
def greet():
print('Hello')
print(greet())Hello
Hello None
None Hello
Error
greet() prints 'Hello', and print(greet()) prints the return value which is None.
A variable declared inside a function is called:
Global variable
Local variable
Static variable
Constant
Variables created inside a function are local to that function.
Which of these is a correct function call?
func;
call func()
func()
func[]
Functions are called using parentheses.
What will be the output?
def add(a,b):
return a+b
print(add(3,2))32
3 2
5
Error
The function returns 3+2 = 5.
What happens if a function has no return statement?
It returns 0
It returns None
It returns False
It raises an error
A function without return automatically returns None.
Which is TRUE about parameters and arguments?
Both are same
Parameters are in definition, arguments in call
Arguments are placeholders
Parameters hold actual values
Parameters are placeholders; arguments are actual values passed.
What is the output?
def test(x):
x = x + 1
print(x)
test(5)5
6
x
Error
x becomes 6 inside the function.
Which type of arguments must be passed in correct order?
Keyword arguments
Default arguments
Positional arguments
Arbitrary arguments
Positional arguments depend on order.
What is the output?
def f(a,b=3):
return a*b
print(f(2))6
2
3
Error
b uses default value 3, so 2*3=6.
Default arguments must be placed:
Before required arguments
After required arguments
Anywhere
At top of function
Required parameters must come first.
Output?
def fun(x,y):
print(x,y)
fun(y=2,x=5)5 2
2 5
Error
x y
Keyword arguments ignore order.
Which of these is NOT a valid argument type?
Positional
Keyword
Default
Forced
Python has no 'forced' argument type.
What is returned by functions without a return statement?
0
False
None
Empty string
Default return is None.
Output?
def f():
return 1,2
print(f())1 2
(1, 2)
[1,2]
Error
Multiple return values form a tuple.
What is the execution order inside a function?
From bottom to top
Random
Line by line top to bottom
Depends on Python version
Functions follow sequential execution.
Which statement ends function execution?
break
stop
return
end
return immediately stops the function.
Output?
x=10
def show():
x=5
print(x)
show()
print(x)5 5
10 10
5 10
Error
Local x=5, global x=10 remain unchanged.
Which scope has highest priority?
Global
Local
Built-in
Enclosing
LEGB rule: Local is highest.
Which keyword is used to modify a global variable?
modify
outer
global
public
Use global variable inside function using 'global' keyword.
Output?
def f(a,b):
return a-b
print(f(b=5,a=8))3
-3
8-5
Error
8-5 = 3.
Which call is invalid? Function: def fn(a,b=10,c=20)
fn(1)
fn(1,2)
fn(1,2,3)
fn(1,c=5,b=7)
fn(1) is valid: b=10, c=20 (default).
What is function composition?
Redefining a function
Calling one function inside another
Combining two files
Deleting a function
Composition means calling functions within other functions.
Output?
def square(x):
return x*x
def cube(x):
return square(x)*x
print(cube(3))9
18
27
81
cube = 333 = 27.
Which parameter receives the actual argument?
variable
parameter
placeholder
value holder
Parameter receives the argument.
Output?
def f(x):
return
print(f(3))3
0
None
Error
Empty return returns None.
Which statement is TRUE?
Functions reduce code duplication
Functions make code longer
Functions slow down execution
Functions are optional in Python
Functions promote reusability.
Output?
x=5
def fun():
print(x)
fun()5
Error
x
None
Fun can access global x.
Which belongs to LEGB rule?
Logical
External
Local
Binary
LEGB stands for Local, Enclosing, Global, Built-in.
What is the output?
def f(n):
n*=2
print(n)
x=4
f(x)
print(x)8 8
4 4
8 4
Error
n inside function is local; x outside remains unchanged.
Mutating a list inside a function:
Always changes original list
Creates new list
Never changes original list
Throws an error
Lists are mutable, so changes apply to caller.
Output?
def add(lst):
lst.append(5)
l=[1,2]
add(l)
print(l)[1,2]
[1,2,5]
Error
None
List passed is modified.
Immutable data objects include:
List
Dictionary
String
Set
Strings, ints, tuples are immutable.
Output?
def f(a,b):
return a/b
print(f(9,3))3
3.0
Error
None
/ gives floating value.
Which call is invalid for:
def fun(a,b,c=10): passfun(1,2)
fun(1,2,3)
fun(1)
fun(a=1,b=2)
b is required, so fun(1) is invalid.
Which type of argument assignment is this?
fun(a=3,b=5)Positional
Keyword
Default
Forced
Arguments are passed by keyword.
Output?
def f(*args):
print(args)
f(1,2,3)1 2 3
(1,2,3)
[1,2,3]
Error
*args packs values into tuple.
What is printed?
def f(x):
return x*2
print(f(f(2)))4
8
16
Error
f(2)=4; f(4)=8.
Which is TRUE about return?
return can return only integers
return must be last line always
return exits the function
Functions can have only one return
return stops execution and sends value back.
Output?
def show():
pass
print(show())pass
None
Error
0
show() returns None because no return statement.
What is recursion?
Repeating loops
Function calling itself
Function calling built-in
Stopping execution
Recursion means a function calls itself.
Output?
def f(a,b): return a*b
print(f(a=2,b=3))6
23
Error
None
Using keyword arguments returns 2*3=6.
Which variable can be accessed anywhere in program?
Local
Global
Parameter
Argument
Global variables are accessible throughout program.
Output?
x=10
def f():
print(x)
f()10
0
Error
None
Global x is printed.
Which function header is valid?
def f(x y):
function f(x):
def f(x):
def f x():
Correct syntax uses def f(parameters):
Output?
def f(a,b):
print(a,b)
f(1,b=2)1 2
2 1
Error
None
Combination of positional and keyword.
Which function returns absolute value?
abs()
absolute()
fabs()
mod()
abs(x) returns absolute value.
Output?
def fun(a,b,c):
print(a+b+c)
fun(1,*(2,3))1
6
Error
5
- unpacks tuple into parameters.
Which is allowed?
Two functions with same name
Function inside function
Function without parentheses
Empty function without pass
Nested functions are allowed.
Output?
def f(): return 'Hi'
print(f()+"! ")Hi
Hi!
Hi!
Error
String concatenation gives 'Hi! '.
Which of these is NOT a property of a function?
Reusable
Modular
Slows down program
Organized code
Functions do not slow program; they improve structure.
Output?
def f(a,b=2): return a*b
print(f(3, b=4))6
12
8
Error
3*4 = 12.
Which parameter pack stores keyword arguments?
*args
**kwargs
*var
**args
**kwargs collects keyword arguments into a dictionary.
Output?
p=10
def f():
p=20
return p
print(f(), p)20 20
20 10
10 20
Error
Local variable p=20; global p=10.
Which is TRUE?
Functions cannot return strings
Functions cannot return multiple values
Functions can return lists
Functions must end with return
Functions may return any type including lists.
Output?
def f(): return len('Python')
print(f())5
6
7
Error
'Python' has 6 characters.
What does **kwargs store values as?
List
Tuple
Dictionary
Set
Keyword arguments are stored in a dictionary.
Output?
def f(n):
if n>0:
return 'Positive'
print(f(0))Positive
None
0
Error
No return executed → returns None.
Which describes a correct flow of execution?
Main program → definition → call
Definition → call → execute body
Call → definition → execute reverse
None
Function must be defined, then called, then executed.
Output?
def outer():
x=5
def inner():
return x+5
return inner()
print(outer())5
10
Error
55
inner() uses enclosing scope value x=5 → returns 10.