MATLAB Basic Introduction 4 — Questions and Answers
Question 1: What symbol is used to start a comment in a MATLAB script?
- //
- #
- % (Correct answer)
- --
Correct answer: %
The percent sign (%) starts a comment in MATLAB; everything after it on the same line is ignored.
Question 2: Which of the following creates a 3×3 identity matrix in MATLAB?
- ones(3)
- eye(3) (Correct answer)
- identity(3)
- diag(3)
Correct answer: eye(3)
eye(n) creates an n×n identity matrix with ones on the diagonal and zeros elsewhere.
Question 3: What is the result of logical expression (3 > 2) in MATLAB?
- true
- 1
- Both 'true' and 1 (Correct answer)
- yes
Correct answer: Both 'true' and 1
MATLAB returns logical 1 for true comparisons; logical 1 and the string 'true' are equivalent representations.
Question 4: Which MATLAB function computes the square root of a number?
- pow(x, 0.5)
- sqrt(x) (Correct answer)
- root(x)
- x^(1/2) only
Correct answer: sqrt(x)
sqrt(x) is the dedicated MATLAB function for computing the square root of x.
Question 5: How does MATLAB index arrays — starting from 0 or 1?
- 0, like C and Python
- 1, unlike most languages (Correct answer)
- Depends on the data type
- User-configurable
Correct answer: 1, unlike most languages
MATLAB arrays are 1-indexed, meaning the first element is at index 1, not 0.
Question 6: What does the command 'who' display in MATLAB?
- The current user's name
- A list of variable names in the workspace (Correct answer)
- The command history
- The loaded toolboxes
Correct answer: A list of variable names in the workspace
who lists the names of all variables currently defined in the MATLAB workspace.
Question 7: In MATLAB, what is the difference between '==' and '='?
- No difference, both assign values
- '==' assigns, '=' compares
- '=' assigns a value; '==' tests equality (Correct answer)
- Both are comparison operators
Correct answer: '=' assigns a value; '==' tests equality
The single '=' is the assignment operator, while '==' is the relational equality comparison operator.
What symbol is used to start a comment in a MATLAB script?