0
0
Pythonprogramming~20 mins

Function definition and syntax in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Function Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of a function with default and keyword arguments
What is the output of this Python code?
Python
def greet(name, greeting="Hello"):
    return f"{greeting}, {name}!"

print(greet("Alice"))
print(greet("Bob", greeting="Hi"))
AHello, Alice!\nHello, Bob!
BHello, Alice!\nHi, Bob!
CHi, Alice!\nHi, Bob!
DError: missing required positional argument
Attempts:
2 left
๐Ÿ’ก Hint
Check how default arguments work and how keyword arguments override them.
โ“ Predict Output
intermediate
2:00remaining
Return value of a function with multiple return statements
What is the output of this code?
Python
def check_number(x):
    if x > 0:
        return "Positive"
    elif x == 0:
        return "Zero"
    else:
        return "Negative"

print(check_number(-5))
ANegative
BZero
CPositive
DNone
Attempts:
2 left
๐Ÿ’ก Hint
Look at the input value and which condition it satisfies.
๐Ÿ“ Syntax
advanced
2:00remaining
Identify the syntax error in function definition
Which option contains a syntax error in the function definition?
Adef add(a, b):\n return a + b
Bdef divide(a, b):\n return a / b
Cdef multiply(a, b)\n return a * b
Ddef subtract(a, b):\n return a - b
Attempts:
2 left
๐Ÿ’ก Hint
Check for missing colons in function headers.
โ“ Predict Output
advanced
2:00remaining
Output of a function using *args and **kwargs
What is the output of this code?
Python
def info(*args, **kwargs):
    return f"Args: {args}, Kwargs: {kwargs}"

print(info(1, 2, 3, name='Alice', age=30))
AArgs: (1, 2, 3), Kwargs: {'name': 'Alice', 'age': 30}
BArgs: [1, 2, 3], Kwargs: {'name': 'Alice', 'age': 30}
CArgs: (1, 2, 3), Kwargs: [('name', 'Alice'), ('age', 30)]
DArgs: 1, 2, 3, Kwargs: name='Alice', age=30
Attempts:
2 left
๐Ÿ’ก Hint
Remember that *args collects positional arguments as a tuple and **kwargs collects keyword arguments as a dictionary.
๐Ÿง  Conceptual
expert
3:00remaining
Effect of mutable default arguments in function definitions
What will be the output after running this code?
Python
def append_item(item, lst=[]):
    lst.append(item)
    return lst

print(append_item(1))
print(append_item(2))
A[1]\n[2]
BError: mutable default argument
C[1]\n[1]
D[1]\n[1, 2]
Attempts:
2 left
๐Ÿ’ก Hint
Think about how default mutable arguments behave across multiple function calls.