0
0
MATLABdata~20 mins

Multiple outputs in MATLAB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multiple Outputs Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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]);
A[6 4]
B[3 6]
C[4 6]
D[4 3]
Attempts:
2 left
💡 Hint
Remember the function adds 1 to x for the first output and multiplies x by 2 for the second output.
Predict Output
intermediate
2: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);
A[4 8]
B4
C[2 4]
DError: Too many output arguments.
Attempts:
2 left
💡 Hint
When calling a function with multiple outputs but only one variable, MATLAB returns the first output.
🔧 Debug
advanced
2: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();
AError: Undefined function or variable 'c'.
BError: Not enough output arguments.
CNo error, a=5, b=10, c=0
DError: Too many output arguments.
Attempts:
2 left
💡 Hint
The function returns only two outputs but you are trying to assign three.
Predict Output
advanced
2: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]);
A[11 30]
B[5 6]
C[5 15]
D[6 7]
Attempts:
2 left
💡 Hint
First compute(2,3) returns sum=5 and product=6. Then compute(5,6) returns sum=11 and product=30.
Predict Output
expert
2: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]);
A[3 -9]
B[-3 -9]
C[3 9]
D[-3 9]
Attempts:
2 left
💡 Hint
Check the else branch for negative input values.