Python Assignment: Functions & Lambda Expressions
Topic: Lambda, Arguments, Return, Sorted (No Map/Filter/Dicts)
Instructions
• This assignment contains 25 questions testing your understanding of Python functions and
lambda expressions.
• Topics covered: def, lambda, positional arguments, keyword arguments, default arguments,
return, print, and sorted().
• Note: Maps, filters, and dictionaries are intentionally excluded from this specific set.
1 Multiple Choice Questions (MCQ)
Question 1
Difficulty: Easy | Company: Infosys
What is the correct syntax to create a lambda function that adds 5 to a given number x?
A) lambda x -> x + 5
B) lambda x: x + 5
C) lambda x: return x + 5
D) def lambda(x): x + 5
Question 2
Difficulty: Easy | Company: Wipro
What is the output of the following code?
1 func = lambda x : x * 2
2 print ( func (5) )
A) 5
B) 10
C) 25
D) TypeError
1
Python Assignment: Lambda Functions December 12, 2025
Question 3
Difficulty: Medium | Company: Accenture
Which of the following statements about lambda functions is FALSE?
A) Lambda functions can take any number of arguments.
B) Lambda functions can have multiple expressions.
C) Lambda functions return the value of the expression automatically (implicit return).
D) Lambda functions are syntactically restricted to a single expression.
Question 4
Difficulty: Easy | Company: TCS
What will be the output of the following code?
1 ( lambda x , y : x + y ) (3 , 7)
A) A function object
B) 37
C) 10
D) Syntax Error
Question 5
Difficulty: Medium | Company: Amazon
Consider the following code using a default argument. What is the output?
1 power = lambda x , y =2: x ** y
2 print ( power (3) )
A) 9
B) 6
C) TypeError: missing argument
D) 8
Question 6
Difficulty: Medium | Company: Microsoft
What happens if you use a print statement inside a lambda function?
1 check = lambda x : print ( x )
2 result = check ( " Hello " )
3 print ( result )
A) Output: Hello then Hello
B) Output: Hello then None
C) Output: None then Hello
D) Syntax Error
2
Python Assignment: Lambda Functions December 12, 2025
Question 7
Difficulty: Medium | Company: Google
How would you rewrite the following function using lambda?
1 def get_max (a , b ) :
2 if a > b :
3 return a
4 else :
5 return b
A) lambda a, b: a if a > b else b
B) lambda a, b: return a if a > b else b
C) lambda a, b: if a > b: a else: b
D) lambda a, b: max(a, b) return
Question 8
Difficulty: Easy | Company: Cognizant
What is the output of the following code involving keyword arguments?
1 full_name = lambda first , last : first + " " + last
2 print ( full_name ( last = " Stark " , first = " Tony " ) )
A) Stark Tony
B) Tony Stark
C) TypeError
D) last first
Question 9
Difficulty: Hard | Company: Goldman Sachs
Predict the output:
1 x = 10
2 func = lambda y : x + y
3 x = 20
4 print ( func (5) )
A) 15
B) 25
C) Error
D) None
3
Python Assignment: Lambda Functions December 12, 2025
Question 10
Difficulty: Medium | Company: Meta (Facebook)
You want to sort the list points = [(1, 2), (3, 1), (5, 0)] based on the second element of each
tuple. Which is correct?
A) sorted(points, key=lambda x: x[0])
B) sorted(points, key=lambda x: x[1])
C) sorted(points, key=lambda x: x)
D) [Link](key=lambda x: x(1))
Question 11
Difficulty: Medium | Company: IBM
What does the following code print?
1 def my_func ( n ) :
2 return lambda a : a * n
3
4 doubler = my_func (2)
5 print ( doubler (11) )
A) 13
B) 22
C) Function object
D) 11
Question 12
Difficulty: Easy | Company: Capgemini
Which of the following is valid syntax for a lambda accepting zero arguments?
A) lambda: return True
B) lambda: True
C) lambda x=None: True
D) Both B and C
Question 13
Difficulty: Hard | Company: PayPal
What is the result of the following?
1 check = lambda x : ( print ( " Even " ) if x % 2 == 0 else print ( " Odd " ) )
2 result = check (4)
A) Prints "Even", result is "Even"
B) Prints "Even", result is None
C) Prints "Odd", result is None
D) Returns True
4
Python Assignment: Lambda Functions December 12, 2025
Question 14
Difficulty: Medium | Company: Oracle
Sorting a list of strings names = ["Al", "Bo", "Ci"] by the last character:
1 sorted ( names , key = lambda s : s [ -1])
The order will be determined by:
A) A, B, C
B) l, o, i
C) The length of the strings
D) Syntax Error
Question 15
Difficulty: Medium | Company: Adobe
What is the output?
1 z = lambda x , y , z : x + y + z
2 print ( z (1 , z =3 , y =2) )
A) 6
B) 123
C) Error: argument z given multiple times
D) 1
Question 16
Difficulty: Easy | Company: HCL
Can a lambda function contain a return statement explicitly?
A) Yes, always.
B) No, return is implied.
C) Only if it has no arguments.
D) Only in Python 2.7.
Question 17
Difficulty: Medium | Company: Intel
What is the output?
1 L = [ lambda x : x ** 2 , lambda x : x ** 3 , lambda x : x ** 4]
2 print ( L [1](3) )
A) 9
B) 27
C) 81
D) 6
5
Python Assignment: Lambda Functions December 12, 2025
Question 18
Difficulty: Medium | Company: Cisco
What will this return?
1 ( lambda * args : sum ( args ) ) (1 , 2 , 3)
A) 6
B) [1, 2, 3]
C) TypeError
D) (1, 2, 3)
Question 19
Difficulty: Hard | Company: Netflix
You have a custom function apply_op.
1 def apply_op ( val , func ) :
2 return func ( val )
3
4 print ( apply_op (5 , lambda x : x * x ) )
A) 10
B) 25
C) 5
D) func
Question 20
Difficulty: Medium | Company: Uber
Sorting a list of lists data = [[1, 5], [10, 2], [3, 8]]. You want to sort by the sum of elements
in the inner list.
A) sorted(data, key=lambda x: x[0] + x[1])
B) sorted(data, key=sum)
C) sorted(data, key=lambda x: sum(x))
D) All of the above work
2 Coding Questions & Scenarios
Question 21: The Custom Calculator
Difficulty: Medium | Company: Tech Mahindra
Write a function named calculate that takes three arguments:
• a (integer)
6
Python Assignment: Lambda Functions December 12, 2025
• b (integer)
• operation (a function)
The function should return the result of operation(a, b). Task: Call this function using a lambda
expression to find the value of ab (a raised to the power of b).
Question 22: Sorting Tuples
Difficulty: Medium | Company: Flipkart
You are given a list of tuples representing students and their marks:
1 students = [( " Alice " , 85) , ( " Bob " , 70) , ( " Charlie " , 95) , ( " Dave " , 85) ]
Write a single line of code using sorted() and a lambda function to sort this list based on marks in
descending order. If marks are the same, the relative order does not matter (or sort by name if you
prefer).
Question 23: String Manipulation
Difficulty: Medium | Company: Zoho
Write a lambda function assigned to a variable named clean_string. This function should take a
string s, strip whitespace from the beginning and end, and convert it to uppercase. Example Input:
" hello "
Expected Output: "HELLO"
Question 24: Complex Logic
Difficulty: Hard | Company: JP Morgan
Define a lambda function that takes two numbers, x and y.
• Return their sum if their product is greater than 100.
• Return their product otherwise.
Test it with (10, 20) and (5, 5).
Question 25: The Formatting Switch
Difficulty: Hard | Company: Samsung
You are building a report generator. Write a function formatter(data, func) that takes a string
data and a formatting function func. Task:
1. Call formatter with "warning" and a lambda that adds "!!! " to the start and end (e.g., "!!!
warning !!!").
2. Call formatter with "info" and a lambda that encloses it in brackets (e.g., "[info]").
7
Python Assignment: Lambda Functions December 12, 2025
3 Answers
MCQ Answers
1. B - lambda x: x + 5 is the correct syntax.
2. B - 5 × 2 = 10.
3. B - Lambda functions are restricted to a single expression.
4. C - 3 + 7 = 10.
5. A - 32 = 9.
6. B - The inner print prints "Hello", but returns None. The outer print prints that None.
7. A - lambda uses ternary operators (x if C else y) for logic.
8. B - Keyword arguments map to the parameter names regardless of order.
9. B - Lambda looks up x at execution time (late binding), so it sees 20.
10. B - x[1] refers to the second element of the tuple.
11. B - 11 × 2 = 22.
12. D - lambda: True is valid (0 args). x=None is valid default args (0 required).
13. B - It executes the print (side effect "Even") and returns the result of print (None).
14. B - It compares ’l’, ’o’, ’i’.
15. A - 1 + 2 + 3 = 6. Positional and keyword args mixed correctly.
16. B - Return is implied; you cannot write return explicitly.
17. B - Index 1 is the cubic function. 33 = 27.
18. A - *args collects arguments into a tuple, sum() adds them.
19. B - 5 × 5 = 25.
20. D - All three approaches correctly calculate the sum for the key.
Coding Solutions
21. Custom Calculator:
1 def calculate (a , b , operation ) :
2 return operation (a , b )
3
4 result = calculate (2 , 5 , lambda x , y : x ** y )
5 print ( result ) # Output : 32
22. Sorting Tuples:
1 students = [( " Alice " , 85) , ( " Bob " , 70) , ( " Charlie " , 95) , ( " Dave " , 85) ]
2 # Sort by marks ( index 1) in descending order
3 sorted_students = sorted ( students , key = lambda x : x [1] , reverse = True )
4 print ( sorted_students )
23. String Manipulation:
8
Python Assignment: Lambda Functions December 12, 2025
1 clean_string = lambda s : s . strip () . upper ()
2 print ( clean_string ( " hello " ) ) # Output : " HELLO "
24. Complex Logic:
1 logic = lambda x , y : x + y if ( x * y ) > 100 else x * y
2 print ( logic (10 , 20) ) # Product is 200 ( >100) , returns Sum : 30
3 print ( logic (5 , 5) ) # Product is 25 ( <=100) , returns Product : 25
25. The Formatting Switch:
1 def formatter ( data , func ) :
2 return func ( data )
3
4 # Case 1
5 print ( formatter ( " warning " , lambda s : f " !!! { s } !!! " ) )
6
7 # Case 2
8 print ( formatter ( " info " , lambda s : f " [{ s }] " ) )