Bird
Raised Fist0
MATLABdata~20 mins

Properties and methods in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of Properties and Methods
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of method accessing property
What is the output of this MATLAB code when calling the method getArea?
MATLAB
classdef Circle
    properties
        Radius
    end
    methods
        function obj = Circle(r)
            obj.Radius = r;
        end
        function a = getArea(obj)
            a = pi * obj.Radius^2;
        end
    end
end

c = Circle(3);
disp(c.getArea())
AError: Undefined function or variable 'Radius'
B28.2743
C9
D18.8496
Attempts:
2 left
💡 Hint
Recall the formula for the area of a circle is pi times radius squared.
❓ Predict Output
intermediate
2:00remaining
Value of property after method call
What is the value of the property Count after running this code?
MATLAB
classdef Counter
    properties
        Count = 0;
    end
    methods
        function obj = increment(obj)
            obj.Count = obj.Count + 1;
        end
    end
end

c = Counter();
c = c.increment();
c = c.increment();
disp(c.Count)
AError: Attempt to modify property of a value class without assignment
B0
C1
D2
Attempts:
2 left
💡 Hint
Each call to increment adds 1 to Count.
❓ Predict Output
advanced
2:00remaining
Output of method with dependent property
What is the output of obj.Area after creating the object obj = Rectangle(4,5);?
MATLAB
classdef Rectangle
    properties
        Width
        Height
    end
    properties (Dependent)
        Area
    end
    methods
        function obj = Rectangle(w,h)
            obj.Width = w;
            obj.Height = h;
        end
        function a = get.Area(obj)
            a = obj.Width * obj.Height;
        end
    end
end

obj = Rectangle(4,5);
disp(obj.Area)
AError: Property 'Area' is not settable
B9
C20
D0
Attempts:
2 left
💡 Hint
Dependent properties compute their value on access.
❓ Predict Output
advanced
2:00remaining
Error type when accessing private property
What error occurs when running this code?
MATLAB
classdef Secret
    properties (Access = private)
        Data = 42;
    end
    methods
        function val = getData(obj)
            val = obj.Data;
        end
    end
end

s = Secret();
disp(s.Data);
AError: No appropriate method, property, or field 'Data' for class 'Secret'.
BError: Undefined function or variable 'Data'.
CError: Attempt to access private property 'Data' from outside the class.
D42
Attempts:
2 left
💡 Hint
Private properties cannot be accessed directly from outside the class.
🧠 Conceptual
expert
2:00remaining
Number of items in property after method call
Consider this MATLAB class that stores names in a property. After running the code below, how many names are stored in the Names property?
MATLAB
classdef NameList
    properties
        Names = {};
    end
    methods
        function obj = addName(obj, newName)
            obj.Names{end+1} = newName;
        end
    end
end

list = NameList();
list = list.addName('Alice');
list = list.addName('Bob');
list = list.addName('Charlie');
disp(length(list.Names))
A3
B0
C1
DError: Cell contents reference from a non-cell array object.
Attempts:
2 left
💡 Hint
Each call to addName adds one name to the cell array.

Practice

(1/5)
1. In MATLAB, what is the main purpose of properties in a class definition?
easy
A. To define functions that perform actions
B. To create loops inside the class
C. To store data related to the object
D. To handle errors during execution

Solution

  1. Step 1: Understand what properties represent

    Properties in MATLAB classes are used to hold data or attributes about the object.
  2. Step 2: Differentiate properties from methods

    Methods are functions that act on the data, not the data itself.
  3. Final Answer:

    To store data related to the object -> Option C
  4. Quick Check:

    Properties = Data storage [OK]
Hint: Properties always hold data, methods perform actions [OK]
Common Mistakes:
  • Confusing properties with methods
  • Thinking properties are for functions
  • Assuming properties handle errors
2. Which of the following is the correct way to define a method inside a MATLAB class?
easy
A. methods function obj = setValue(obj, val) obj.Value = val; end end
B. properties function obj = setValue(obj, val) obj.Value = val; end end
C. function setValue(obj, val) obj.Value = val; end
D. methods obj.Value = val; end

