CBSERanker

Loading

Worksheet #24090602 Functions in Python: Types, User-Defined Functions, Arguments, Parameters, and Scope.

Worksheet #24090602 Functions in Python: Types, User-Defined Functions, Arguments, Parameters, and Scope.

Multiple Choice Questions (MCQs)

  1. Which of the following is a built-in function in Python?
    • a) print()
    • b) my_function()
    • c) calculate()
    • d) run_code()
  2. What is a user-defined function in Python?
    • a) A function available in the Python standard library
    • b) A function defined by the user using the def keyword
    • c) A function that cannot be modified by the user
    • d) A function that Python defines automatically
  3. Which of the following correctly defines a function in Python?
    • a) function myFunc() {}
    • b) def myFunc():
    • c) myFunc def():
    • d) def myFunc:
  4. What is the correct syntax for a function with default parameters?
    • a) def func(x=10, y):
    • b) def func(x, y=20):
    • c) def func(x=10, y=20):
    • d) def func(y=20, x):
  5. What does a function return if there is no return statement?
    • a) None
    • b) 0
    • c) The last value computed
    • d) An error message
  6. What is the local scope of a variable?
    • a) The variable is accessible throughout the entire program
    • b) The variable is accessible only inside the function where it is defined
    • c) The variable is stored permanently
    • d) The variable is defined outside any function
  7. What is the correct way to call a function named myFunc with arguments 10 and 20?
    • a) myFunc(10, 20)
    • b) myFunc x=10, y=20
    • c) myFunc(10; 20)
    • d) myFunc(x: 10, y: 20)
  8. Which of the following is true about positional parameters in Python functions?
    • a) They can only be integers
    • b) They must be provided in the order the function defines them
    • c) They are always optional
    • d) They can be placed in any order when calling a function
    • What will the following code print?
      def testFunc(x, y=5):
           return x + y
               print(testFunc(10))
    • a) 5
    • b) 15
    • c) 10
    • d) Error
  9. Which keyword is used to specify a global variable inside a function?
    • a) global
    • b) nonlocal
    • c) local
    • d) var

Programming Questions

  1. Write a Python program to create a user-defined function greet() that takes a name as a parameter and prints “Hello, [name]!”
  2. Define a function add_numbers() that takes two numbers as positional arguments and returns their sum.
  3. Create a function find_max() that accepts three numbers as parameters and returns the largest number among them.
  4. Write a program that defines a function with default parameters and prints different results when called with and without providing arguments.
  5. Define a function calculate_area() that takes length and width as parameters and returns the area of a rectangle.
  6. Create a program that demonstrates the use of a global variable inside a function.
  7. Write a Python program to demonstrate the flow of execution inside nested functions (functions within functions).
  8. Write a function factorial(n) that returns the factorial of a given number n. Demonstrate calling this function with an argument.
  9. Define a function that takes a list of numbers and returns the sum of the numbers. Demonstrate the use of both positional and keyword arguments when calling the function.
  10. Write a Python program to demonstrate the difference between local and global scope by defining and printing variables inside and outside a function.

Leave a Reply

Your email address will not be published. Required fields are marked *