Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a value class in MATLAB.
MATLAB
classdef MyClass [1]
properties
Data
end
endAttempts:
3 left
💡 Hint
Common Mistakes
Using keyword to define a value class.
Adding inheritance from handle when not needed.
✗ Incorrect
In MATLAB, value classes are defined by default without inheriting from handle. Using here is a placeholder to indicate a value class, but actually no inheritance is needed. The blank expects the keyword to indicate value class, so is correct.
2fill in blank
mediumComplete the code to define a handle class in MATLAB.
MATLAB
classdef MyHandleClass < [1]
properties
Data
end
endAttempts:
3 left
💡 Hint
Common Mistakes
Using 'value' instead of 'handle' as superclass.
Not inheriting from any class for handle behavior.
✗ Incorrect
Handle classes in MATLAB inherit from the built-in class 'handle'. This allows objects to be passed by reference.
3fill in blank
hardFix the error in the code to correctly copy a handle object.
MATLAB
obj1 = MyHandleClass();
obj2 = [1](obj1);Attempts:
3 left
💡 Hint
Common Mistakes
Assigning obj2 = obj1, which copies the reference, not the object.
Using 'clone' which is not a MATLAB method.
✗ Incorrect
To copy a handle object in MATLAB, you use the 'copy' method, which creates a new object with the same properties.
4fill in blank
hardFill both blanks to create a value class and assign a new object.
MATLAB
classdef MyValueClass [1] properties Data end end obj1 = MyValueClass(); obj2 = [2];
Attempts:
3 left
💡 Hint
Common Mistakes
Using instead of for value class.
Assigning obj2 = MyValueClass without parentheses.
✗ Incorrect
Value classes do not inherit from handle, so is correct. To create a new object, call the constructor with parentheses: MyValueClass().
5fill in blank
hardFill all three blanks to define a handle class, create an object, and copy it.
MATLAB
classdef MyHandleClass < [1] properties Data end end obj1 = [2]; obj2 = [3](obj1);
Attempts:
3 left
💡 Hint
Common Mistakes
Not inheriting from 'handle'.
Assigning obj1 without parentheses.
Using 'clone' instead of 'copy'.
✗ Incorrect
Handle classes inherit from 'handle'. Create an object by calling the constructor with parentheses. Copy the object using the 'copy' method.
