Free MATLAB Functions and Flow Control Questions and Answers — Questions and Answers
Question 1: A function `calculateArea` is defined as follows: ```matlab function area = calculateArea(width, height) if nargin == 1 % Assume a square if only one input is given height = width; end area = width * height; end ``` What is the output of the command `calculateArea(5)`?
- 25 (Correct answer)
- Error: Not enough input arguments.
- 0
- 5
Correct answer: 25
The function is called with one argument, so the special variable `nargin` returns 1. The `if` condition evaluates to true, causing the `height` variable to be assigned the value of `width` (which is 5). The function then calculates `area = 5 * 5`, returning 25.
Question 2: A script `mainScript.m` contains the following code: ```matlab % mainScript.m value = 10; modifyValue(value); disp(value); ``` The function `modifyValue` is defined in a separate file `modifyValue.m`: ```matlab function modifyValue(inputArg) inputArg = inputArg + 5; end ``` Assuming both files are in the current directory, what is displayed in the Command Window when `mainScript` is run?
- 15
- 10 (Correct answer)
- An error is thrown because `value` is not returned.
- 5
Correct answer: 10
MATLAB passes arguments to functions by value. This means that when `value` is passed to `modifyValue`, a local copy of the variable is created within the function's workspace named `inputArg`. Modifying `inputArg` inside the function does not affect the original `value` variable in the script's (base) workspace. Therefore, the value of `value` remains 10.
Question 3: After executing the following code, what is the final value of `x`? ```matlab x = 1; n = 10; while x <= n x = x * 2; n = n - 1; end ```
- 16
- 4
- 8 (Correct answer)
- 10
Correct answer: 8
The loop executes as follows: - Pass 1: `x=1`, `n=10`. Condition `1<=10` is true. `x` becomes `2`, `n` becomes `9`. - Pass 2: `x=2`, `n=9`. Condition `2<=9` is true. `x` becomes `4`, `n` becomes `8`. - Pass 3: `x=4`, `n=8`. Condition `4<=8` is true. `x` becomes `8`, `n` becomes `7`. - Pass 4: `x=8`, `n=7`. Condition `8<=7` is false. The loop terminates. The final value of `x` is 8.
Question 4: Consider the following function `countCalls`: ```matlab function currentCount = countCalls() persistent callCounter; if isempty(callCounter) callCounter = 1; else callCounter = callCounter + 1; end currentCount = callCounter; end ``` If the following commands are executed sequentially in the Command Window, what is the final value of `c`? ```matlab countCalls(); countCalls(); c = countCalls(); ```
- 1
- 2
- An error occurs because `callCounter` is not initialized.
- 3 (Correct answer)
Correct answer: 3
A `persistent` variable is local to a function but retains its value in memory between calls to that function. The first call initializes `callCounter` to 1. The second call increments it to 2. The third call increments it to 3, and this final value is assigned to the output variable `c`.
Question 5: A programmer needs to search a numeric vector `V` for the first element greater than 100 and store its index. Which code snippet correctly and most efficiently accomplishes this?
- ```matlab targetIndex = -1; for k = 1:length(V) if V(k) > 100 targetIndex = k; break; end end ``` (Correct answer)
- ```matlab targetIndex = -1; for k = 1:length(V) if V(k) > 100 targetIndex = k; end end ```
- ```matlab targetIndex = -1; for k = 1:length(V) if V(k) <= 100 continue; end targetIndex = k; end ```
- ```matlab targetIndex = find(V > 100, 1, 'first'); continue; ```
Correct answer: ```matlab targetIndex = -1; for k = 1:length(V) if V(k) > 100 targetIndex = k; break; end end ```
The `break` statement immediately terminates the `for` loop as soon as the condition `V(k) > 100` is met. This is the most efficient approach because it stops unnecessary iterations once the first matching element is found. The second option finds the *last* such element, not the first. The third option also finds the *last* such element. The fourth option is syntactically incorrect as `continue` cannot be used outside of a loop.
Question 6: What is the value of `result` after this code is executed? ```matlab value = 'medium'; switch value case {'low', 'minimum'} result = 1; case 'medium' result = 2; case 'high' result = 3; otherwise result = 0; end ```
- 1
- 2 (Correct answer)
- 0
- An error occurs because 'medium' is a string.
Correct answer: 2
The `switch` statement compares the `switch_expression` (`value`) to each `case`. The variable `value` contains the character vector 'medium', which exactly matches the second `case 'medium'`. MATLAB executes the statement associated with that case, `result = 2;`, and then exits the switch block.
A function `calculateArea` is defined as follows:
```matlab
function area = calculateArea(width, height)
if nargin == 1
% Assume a square if only one input is given
height = width;
end
area = width * height;
end
```
What is the output of the command `calculateArea(5)`?