MATLAB Environment and Syntax 2 — Questions and Answers
Question 1: What does the `whos` command display that `who` does not?
- Variable names only
- Variable names, sizes, bytes, and class (Correct answer)
- Only numeric variables
- Only the last assigned variable
Correct answer: Variable names, sizes, bytes, and class
`whos` provides detailed information including size, bytes allocated, and data class for each variable, while `who` lists names only.
Question 2: In MATLAB, what is the result of entering `3 == 3` at the command line?
- 3
- true
- 1 (Correct answer)
- ans = true
Correct answer: 1
MATLAB's equality operator returns a logical 1 (true) or 0 (false), and the result is displayed as the integer 1.
Question 3: Which keyboard shortcut recalls the previous command in the MATLAB Command Window?
- Ctrl+P
- Up arrow key (Correct answer)
- Ctrl+Z
- Alt+Up
Correct answer: Up arrow key
Pressing the Up arrow key cycles through command history in the MATLAB Command Window.
Question 4: What happens when you type `clear all` in MATLAB?
- Clears the Command Window output
- Removes all variables from the workspace (Correct answer)
- Closes all open figures
- Resets MATLAB to default settings
Correct answer: Removes all variables from the workspace
`clear all` removes all variables, globals, functions, and MEX links from the workspace memory.
Question 5: How do you suppress output for a statement in MATLAB?
- End the line with a period (.)
- End the line with a semicolon (;) (Correct answer)
- Prefix the line with //
- Wrap the expression in suppress()
Correct answer: End the line with a semicolon (;)
A semicolon at the end of a statement prevents MATLAB from displaying the result in the Command Window.
Question 6: What is the purpose of the `ans` variable in MATLAB?
- It stores the answer to the last `solve()` call
- It holds the result of the most recent expression that wasn't assigned to a variable (Correct answer)
- It is a reserved constant equal to 1
- It stores the last user input from `input()`
Correct answer: It holds the result of the most recent expression that wasn't assigned to a variable
`ans` is automatically assigned the result of any expression evaluated without an explicit variable assignment.
Question 7: Which of the following is a valid MATLAB variable name?
- 1stValue
- first-value
- _myVar
- firstValue1 (Correct answer)
Correct answer: firstValue1
MATLAB variable names must start with a letter and contain only letters, digits, and underscores; `firstValue1` satisfies all rules.
What does the `whos` command display that `who` does not?