MATLAB Data Types and Structures 2 — Questions and Answers
Question 1: What does the MATLAB function `iscell()` return when passed a cell array?
- 0
- 1 (Correct answer)
- The cell array contents
- An error
Correct answer: 1
`iscell()` returns logical 1 (true) if the input is a cell array, and 0 otherwise.
Question 2: Which MATLAB syntax correctly accesses the second element of a cell array `C`?
- C(2)
- C{2} (Correct answer)
- C[2]
- C.2
Correct answer: C{2}
Curly braces `C{2}` extract the contents of a cell, while parentheses `C(2)` return a cell array subset.
Question 3: What is the default numeric data type for variables created by typing a number in MATLAB?
- int32
- single
- double (Correct answer)
- float64
Correct answer: double
MATLAB defaults to `double` (64-bit floating point) for all numeric literals unless explicitly cast.
Question 4: How do you create a 1x3 struct array named `s` with field `name` in MATLAB?
- s = struct('name', {'Alice','Bob','Carol'}) (Correct answer)
- s = {struct('name','Alice'), struct('name','Bob'), struct('name','Carol')}
- s.name = {'Alice','Bob','Carol'}
- s = struct(['Alice','Bob','Carol'])
Correct answer: s = struct('name', {'Alice','Bob','Carol'})
Passing a cell array of values to `struct()` automatically creates a struct array with one element per cell.
Question 5: What does `typecast(int32(1), 'uint32')` return in MATLAB?
- 1 as uint32
- An error due to sign mismatch
- 0
- The bit pattern of int32(1) reinterpreted as uint32 (Correct answer)
Correct answer: The bit pattern of int32(1) reinterpreted as uint32
`typecast` reinterprets the underlying bit pattern without converting the value, so int32(1) becomes uint32(1) since the bits are identical.
Question 6: Which function converts a numeric array to a logical array in MATLAB?
- bool()
- logical() (Correct answer)
- tobool()
- int2logical()
Correct answer: logical()
`logical()` converts a numeric array to logical type, where 0 becomes false and any nonzero becomes true.
Question 7: What is the result of `class(uint8(200) + uint8(100))` in MATLAB?
- double
- uint16
- uint8 (Correct answer)
- int16
Correct answer: uint8
Arithmetic on uint8 operands produces a uint8 result, with the value saturating at 255 instead of overflowing.
What does the MATLAB function `iscell()` return when passed a cell array?