MATLAB Multiple Choice Questions on “Evaluation – 1”.
1. What is the output of the following code?
a) q=23
b) Logical Error
c) Symbolic error
d) No output
Answer: a
Clarification: As eval evaluates the MATLAB expression in the input, it will evaluate the input to 23 and assign it to q. There are no errors in the above code.
2. What is the output of the following code?
[q,r]=eval(‘sprintf('23 45')’)
a) Unexpected MATLAB expression
b) q=23,r=45
c)
q = 23 r = 45
d) Error due to eval
Answer: a
Clarification: The eval command returns an error if more than a pair of inverted commas are found. Hence, unexpected MATLAB expression is correct option. Option q=23,r=45 cannot happen since 23 and 45 will be stored in a single character array.
3. What is the output of the following code?
a) p=1
b)
p=
1
c) Syntactical error
d) The eval command does not exist
Answer: c
Clarification: An input to the eval command will have to be within a pair of inverted commas. Here, we haven’t used them so MATLAB will return an error.
4. What is the output of the following code?
a) No such command
b) Error
c) 1
d) ‘ ans=1 ’
Answer: d
Clarification: Evalc takes the entire output of the input expression into a string. Hence, it will show a as an answer were the entire thing will be shown, within, as a string.
5. What is the size of p from the following code?
a) 2 bytes
b) 0 bytes
c) Error
d) 6 bytes
Answer: a
Clarification: The evalc command results in an empty character array. Hence, the size of p will be 0 but it’s class will be char.
6. The for keyword can be used to evaluate expressions using eval().
a) True
b) False
Answer: b
Clarification: Reserved keywords cannot be used explicitly in the eval() command. But they can be used by mentioning a function which is defined to use a for loop.
7. The evalc function is different from the eval command ________
a) no difference
b) from precision of output
c) from nature of output
d) from display of output
Answer: d
Clarification: The entire output is displayed as a character array using evalc while the eval command displays the output itself. There is no difference in precision or nature of output.
8. What is common to evalc and the eval commands, apart from the output?
a) The error message is displayed separately
b) The outputs are of different accuracy
c) There is nothing common
d) evalc does not exist
Answer: a
Clarification: The error messages, if any error, pertaining to the input expression, occurs, are displayed separately by both the command.
9. What is the output of the following code?
a) P= ‘ ’
b) P will be an empty character array
c) Error
d) No such function
Answer: c
Clarification: We have assigned the output of the eval command to an expression. Hence, we will have to give an expression within ‘’ which results into any value. Here, we haven’t provided any expression so the statement in not complete. Hence, MATLAB returns an error.
10. What is the output of the following code?
a) Syntactical error
b) Logical Error
c) 1235
d) ‘1234’
Answer: c
Clarification: There is no error in the following code. The string input to the feval command can also be given within ‘’. Hence, the final output is 1235.
Output: 1235
