Bird
Raised Fist0
MATLABdata~20 mins

Access control in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
MATLAB Access Control Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this MATLAB class property access?

Consider the following MATLAB class definition and usage:

classdef MyClass
    properties (Access = private)
        secret = 42;
    end
    methods
        function val = getSecret(obj)
            val = obj.secret;
        end
    end
end

obj = MyClass();
disp(obj.getSecret());

What will be printed when running this code?

MATLAB
classdef MyClass
    properties (Access = private)
        secret = 42;
    end
    methods
        function val = getSecret(obj)
            val = obj.secret;
        end
    end
end

obj = MyClass();
disp(obj.getSecret());
A42
BError: Property 'secret' is not accessible.
C0
DEmpty output
Attempts:
2 left
💡 Hint

Private properties can be accessed inside class methods.

❓ Predict Output
intermediate
2:00remaining
What happens when accessing a protected property from outside the class?

Given this MATLAB class:

classdef MyClass
    properties (Access = protected)
        data = 10;
    end
end

obj = MyClass();
disp(obj.data);

What will be the result of running this code?

MATLAB
classdef MyClass
    properties (Access = protected)
        data = 10;
    end
end

obj = MyClass();
disp(obj.data);
A10
BError: Property 'data' is not accessible.
CEmpty output
D0
Attempts:
2 left
💡 Hint

Protected properties are accessible only within the class and subclasses.

🔧 Debug
advanced
2:00remaining
Why does this code cause an error when accessing a private method?

Examine this MATLAB class and script:

classdef MyClass
    methods (Access = private)
        function secretMethod(obj)
            disp('Secret method called');
        end
    end
    methods
        function callSecret(obj)
            obj.secretMethod();
        end
    end
end

obj = MyClass();
obj.secretMethod();

Why does the last line cause an error?

MATLAB
classdef MyClass
    methods (Access = private)
        function secretMethod(obj)
            disp('Secret method called');
        end
    end
    methods
        function callSecret(obj)
            obj.secretMethod();
        end
    end
end

obj = MyClass();
obj.secretMethod();
ABecause private methods cannot be called from outside the class.
BBecause the method name is misspelled.
CBecause the object was not created properly.
DBecause the method is static and needs to be called differently.
Attempts:
2 left
💡 Hint

Think about where private methods can be accessed.

❓ Predict Output
advanced
2:00remaining
What is the output of this subclass accessing a protected property?

Consider these MATLAB classes:

classdef ParentClass
    properties (Access = protected)
        value = 5;
    end
end

classdef ChildClass < ParentClass
    methods
        function v = getValue(obj)
            v = obj.value;
        end
    end
end

obj = ChildClass();
disp(obj.getValue());

What will be printed?

MATLAB
classdef ParentClass
    properties (Access = protected)
        value = 5;
    end
end

classdef ChildClass < ParentClass
    methods
        function v = getValue(obj)
            v = obj.value;
        end
    end
end

obj = ChildClass();
disp(obj.getValue());
AEmpty output
B0
C5
DError: Property 'value' is not accessible.
Attempts:
2 left
💡 Hint

Protected properties are accessible in subclasses.

🧠 Conceptual
expert
2:00remaining
Which access level allows property access only within the defining class and its subclasses?

In MATLAB, which property access level restricts access to the class itself and any subclasses, but not from outside these classes?

Aglobal
Bprivate
Cpublic
Dprotected
Attempts:
2 left
💡 Hint

Think about access levels that allow inheritance but restrict external access.

Practice

(1/5)
1. What does the private access modifier do in a MATLAB class?
easy
A. It makes properties or methods accessible only in the command window.
B. It allows access to properties or methods from anywhere in the program.
C. It allows access only to child classes and the class itself.
D. It restricts access to class properties or methods only within the class itself.

Solution

  1. Step 1: Understand the role of private in MATLAB classes

    The private keyword restricts access to properties or methods so that only the class itself can use them.
  2. Step 2: Compare with other access modifiers

    public allows access from anywhere, protected allows access from child classes, but private is the most restrictive.
  3. Final Answer:

    It restricts access to class properties or methods only within the class itself. -> Option D
  4. Quick Check:

    private means class-only access [OK]
