MATLAB Assessment Questions and Answers on “User Input and Screen Output – 2”.
1. What is the output of the following code?
a) 91293
b) Syntactical Error
c) The format specifier does not exist
d) ‘91293’
Answer: d
Clarification: The format specifier %i is used to display integers. But sprint keeps the input arguments in a character array. Hence, the output is ‘91293’.
Output: ‘91293’
2. What is the output of the following code?
a) 9.120000E+02
b) 9.120E+02
c) 9.1200E+02
d) 9.12000E+02
Answer: a
Clarification: If we don’t mention a precision amount, the default number of digits shown after the decimal place will be 6. Hence, option 9.120000E+02 is correct only. Option 9.120E+02 is true for %.3E used as the format specifier.
Output: 9.120000E+02
3. What is the output of the following code?
sprintf(‘%d %d %d’,.1, .2.3)
a) Error
b) ‘1.000000e-01 2.000000e-01 3.000000e-01’
c) 1.000000e-01 2.000000e-01 3.000000e-01
d) []
Answer: a
Clarification: The %d format specifier converts the arguments into those given in option ‘1.000000e-01 2.000000e-01 3.000000e-01’. But we have not given a, after .2. So, .3 is not even considered an argument and .2.3 together results in an error.
4. What is the output of the following code?
a) [12]
b) 12
c) [12 ]
d) Error
Answer: a
Clarification: The input to the disp command is a cellular array. Cellular arrays are displayed within []. Output: [12]
5. What is the output of the following code?
a) 0
b) Error
c) 0 0
d) 1 1
Answer: c
Clarification: The size command returns the nature of input given to the command. Here we have given an empty string as an input but the size of the string will return the number of rows and columns present in the input string which is 0 and 0. The ‘’ indicates a 2-D character array which has 0 rows and 0 columns; 0 is treated as a value while returning the size of the input.
6. What is the output of the following code?
a) ‘2.91210000000000000000’
b) ‘2.91’
c) 2.91
d) Error
Answer: b
Clarification: %4.2f implies that the input argument, 2.9121, will be approximated to 4 places places before the decimal point and 2 places after it.
7. What is the output of the following code?
a) Error due to disp
b) Error due to sprintf()
c) Both gives an error
d) No output is displayed
Answer: b
Clarification: Both would give an error due to absence of any input arguments in the commands. But sprintf() will give an error first as MATLAB returns the first error it finds.
8. What is the output of the following code?
a) Error
b) P=234
c) 234
d)
P=
234
View Answer
Answer: a
Clarification: No output arguments can be initialized by the disp command. It would have displayed 234 only if the command was disp(234) but here there will be error because we are assigning the output of the disp command to a variable. This is possible by the sprintf command.
9. How can we influence the disp command with format specifiers?
a) Not possible
b) Via the sprintf() command
c) Use format specifiers as string input
d) Give format specifiers as input only
