C. Mixing scalar and array structure assignment causes error
D. No error, code runs fine
Solution
Step 1: Analyze structure assignment styles
First line creates scalar structure car with field Make. Next lines assign to car(1), valid for scalar structures.
Step 2: Understand MATLAB structure array rules
MATLAB allows indexing scalar structures and dynamically adding fields; no error occurs.
Final Answer:
No error, code runs fine -> Option D
Quick Check:
Scalar struct indexing + field addition works [OK]
Hint: Scalar structures can be indexed like arrays [OK]
Common Mistakes:
Thinking scalar and array assignments cannot mix
Believing field names must be numeric
Assuming missing semicolons cause errors
5. Given a structure array books with fields Title and Pages, how do you create a new structure array shortBooks containing only books with less than 100 pages?
hard
A. shortBooks = books(books.Pages < 100);
B. shortBooks = books([books.Pages] < 100);
C. shortBooks = books{books.Pages < 100};
D. shortBooks = books(books.Pages > 100);
Solution
Step 1: Extract numeric array of Pages
Use [books.Pages] to get all Pages values as a numeric array.
Step 2: Use logical indexing to filter
Compare with < 100 to get logical indices, then index books with these.
Step 3: Check syntax correctness
shortBooks = books([books.Pages] < 100); correctly uses parentheses and logical indexing; others are incorrect.
Final Answer:
shortBooks = books([books.Pages] < 100); -> Option B
Quick Check:
Logical indexing with [books.Pages] works [OK]
Hint: Use [struct.field] for numeric indexing [OK]
Common Mistakes:
Using curly braces {} instead of parentheses ()
Forgetting to use brackets [] around field extraction
Using wrong comparison operator
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.