Brainstorming 08: Python Functions: Error Analysis, Output Prediction & Function Design

Easy Level (10 Questions)

Error-Based (3 Questions)


  1.  Identify and correct the error in the following function:

    def greet(name): print("Hello" + name) greet()
  2.  What will be the output of the following code? If there is an error, explain why.


    def square(num): return num ** 2 print(square())
  3.  Find the error in the function and fix it:


    def add(a, b): return a + b print(add(2))

Output-Based (3 Questions)

  1.  Predict the output:


    def func(a, b=5): return a * b print(func(2)) print(func(2, 3))
  2.  What will be the output?


    def message(name="User"): return "Hello " + name print(message("Alice")) print(message())
  3.  Predict the output:


    def my_func(val): val += 5 return val num = 10 print(my_func(num)) print(num)

Function Design-Based (4 Questions)

  1.   Write a function that takes an integer as input and returns whether it is positive, negative, or zero.

  2.   Write a function that takes a string as input and returns its length.

  3.   Create a function that accepts a list of numbers and returns the sum of even numbers.

  4.  Define a function that takes a tuple of numbers and returns the largest number.


Medium Level (6 Questions)

Error-Based (2 Questions)

  1.  Identify and correct the error :


def greet(name="Guest", msg): print(f"{msg}, {name}") greet("Hello")
  1.  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)

  1.  Predict the output:


def modify_list(lst): lst.append(100) return lst nums = [10, 20, 30] print(modify_list(nums)) print(nums)
  1.  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)

  1.  Write a function that accepts a dictionary and returns a new dictionary with keys and values swapped.

  2.  Write a function that takes a tuple of integers and returns the sum of all elements.



Hard Level (4 Questions)

Error-Based (1 Question)

  1.  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)

  1.  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)

  1.  Write a recursive function to compute the sum of digits of a given number.

  2.  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.

Previous Post Next Post