Challenge - 5 Problems
Handle vs Value Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of modifying a handle class object
Consider the following MATLAB code using a handle class. What will be the output after running the code?
MATLAB
classdef MyHandle < handle
properties
Value
end
end
obj1 = MyHandle();
obj1.Value = 10;
obj2 = obj1;
obj2.Value = 20;
disp(obj1.Value);Attempts:
2 left
💡 Hint
Remember that handle classes share the same object reference.
✗ Incorrect
In MATLAB, handle class objects are passed by reference. Assigning obj2 = obj1 makes obj2 refer to the same object as obj1. Changing obj2.Value changes obj1.Value as well.
❓ Predict Output
intermediateOutput of modifying a value class object
Consider the following MATLAB code using a value class. What will be the output after running the code?
MATLAB
classdef MyValue
properties
Value
end
end
obj1 = MyValue();
obj1.Value = 10;
obj2 = obj1;
obj2.Value = 20;
disp(obj1.Value);Attempts:
2 left
💡 Hint
Value classes create copies when assigned.
✗ Incorrect
Value class objects are copied when assigned. Changing obj2.Value does not affect obj1.Value.
🧠 Conceptual
advancedDifference in behavior between handle and value classes
Which statement best describes the difference between handle and value classes in MATLAB?
Attempts:
2 left
💡 Hint
Think about what happens when you assign one object to another variable.
✗ Incorrect
Handle classes behave like pointers; assigning them copies the reference. Value classes create a new copy on assignment.
❓ Predict Output
advancedEffect of modifying a property inside a method for handle vs value classes
Given the following MATLAB classes and code, what will be the output after running the script?
MATLAB
classdef MyHandle < handle
properties
Value = 0;
end
methods
function increment(obj)
obj.Value = obj.Value + 1;
end
end
end
classdef MyValue
properties
Value = 0;
end
methods
function obj = increment(obj)
obj.Value = obj.Value + 1;
end
end
end
h = MyHandle();
v = MyValue();
h.increment();
v = v.increment();
disp([h.Value, v.Value]);Attempts:
2 left
💡 Hint
Consider how methods modify properties in handle vs value classes.
✗ Incorrect
The handle class method modifies the object directly in place. The value class method returns a modified copy, which is assigned back to v with v = v.increment(), updating v.
🔧 Debug
expertIdentify the error when mixing handle and value class assignments
What error will occur when running the following MATLAB code?
MATLAB
classdef MyHandle < handle
properties
Value
end
end
classdef MyValue
properties
Value
end
end
h = MyHandle();
h.Value = 5;
v = MyValue();
v.Value = h;
v.Value.Value = 10;Attempts:
2 left
💡 Hint
Consider what happens when you assign a handle object to a value class property and then try to modify it.
✗ Incorrect
No error occurs. A value class can store a handle class object in its property. Properties of the handle class object can be modified directly even through the value class property because handle classes support mutation.