Solution

  1. Step 1: Identify method block syntax

    Methods in MATLAB classes are defined inside a methods block.
  2. Step 2: Check function definition inside methods

    The function must have the object as input and output if modifying properties.
  3. Final Answer:

    methods function obj = setValue(obj, val) obj.Value = val; end end -> Option A
  4. Quick Check:

    Methods block + function syntax = methods function obj = setValue(obj, val) obj.Value = val; end end [OK]
Hint: Methods go inside 'methods' block with function keyword [OK]
Common Mistakes:
  • Placing functions inside properties block
  • Omitting the methods keyword
  • Not returning the modified object
3. Consider the following MATLAB class snippet:
classdef MyClass
  properties
    Value
  end
  methods
    function obj = setValue(obj, val)
      obj.Value = val;
    end
    function v = getValue(obj)
      v = obj.Value;
    end
  end
end

obj = MyClass();
obj = obj.setValue(10);
disp(obj.getValue());

What will be the output when this code runs?
medium
A. 0
B. obj.Value
C. Error: Undefined function or variable 'Value'
D. 10

Solution

  1. Step 1: Understand method calls and property assignment

    The method setValue sets the property Value to 10 and returns the updated object.
  2. Step 2: Check the output of getValue

    The getValue method returns the current value of the property Value, which is 10.
  3. Final Answer:

    10 -> Option D
  4. Quick Check:

    Property set and get returns 10 [OK]
Hint: Methods update and return object; get method returns stored value [OK]
Common Mistakes:
  • Forgetting to assign returned object from setValue
  • Expecting property access without method
  • Confusing property name with variable
4. Identify the error in this MATLAB class method definition:
methods
  function setValue(obj, val)
    obj.Value = val;
  end
end
medium
A. The method must return the modified object
B. The method name is invalid
C. Properties block is missing
D. The function keyword is not allowed inside methods

Solution

  1. Step 1: Check method signature for modifying properties

    In MATLAB, when a method modifies object properties, it must return the updated object.
  2. Step 2: Identify missing return value

    The method setValue does not return obj, so changes won't persist.
  3. Final Answer:

    The method must return the modified object -> Option A
  4. Quick Check:

    Modify method must return object [OK]
Hint: Return updated object when changing properties [OK]
Common Mistakes:
  • Not returning the object after modification
  • Assuming properties change without return
  • Confusing function keyword usage
5. You want to create a MATLAB class BankAccount with a property Balance and a method deposit that adds money to the balance only if the amount is positive. Which of the following method definitions correctly implements this?
hard
A. methods function deposit(obj, amount) if amount > 0 obj.Balance = obj.Balance + amount; end end end
B. methods function obj = deposit(obj, amount) if amount > 0 obj.Balance = obj.Balance + amount; end end end
C. methods function obj = deposit(amount) if amount > 0 obj.Balance = obj.Balance + amount; end end end
D. methods function obj = deposit(obj, amount) obj.Balance = obj.Balance + amount; end end

Solution

  1. Step 1: Check method signature for modifying object

    The method must accept the object and amount, and return the updated object.
  2. Step 2: Verify condition for positive amount

    The method correctly adds amount only if it is greater than zero.
  3. Step 3: Identify incorrect options

    methods function deposit(obj, amount) if amount > 0 obj.Balance = obj.Balance + amount; end end end does not return the object, so changes won't persist. methods function obj = deposit(amount) if amount > 0 obj.Balance = obj.Balance + amount; end end end misses the object input. methods function obj = deposit(obj, amount) obj.Balance = obj.Balance + amount; end end adds amount without checking if positive.
  4. Final Answer:

    methods function obj = deposit(obj, amount) if amount > 0 obj.Balance = obj.Balance + amount; end end end -> Option B
  5. Quick Check:

    Return updated object and check amount > 0 [OK]
Hint: Return object and check positive amount before adding [OK]
Common Mistakes:
  • Not returning updated object
  • Missing object input argument
  • Not checking if amount is positive
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