Challenge - 5 Problems
Multiple Outputs Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a function with multiple outputs
What is the output of the following MATLAB code?
MATLAB
function [a, b] = example(x)
a = x + 1;
b = x * 2;
end
[a_out, b_out] = example(3);
disp([a_out, b_out]);Attempts:
2 left
💡 Hint
Remember the function adds 1 to x for the first output and multiplies x by 2 for the second output.
✗ Incorrect
The function returns two outputs: a = x + 1 = 4, and b = x * 2 = 6. So the displayed array is [4 6].
❓ Predict Output
intermediate2:00remaining
Using only one output from a function with multiple outputs
What will be the output of this MATLAB code?
MATLAB
function [a, b] = example(x)
a = x^2;
b = x^3;
end
result = example(2);
disp(result);Attempts:
2 left
💡 Hint
When calling a function with multiple outputs but only one variable, MATLAB returns the first output.
✗ Incorrect
Only the first output 'a' is assigned to 'result', which is 2^2 = 4.
🔧 Debug
advanced2:00remaining
Identify the error in multiple output assignment
What error will this MATLAB code produce?
MATLAB
function [x, y] = calc()
x = 5;
y = 10;
end
[a, b, c] = calc();Attempts:
2 left
💡 Hint
The function returns only two outputs but you are trying to assign three.
✗ Incorrect
The function returns two outputs but the caller expects three, causing a 'Too many output arguments' error.
❓ Predict Output
advanced2:00remaining
Output of nested function calls with multiple outputs
What is the output of this MATLAB code?
MATLAB
function [sumVal, prodVal] = compute(a, b)
sumVal = a + b;
prodVal = a * b;
end
[x, y] = compute(2, 3);
[z, w] = compute(x, y);
disp([z, w]);Attempts:
2 left
💡 Hint
First compute(2,3) returns sum=5 and product=6. Then compute(5,6) returns sum=11 and product=30.
✗ Incorrect
First call: [x, y] = compute(2, 3) returns x=5, y=6. Second call: [z, w] = compute(5, 6) returns z=11, w=30. disp([z, w]) displays [11 30].
❓ Predict Output
expert2:00remaining
Multiple outputs with conditional assignment
What is the output of this MATLAB code?
MATLAB
function [a, b] = conditionalOutputs(x)
if x > 0
a = x;
b = x^2;
else
a = -x;
b = -x^2;
end
end
[p, q] = conditionalOutputs(-3);
disp([p, q]);Attempts:
2 left
💡 Hint
Check the else branch for negative input values.
✗ Incorrect
Since x = -3 (less than 0), a = -(-3) = 3, b = -((-3)^2) = -9, so output is [3 -9].