Easy Level (10 Questions)
Error-Based (3 Questions)
Identify and correct the error in the following function:
def greet(name): print("Hello" + name) greet()
-
What will be the output of the following code? If there is an error, explain why.
def square(num): return num ** 2 print(square())
-
Find the error in the function and fix it:
def add(a, b): return a + b print(add(2))
Output-Based (3 Questions)
-
Predict the output:
def func(a, b=5): return a * b print(func(2)) print(func(2, 3))
-
What will be the output?
def message(name="User"): return "Hello " + name print(message("Alice")) print(message())
-
Predict the output:
def my_func(val): val += 5 return val num = 10 print(my_func(num)) print(num)
Function Design-Based (4 Questions)
-
Write a function that takes an integer as input and returns whether it is positive, negative, or zero.
-
Write a function that takes a string as input and returns its length.
-
Create a function that accepts a list of numbers and returns the sum of even numbers.
-
Define a function that takes a tuple of numbers and returns the largest number.
Medium Level (6 Questions)
Error-Based (2 Questions)
-
Identify and correct the error :
def greet(name="Guest", msg):
print(f"{msg}, {name}")
greet("Hello")
-
What will be the output? Explain any errors.
def multiply(a, b=2, c):
return a*b*c
print(multiply(3, 4))
Output-Based (2 Questions)
-
Predict the output:
def modify_list(lst):
lst.append(100)
return lst
nums = [10, 20, 30]
print(modify_list(nums))
print(nums)
-
Predict the output:
def update_dict(d):
d["new"] = 50
return d
data = {"a": 10, "b": 20}
print(update_dict(data))
print(data)
Function Design-Based (2 Questions)
-
Write a function that accepts a dictionary and returns a new dictionary with keys and values swapped.
-
Write a function that takes a tuple of integers and returns the sum of all elements.
Hard Level (4 Questions)
Error-Based (1 Question)
-
Identify and fix the error in the following function:
def calculate(a, b=2, c=3, d):
return a + b + c + d
print(calculate(1, 2, 3, 4))
Output-Based (1 Question)
-
Predict the output:
def test(a, L=[]):
L.append(a)
return L
print(test(1))
print(test(2))
print(test(3))
Function Design-Based (2 Questions)
-
Write a recursive function to compute the sum of digits of a given number.
-
Write a function that takes a list of tuples where each tuple contains a name and age, and returns a dictionary where names are keys and ages are values.