Bird
0
0

What is wrong with this code snippet?

medium📝 Debug Q7 of 15
MATLAB - Cell Arrays and Structures
What is wrong with this code snippet?
student(1).Name = 'Tom';
disp(student(1).Age);
student(2).Age = 20;
AMissing semicolon after first line.
BField 'Age' does not exist for student(1).
CCannot mix different fields in structure array.
Dstudent must be initialized before assignment.
Step-by-Step Solution
Solution:
  1. Step 1: Check field existence for student(1)

    student(1) has 'Name' but no 'Age' field assigned yet.
  2. Step 2: disp(student(1).Age) tries to access missing field

    This causes an error because 'Age' is undefined for student(1).
  3. Final Answer:

    Field 'Age' does not exist for student(1). -> Option B
  4. Quick Check:

    Accessing missing field causes error [OK]
Quick Trick: All elements must have same fields to avoid missing field errors [OK]
Common Mistakes:
  • Ignoring that fields can be missing in some elements.
  • Thinking MATLAB auto-fills missing fields.
  • Assuming semicolon omission causes error.

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More MATLAB Quizzes