Challenge - 5 Problems
Function Handle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple function handle call
What is the output of this MATLAB code?
MATLAB
f = @(x) x^2 + 3*x + 1; result = f(2); disp(result);
Attempts:
2 left
💡 Hint
Calculate 2 squared plus 3 times 2 plus 1.
✗ Incorrect
The function computes x^2 + 3*x + 1. For x=2, 2^2=4, 3*2=6, sum is 4+6+1=11.
❓ Predict Output
intermediate2:00remaining
Function handle with multiple inputs
What is the output of this MATLAB code?
MATLAB
g = @(x,y) x*y + y^2; val = g(3,4); disp(val);
Attempts:
2 left
💡 Hint
Calculate 3*4 + 4^2.
✗ Incorrect
g(3,4) = 3*4 + 4^2 = 12 + 16 = 28.
🔧 Debug
advanced2:00remaining
Identify the error in function handle usage
What error does this MATLAB code produce?
MATLAB
h = @(x) x(2) + 5; result = h(3); disp(result);
Attempts:
2 left
💡 Hint
Check how x is used inside the function handle.
✗ Incorrect
The function tries to access the second element of x, but x is a scalar (3), so indexing fails.
🧠 Conceptual
advanced2:00remaining
Function handle behavior with variable scope
Consider this MATLAB code snippet. What is the output?
MATLAB
a = 10; f = @(x) x + a; a = 20; disp(f(5));
Attempts:
2 left
💡 Hint
Anonymous functions in MATLAB capture variables by value at the time of creation.
✗ Incorrect
When the function handle is created, a=10 is captured. Changing a to 20 afterward does not affect the function. Thus, f(5) = 5 + 10 = 15.
❓ Predict Output
expert2:00remaining
Output of nested function handles with array input
What is the output of this MATLAB code?
MATLAB
f1 = @(x) x.^2; f2 = @(g, y) g(y) + 1; arr = [1, 2, 3]; result = f2(f1, arr); disp(result);
Attempts:
2 left
💡 Hint
Apply f1 to each element of arr, then add 1 to each result.
✗ Incorrect
f1 squares each element: [1 4 9]. Adding 1 gives [2 5 10].