MATLAB Functions and Flow Control 5 — Questions and Answers
Question 1: What is a recursive function in MATLAB?
- A function that uses a for loop
- A function that calls itself (Correct answer)
- A function stored in a private folder
- A function with no return value
Correct answer: A function that calls itself
A recursive function calls itself within its own body, typically with a simpler input, until a base case is reached.
Question 2: Which of the following correctly implements factorial recursively in MATLAB?
- function n = fact(n); if n>1; n = n * fact(n-1); end; end (Correct answer)
- function n = fact(n); n = n * fact(n+1); end
- function n = fact(n); for k=1:n; n=k; end; end
- function n = fact(n); n = factorial(n-1); end
Correct answer: function n = fact(n); if n>1; n = n * fact(n-1); end; end
The correct recursive factorial checks for the base case (n<=1 returns 1) and multiplies n by fact(n-1).
Question 3: What is a local function in MATLAB?
- A function defined inside a loop
- A helper function defined after the main function in the same .m file (Correct answer)
- A function visible to all scripts on the path
- An anonymous function stored in a variable
Correct answer: A helper function defined after the main function in the same .m file
Local functions are additional functions defined at the bottom of a script or function file and are only accessible within that file.
Question 4: Which MATLAB statement evaluates a block of code only if all previous `elseif` and `if` conditions were false?
- otherwise
- default
- else (Correct answer)
- catch
Correct answer: else
The `else` clause runs when no preceding `if` or `elseif` condition evaluated to true.
Question 5: What is `cellfun` used for in MATLAB?
- Apply a function to each element of a cell array (Correct answer)
- Create a cell array from a function output
- Define a function that returns cells
- Convert a function handle to a cell
Correct answer: Apply a function to each element of a cell array
`cellfun(func, C)` applies the function `func` to each element of cell array `C` and returns the results.
Question 6: In MATLAB, what happens if a `switch` expression matches multiple `case` values listed in a cell array like `case {1, 2}`?
- Only the first match executes
- Both matching cases execute independently
- The case block executes if the expression equals any value in the cell array (Correct answer)
- MATLAB throws an error
Correct answer: The case block executes if the expression equals any value in the cell array
When a case uses a cell array, MATLAB executes that block if the switch expression matches any value in the cell array.
Question 7: What is the key difference between `break` and `return` in MATLAB?
- `break` exits the script; `return` exits a loop
- `break` exits the innermost loop; `return` exits the current function (Correct answer)
- `break` skips one iteration; `return` exits all loops
- They are interchangeable
Correct answer: `break` exits the innermost loop; `return` exits the current function
`break` only exits the innermost loop construct, while `return` exits the entire current function and returns to the caller.
What is a recursive function in MATLAB?