Functions and Flow Control Flashcards
6 cards from real MATLAB practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 6 Functions and Flow Control flashcards as text
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)`?
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.
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?
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.
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 ```
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.
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(); ```
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`.
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?
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.
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 ```
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.