1. Basic Function Syntax
What will be the output of the following code?
def greet(name="Guest"):return f"Hello, {name}!"
print(greet())print(greet("John"))
a. Hello, Guest!, Hello, John!
b. Hello, John!, Hello, Guest!
c. Hello, Guest!, Hello, Guest!
d. Error
2. Positional and Keyword Arguments
What does the following function call print?
def calculate(a, b=5, c=10): return a + b + cprint(calculate(3, c=7))
a. 15
b. 20
c. 25
d. Error
3. Function with Variable Arguments
What will be the output of this code?
def add_all(*args):return sum(args)print(add_all(1, 2, 3, 4))
a. 10
b. [1, 2, 3, 4]
c. Error
d. 1, 2, 3, 4
4. Returning Multiple Values
What will print(result) output?
def divide(a, b):quotient = a // b
remainder = a % b
return quotient, remainder
result = divide(10, 3)
print(result)
a. 10, 3
b. (3, 1)
c. 3.1
d. Error
5. Scope of Variables
What will the following code print?
x = 5def update_value():
x = 10
print(x)
update_value()
print(x)
a. 10, 5
b. 10, 10
c. 5, 5
d. Error
6. Default and Non-Default Arguments
Why does this code throw an error?
def example(a=1, b): return a + b
a. b is not assigned a default value
b. Default arguments must come after non-default arguments
c. Both a and b must have default values
d. No error
7. Lambda Functions
What will the following code print?
double = lambda x: x * 2print(double(4))
a. 2
b. 4
c. 8
d. Error
8. Nested Functions
What will the following code output?
def outer_function(x): def inner_function(y):
return y + 1
return inner_function(x) + 1print(outer_function(5))
a. 6
b. 7
c. 8
d. Error
9. Anonymous Functions with map()
What is the result of the following code?
numbers = [1, 2, 3, 4]result = list(map(lambda x: x ** 2, numbers))print(result)
a. [1, 4, 9, 16]
b. [2, 4, 6, 8]
c. None
d. Error
1. Basic Function Syntax
Answer: a. Hello, Guest!, Hello, John!
Explanation:
- Default value Guest is used when no argument is passed.
- Passing "John" overrides the default value.
2. Positional and Keyword Arguments
Answer: b. 20
Explanation:
- a = 3, b = 5 (default), c = 7 (overrides the default value of 10).
- Result: 3 + 5 + 7 = 20.
3. Function with Variable Arguments
Answer: a. 10
Explanation:
- *args collects all arguments into a tuple.
- sum(args) calculates the sum: 1 + 2 + 3 + 4 = 10.
4. Returning Multiple Values
Answer: b.
(3, 1)
Explanation:
- The function returns a tuple (quotient, remainder).
- 10 // 3 = 3 (quotient), 10 % 3 = 1 (remainder).
5. Scope of Variables
Answer: a. 10, 5
Explanation:
- x = 10 inside the function is local and does not affect the global x.
- Outside the function, x = 5.
6. Default and Non-Default Arguments
Answer: b. Default arguments must come after non-default arguments
Explanation:
- In Python, arguments with default values (like a=1) must appear after those without defaults (like b).
7. Lambda Functions
Answer: c. 8
Explanation:
- The lambda function doubles the input: 4 * 2 = 8.
8. Nested Functions
Answer: b. 7
Explanation:
- inner_function(5) returns 5 + 1 = 6.
- Adding 1 in outer_function: 6 + 1 = 7.
9. Anonymous Functions with map()
Answer: a. [1, 4, 9, 16]
Explanation:
- The lambda function squares each number in the list:
[1^2, 2^2, 3^2, 4^2] = [1, 4, 9, 16].