Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a private property in a MATLAB class.
MATLAB
classdef MyClass
properties ([1])
data
end
endAttempts:
3 left
💡 Hint
Common Mistakes
Using 'public' instead of 'private' allows access from outside the class.
Using 'constant' is for fixed values, not access control.
✗ Incorrect
The keyword 'private' makes the property accessible only within the class methods.
2fill in blank
mediumComplete the code to define a method with protected access in a MATLAB class.
MATLAB
classdef MyClass
methods ([1])
function greet(obj)
disp('Hello')
end
end
endAttempts:
3 left
💡 Hint
Common Mistakes
Using 'private' restricts access only to the class itself.
Using 'static' is unrelated to access control.
✗ Incorrect
The 'protected' keyword allows access to methods from the class and subclasses only.
3fill in blank
hardFix the error in the code to correctly restrict access to a method only within the class.
MATLAB
classdef MyClass
methods ([1])
function secret(obj)
disp('Secret method')
end
end
endAttempts:
3 left
💡 Hint
Common Mistakes
Using 'public' exposes the method to all.
Using 'global' is not valid for method access control.
✗ Incorrect
The 'private' keyword restricts method access to within the class only.
4fill in blank
hardFill both blanks to define a class with a private property and a public method.
MATLAB
classdef MyClass
properties ([1])
secretData
end
methods ([2])
function showData(obj)
disp(obj.secretData)
end
end
endAttempts:
3 left
💡 Hint
Common Mistakes
Making properties public exposes data directly.
Making methods private prevents outside access.
✗ Incorrect
Properties are private to hide data; methods are public to allow access.
5fill in blank
hardFill all three blanks to define a class with a protected property, a private method, and a public method.
MATLAB
classdef MyClass
properties ([1])
data
end
methods ([2])
function helper(obj)
disp('Helper method')
end
end
methods ([3])
function show(obj)
obj.helper();
disp(obj.data)
end
end
endAttempts:
3 left
💡 Hint
Common Mistakes
Making helper method public exposes internal logic.
Making data property public exposes data.
✗ Incorrect
Property is protected, helper method is private, show method is public.
