PYTHON Function and Recursion MCQs

  1. Which keyword is used to define a function in Python?
    a) func
    b) define
    c) def
    d) function
    View Answer

    Correct answer: C — def

  2. What will be printed?
    1 def greet():
    2 return "Hello Eduinq"
    3 print(greet())
    a) Hello Eduinq
    b) greet
    c) Error
    d) None
    View Answer

    Correct answer: A — Hello Eduinq

  3. Which of the following is used to call a function?
    a) function_name()
    b) call function_name
    c) run function_name
    d) exec function_name
    View Answer

    Correct answer: A — function_name()

  4. What is the default return value of a function if no return statement is given?
    a) None
    b) 0
    c) Empty string
    d) Error
    View Answer

    Correct answer: A — None

  5. Which of the following is used to define anonymous functions?
    a) def
    b) lambda
    c) func
    d) anon
    View Answer

    Correct answer: B — lambda

  6. What will be printed?
    1 def add(a,b):
    2 return a+b
    3 print(add(2,3))
    a) 5
    b) 23
    c) Error
    d) None
    View Answer

    Correct answer: A — 5

  7. Which of the following is used to pass variable number of arguments?
    a) *args
    b) **kwargs
    c) Both A and B
    d) None
    View Answer

    Correct answer: C — Both A and B

  8. What is the output of print(type(lambda x: x+1))?
    a) <class 'function'>
    b) <class 'lambda'>
    c) <class 'object'>
    d) <class 'method'>
    View Answer

    Correct answer: A — <class 'function'>

  9. Which of the following is used to define recursion?
    a) Function calling itself
    b) Function calling other function
    c) Function without return
    d) Function without arguments
    View Answer

    Correct answer: A — Function calling itself

  10. What will be printed?
    1 def fact(n):
    2 if n==0:
    3 return 1
    4 else:
    5 return n*fact(n-1)
    6 print(fact(3))
    a) 6
    b) 3
    c) Error
    d) None
    View Answer

    Correct answer: A — 6

  11. Which of the following is used to define default arguments?
    a) def func(a=10)
    b) def func(a:10)
    c) def func(a==10)
    d) def func(a->10)
    View Answer

    Correct answer: A — def func(a=10)

  12. What is the output of print(len([lambda x: x**2 for x in range(3)]))?
    a) 2
    b) 3
    c) 4
    d) Error
    View Answer

    Correct answer: B — 3

  13. Which of the following is used to define keyword arguments?
    a) func(a=10)
    b) func(10=a)
    c) func(a:10)
    d) func(a->10)
    View Answer

    Correct answer: A — func(a=10)

  14. What will be printed?
    1 def greet(name="Eduinq"):
    2 return "Hello " + name
    3 print(greet())
    a) Hello Eduinq
    b) Hello name
    c) Error
    d) None
    View Answer

    Correct answer: A — Hello Eduinq

  15. Which of the following is used to define nested functions?
    a) Function inside function
    b) Function inside class
    c) Function inside loop
    d) Function inside module
    View Answer

    Correct answer: A — Function inside function

  16. What is the output of print((lambda x,y: x*y)(2,3))?
    a) 6
    b) 5
    c) Error
    d) None
    View Answer

    Correct answer: A — 6

  17. Which of the following is used to define docstring?
    a) """string"""
    b) #string
    c) 'string'
    d) //string
    View Answer

    Correct answer: A — """string"""

  18. What will be printed?
    1 def square(x):
    2 """Eduinq function"""
    3 return x*x
    4 print(square.__doc__)
    a) Eduinq function
    b) square
    c) Error
    d) None
    View Answer

    Correct answer: A — Eduinq function

  19. Which of the following is used to return multiple values?
    a) Tuple
    b) List
    c) Dictionary
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  20. What is the output of print((lambda x: x+10)(5))?
    a) 15
    b) 10
    c) 5
    d) Error
    View Answer

    Correct answer: A — 15

  21. Which of the following is used to define function scope?
    a) Local
    b) Global
    c) Enclosed
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  22. What will be printed?
    1 x = 10
    2 def func():
    3 global x
    4 x = 20
    5 func()
    6 print(x)
    a) 10
    b) 20
    c) Error
    d) None
    View Answer

    Correct answer: B — 20

  23. Which of the following is used to define function with keyword arguments?
    a) def func(**kwargs)
    b) def func(*args)
    c) def func(args)
    d) def func(kwargs)
    View Answer

    Correct answer: A — def func(**kwargs)

  24. What is the output of print((lambda x: x**2)(4))?
    a) 16
    b) 8
    c) Error
    d) None
    View Answer

    Correct answer: A — 16

  25. Which of the following is used to define recursive factorial function?
    a) def fact(n): return n*fact(n-1)
    b) def fact(n): return n+fact(n-1)
    c) def fact(n): return n-fact(n-1)
    d) def fact(n): return n/fact(n-1)
    View Answer

    Correct answer: A — def fact(n): return n*fact(n-1)

  26. What will be printed?
    1 def func(a,b=5):
    2 return a+b
    3 print(func(3))
    a) 8
    b) 5
    c) Error
    d) None
    View Answer

    Correct answer: A — 8

  27. Which of the following is used to define function with variable arguments?
    a) def func(*args)
    b) def func(**kwargs)
    c) Both A and B
    d) None
    View Answer

    Correct answer: C — Both A and B

  28. What is the output of print((lambda x,y: x+y)(2,3))?
    a) 5
    b) 6
    c) Error
    d) None
    View Answer

    Correct answer: A — 5

  29. Which of the following is used to define recursive Fibonacci function?
    a) def fib(n): return fib(n-1)+fib(n-2)
    b) def fib(n): return fib(n-1)-fib(n-2)
    c) def fib(n): return fib(n-1)*fib(n-2)
    d) def fib(n): return fib(n-1)/fib(n-2)
    View Answer

    Correct answer: A — def fib(n): return fib(n-1)+fib(n-2)

  30. What will be printed?
    1 def fib(n):
    2 if n<=1:
    3 return n
    4 else:
    5 return fib(n-1)+fib(n-2)
    6 print(fib(5))
    a) 5
    b) 8
    c) Error
    d) None
    View Answer

    Correct answer: A — 5

  31. Which of the following is used to define function with default keyword arguments?
    a) def func(a=10,b=20)
    b) def func(a:10,b:20)
    c) def func(a==10,b==20)
    d) def func(a->10,b->20)
    View Answer

    Correct answer: A — def func(a=10,b=20)

  32. What is the output of print((lambda x: x*2)(5))?
    a) 10
    b) 5
    c) Error
    d) None
    View Answer

    Correct answer: A — 10

  33. Which of the following is used to define recursive sum function?
    a) def sum(n): return n+sum(n-1)
    b) def sum(n): return n-sum(n-1)
    c) def sum(n): return n*sum(n-1)
    d) def sum(n): return n/sum(n-1)
    View Answer

    Correct answer: A — def sum(n): return n+sum(n-1)

  34. What will be printed?
    1 def sum(n):
    2 if n==0:
    3 return 0
    4 else:
    5 return n+sum(n-1)
    6 print(sum(3))
    a) 6
    b) 3
    c) Error
    d) None
    View Answer

    Correct answer: A — 6

  35. Which of the following is used to define recursive power function?
    a) def power(n): return n*power(n-1)
    b) def power(n): return n**power(n-1)
    c) def power(n): return n/power(n-1)
    d) def power(n): return n+power(n-1)
    View Answer

    Correct answer: A — def power(n): return n*power(n-1)

  36. What will be printed?
    1 def power(n):
    2 if n==0:
    3 return 1
    4 else:
    5 return 2*power(n-1)
    6 print(power(3))
    a) 8
    b) 6
    c) Error
    d) None
    View Answer

    Correct answer: A — 8

  37. Which of the following is used to define recursive string reversal?
    a) def rev(s): return rev(s[1:])+s[0]
    b) def rev(s): return rev(s[0])+s[1:]
    c) def rev(s): return s[::-1]
    d) def rev(s): return s
    View Answer

    Correct answer: A — def rev(s): return rev(s[1:])+s[0]

  38. What will be printed?
    1 def rev(s):
    2 if len(s)==0:
    3 return s
    4 else:
    5 return rev(s[1:])+s[0]
    6 print(rev("Eduinq"))
    a) qniudE
    b) Eduinq
    c) Error
    d) None
    View Answer

    Correct answer: A — qniudE

  39. Which of the following is used to define recursive sum of digits?
    a) def sumd(n): return n%10+sumd(n//10)
    b) def sumd(n): return n+sumd(n-1)
    c) def sumd(n): return n*sumd(n-1)
    d) def sumd(n): return n/sumd(n-1)
    View Answer

    Correct answer: A — def sumd(n): return n%10+sumd(n//10)

  40. What will be printed?
    1 def sumd(n):
    2 if n==0:
    3 return 0
    4 else:
    5 return n%10+sumd(n//10)
    6 print(sumd(123))
    a) 6
    b) 123
    c) Error
    d) None
    View Answer

    Correct answer: A — 6

  41. Which of the following is used to define recursive length of string?
    a) def length(s): return 1+length(s[1:])
    b) def length(s): return len(s)
    c) def length(s): return s[::-1]
    d) def length(s): return s
    View Answer

    Correct answer: A — def length(s): return 1+length(s[1:])

  42. What will be printed?
    1 def length(s):
    2 if s=="":
    3 return 0
    4 else:
    5 return 1+length(s[1:])
    6 print(length("Eduinq"))
    a) 6
    b) 7
    c) Error
    d) None
    View Answer

    Correct answer: A — 6

  43. Which of the following is used to define recursive maximum in list?
    a) def maxlist(l): return max(l[0],maxlist(l[1:]))
    b) def maxlist(l): return min(l[0],maxlist(l[1:]))
    c) def maxlist(l): return sum(l)
    d) def maxlist(l): return len(l)
    View Answer

    Correct answer: A — def maxlist(l): return max(l[0],maxlist(l[1:]))

  44. What will be printed?
    1 def maxlist(l):
    2 if len(l)==1:
    3 return l[0]
    4 else:
    5 return max(l[0],maxlist(l[1:]))
    6 print(maxlist([1,5,3]))
    a) 5
    b) 3
    c) 1
    d) Error
    View Answer

    Correct answer: A — 5

  45. Which of the following is used to define recursive minimum in list?
    a) def minlist(l): return min(l[0],minlist(l[1:]))
    b) def minlist(l): return max(l[0],minlist(l[1:]))
    c) def minlist(l): return sum(l)
    d) def minlist(l): return len(l)
    View Answer

    Correct answer: A — def minlist(l): return min(l[0],minlist(l[1:]))

  46. What will be printed?
    1 def minlist(l):
    2 if len(l)==1:
    3 return l[0]
    4 else:
    5 return min(l[0],minlist(l[1:]))
    6 print(minlist([1,5,3]))
    a) 1
    b) 3
    c) 5
    d) Error
    View Answer

    Correct answer: A — 1

  47. Which of the following is used to define recursive palindrome check?
    a) def pal(s): return s==s[::-1]
    b) def pal(s): return pal(s[1:-1])
    c) def pal(s): return s==s
    d) def pal(s): return s!=s[::-1]
    View Answer

    Correct answer: A — def pal(s): return s==s[::-1]

  48. What will be printed?
    1 def pal(s):
    2 return s==s[::-1]
    3 print(pal("Eduinq"))
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: B — False

  49. Which of the following is used to define recursive binary search?
    a) def bsearch(l,x): return bsearch(l[:mid],x) or bsearch(l[mid:],x)
    b) def bsearch(l,x): return x in l
    c) def bsearch(l,x): return l.index(x)
    d) def bsearch(l,x): return len(l)
    View Answer

    Correct answer: A — def bsearch(l,x): return bsearch(l[:mid],x) or bsearch(l[mid:],x)

  50. What will be printed?
    1 def bsearch(l,x):
    2 if len(l)==0:
    3 return False
    4 mid=len(l)//2
    5 if l[mid]==x:
    6 return True
    7 elif l[mid]>x:
    8 return bsearch(l[:mid],x)
    9 else:
    10 return bsearch(l[mid+1:],x)
    11 print(bsearch([1,2,3,4,5],3))
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

Quick Links to Explore