MCQs on Functions and Scripts | MATLAB

Creating and Calling Functions (10 Questions)

  1. What is the purpose of defining a function in programming?
    • A) To execute a set of instructions
    • B) To declare variables
    • C) To organize code into reusable blocks
    • D) To create loops
  2. How do you call a function in Python?
    • A) function_name[]
    • B) call function_name
    • C) function_name()
    • D) function_name{}
  3. Which of the following is the correct syntax for defining a function in Python?
    • A) def function_name{}
    • B) function function_name():
    • C) def function_name():
    • D) function_name() =>
  4. What is the primary advantage of using functions in programming?
    • A) Increase memory usage
    • B) Reuse code and enhance readability
    • C) Decrease program execution speed
    • D) Increase complexity
  5. What is returned by a function if there is no return statement?
    • A) None
    • B) 0
    • C) False
    • D) An error
  6. Which of these options is used to pass parameters to a function?
    • A) Parentheses ()
    • B) Curly braces {}
    • C) Square brackets []
    • D) Angle brackets <>
  7. What will be the output of this function call: my_function(3) if def my_function(a): return a * 2?
    • A) 3
    • B) 6
    • C) None
    • D) Error
  8. What is a function signature?
    • A) The function’s name and return type
    • B) The function’s parameters and body
    • C) The function’s name and parameters
    • D) The function’s body and return statement
  9. What is the result of calling a function without passing any arguments when the function requires one argument?
    • A) The program crashes
    • B) The default value of the argument is used (if defined)
    • C) It results in a syntax error
    • D) None of the above
  10. In the context of functions, what does the term “scope” refer to?
    • A) The range of values a function can return
    • B) The area where a function can be called and accessed
    • C) The time a function takes to execute
    • D) The space where variables are defined

Function Inputs and Outputs (10 Questions)

  1. What is an input parameter in a function?
    • A) A value the function computes
    • B) A variable defined inside the function
    • C) A value passed to the function when called
    • D) A constant value defined within the program
  2. What keyword is used to return a value from a function in Python?
    • A) yield
    • B) return
    • C) output
    • D) send
  3. Which of the following is true about return statements in Python functions?
    • A) A function can have multiple return statements
    • B) A function must always have a return statement
    • C) A return statement can only return one value
    • D) None of the above
  4. What happens if you forget to include a return statement in a Python function?
    • A) It raises a syntax error
    • B) It returns None by default
    • C) The function execution is aborted
    • D) It returns a random value
  5. How would you call a function that takes multiple parameters and returns a value?
    • A) function(param1, param2)
    • B) function(param1 + param2)
    • C) function(param1, param2) and store result
    • D) None of the above
  6. What is the result of def add(x, y=5): return x + y if called as add(3)?
    • A) 3
    • B) 5
    • C) 8
    • D) Error
  7. How can functions be used to process output in programming?
    • A) By performing calculations
    • B) By returning the result of computations
    • C) By modifying global variables
    • D) All of the above
  8. Which of these is a method for passing data into a function?
    • A) Return statement
    • B) Variables
    • C) Arguments
    • D) Data manipulation
  9. How can you pass multiple arguments into a function in Python?
    • A) By separating them with commas inside parentheses
    • B) By passing them in a list
    • C) By passing them in a string
    • D) Both A and B
  10. Can a function return multiple values in Python?
    • A) Yes, by returning a tuple
    • B) No, functions can only return one value
    • C) Yes, by returning a list
    • D) Yes, by returning an array

Anonymous Functions (5 Questions)

  1. What is the syntax for creating an anonymous function in Python?
    • A) lambda function_name():
    • B) def function_name():
    • C) lambda x: expression
    • D) func(x): return expression
  2. Which of the following is true about anonymous functions in Python?
    • A) They are named functions
    • B) They can only have one expression
    • C) They are used for large computations
    • D) They can return multiple values
  3. What is an advantage of using anonymous functions?
    • A) They simplify code for small tasks
    • B) They can store multiple functions
    • C) They increase program speed significantly
    • D) They always return values
  4. Which function is an example of using an anonymous function in Python?
    • A) map(lambda x: x * 2, [1, 2, 3])
    • B) def multiply(x): return x * 2
    • C) print(lambda x: x * 2)
    • D) None of the above
  5. Can an anonymous function accept multiple parameters in Python?
    • A) Yes, by separating parameters with commas
    • B) No, they can only accept one parameter
    • C) Yes, but they return only one result
    • D) No, they don’t accept parameters

Modular Programming Basics (5 Questions)

  1. What is modular programming?
    • A) Writing programs in a linear fashion
    • B) Organizing a program into separate, reusable modules
    • C) Writing one large function to handle all tasks
    • D) Using loops to repeat tasks
  2. How do you reuse code in modular programming?
    • A) By calling functions from other modules
    • B) By writing code in a single file
    • C) By avoiding the use of variables
    • D) By avoiding function definitions
  3. Why is modular programming beneficial?
    • A) It increases code complexity
    • B) It allows for easier debugging and maintenance
    • C) It reduces code efficiency
    • D) It eliminates the need for functions
  4. Which of the following is a key concept of modular programming?
    • A) Large monolithic programs
    • B) Independent modules that can be developed and tested separately
    • C) Avoiding code reuse
    • D) Functions that always return values
  5. How does modular programming improve collaboration?
    • A) It forces all developers to use the same code
    • B) It allows developers to work on separate modules simultaneously
    • C) It requires fewer functions
    • D) It eliminates the need for testing

Answers

QnoAnswer (Option with the text)
1C) To organize code into reusable blocks
2C) function_name()
3C) def function_name():
4B) Reuse code and enhance readability
5A) None
6A) Parentheses ()
7B) 6
8C) The function’s name and parameters
9B) The default value of the argument is used (if defined)
10B) The area where a function can be called and accessed
11C) A value passed to the function when called
12B) return
13A) A function can have multiple return statements
14B) It returns None by default
15C) function(param1, param2) and store result
16C) 8
17D) All of the above
18C) Arguments
19D) Both A and B
20A) Yes, by returning a tuple
21C) lambda x: expression
22B) They can only have one expression
23A) They simplify code for small tasks
24A) map(lambda x: x * 2, [1, 2, 3])
25A) Yes, by separating parameters with commas
26B) Organizing a program into separate, reusable modules
27A) By calling functions from other modules
28B) It allows for easier debugging and maintenance
29B) Independent modules that can be developed and tested separately
30B) It allows developers to work on separate modules simultaneously

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top