Hint: Private means only the class itself can access [OK]
Common Mistakes:
  • Confusing private with public access
  • Thinking private allows child class access
  • Assuming private is accessible from outside
2. Which of the following is the correct syntax to declare a protected property named data in a MATLAB class?
easy
A. properties protected data end
B. properties (protected) data end
C. properties (public) data end
D. properties (private) data end

Solution

  1. Step 1: Recall MATLAB property access syntax

    Access modifiers are specified in parentheses after the properties keyword, e.g., properties (protected).
  2. Step 2: Identify correct syntax for protected

    The correct syntax uses parentheses: properties (protected). properties protected data end misses parentheses, so it's invalid.
  3. Final Answer:

    properties (protected) data end -> Option B
  4. Quick Check:

    Use parentheses for access modifiers [OK]
Hint: Use parentheses after properties for access [OK]
Common Mistakes:
  • Omitting parentheses around access modifier
  • Using wrong access modifier for the question
  • Placing access modifier outside properties block
3. Consider this MATLAB class snippet:
classdef MyClass
    properties (Access = private)
        secret = 42;
    end
    methods
        function val = getSecret(obj)
            val = obj.secret;
        end
    end
end

obj = MyClass();
disp(obj.secret);
What happens when you run this code?
medium
A. It causes an error because secret is private.
B. It displays 42.
C. It displays an empty value.
D. It displays the class name.

Solution

  1. Step 1: Identify access level of secret

    The property secret is declared private, so it cannot be accessed directly outside the class.
  2. Step 2: Analyze the code trying to access secret

    The line disp(obj.secret); tries to access the private property directly, which is not allowed and causes an error.
  3. Final Answer:

    It causes an error because secret is private. -> Option A
  4. Quick Check:

    Private properties block outside access [OK]
Hint: Private properties can't be accessed outside class [OK]
Common Mistakes:
  • Assuming private properties can be accessed directly
  • Confusing method access with property access
  • Ignoring access modifiers in property declaration
4. This MATLAB class has an error. Find and fix it:
classdef Parent
    properties (Access = protected)
        value
    end
end

classdef Child < Parent
    methods
        function setValue(obj, val)
            obj.value = val;
        end
    end
end

c = Child();
c.setValue(10);
disp(c.value);
medium
A. Add a getter method in Child to access value.
B. Make setValue a static method.
C. Change value access to public.
D. Remove inheritance from Child.

Solution

  1. Step 1: Understand protected access

    The property value is protected, so Child can set it, but outside code cannot access it directly.
  2. Step 2: Identify why disp(c.value) fails

    Outside code tries to access value directly, which is not allowed for protected properties.
  3. Step 3: Fix by adding a getter method

    Adding a method in Child to return value allows controlled access without changing access level.
  4. Final Answer:

    Add a getter method in Child to access value. -> Option A
  5. Quick Check:

    Use getter for protected property access outside class [OK]
Hint: Use getter methods to access protected properties outside class [OK]
Common Mistakes:
  • Trying to access protected property directly outside class
  • Changing access to public unnecessarily
  • Not using inheritance properly
5. You want to create a MATLAB class BankAccount where the balance is private, but child classes can update it. You also want to allow anyone to check the balance. Which access control setup is correct?
hard
A. Declare balance as public; anyone can read and update balance.
B. Declare balance as private and provide a public getter method; child classes cannot update balance directly.
C. Declare balance as protected and provide a public getter method; child classes can update balance, others can only read it.
D. Declare balance as private with no getter; only the class can access balance.

Solution

  1. Step 1: Identify access needs for balance

    Balance should be hidden from outside for updates but readable by anyone, and updatable by child classes.
  2. Step 2: Choose access modifier and methods

    Protected allows child classes to update balance. A public getter method allows anyone to read balance safely.
  3. Final Answer:

    Declare balance as protected and provide a public getter method; child classes can update balance, others can only read it. -> Option C
  4. Quick Check:

    Protected + public getter fits requirements [OK]
Hint: Protected for update by children, public getter for read [OK]
Common Mistakes:
  • Making balance public allowing unwanted updates
  • Using private without getter blocking read access
  • Not providing a getter method for balance
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard