This set of MATLAB Assessment Questions and Answers focuses on “User Input and Screen Output – 2”.
1. What is the output of the following code?
b) Syntactical Error
c) The format specifier does not exist
Explanation: The format specifier %i is used to display integers. But sprint keeps the input arguments in a character array. Hence, the output is ‘91293’.
2. What is the output of the following code?
Explanation: 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.
3. What is the output of the following code?
sprintf(‘%d %d %d’,.1, .2.3)
b) ‘1.000000e-01 2.000000e-01 3.000000e-01’
c) 1.000000e-01 2.000000e-01 3.000000e-01
Explanation: 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?
c) [12 ]
Explanation: The input to the disp command is a cellular array. Cellular arrays are displayed within . Output: 
5. What is the output of the following code?
c) 0 0
d) 1 1
Explanation: 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?
Explanation: %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
Explanation: 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?
P= 234View Answer
Explanation: 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
Explanation: We can create a character array using the sprintf command and we can use format specifiers in the sprintf command. So, the final character array can be printed by giving that variable as an input to the disp command.
10. What is the output of the following code?
Explanation: The string argument, given as an input to the disp() command, can be given within “” and hence, the output is 12 only. There rest of the options are incorrect.
11. What is the output of the following code?
Explanation: Since we haven’t mentioned a precision while representing .123, the output will be upto 6 places of decimal. Thus, the output is .123000 and there is no error.
12. What is the size of ans variable after the following code?
sprintf('23 23') ans= ’23 23’
a) 14 bytes
b) 10 bytes
Explanation: The number of elements in the character array, given as input are 5. They are ‘2’, ‘3’, ‘ ’, ‘2’, ‘3’. Hence, the total size will be 10 bytes as each element will take 2 bytes. The ‘’ are not taken as elements.
Sanfoundry Global Education & Learning Series – MATLAB.
To practice all areas of MATLAB Assessment Questions, here is complete set of 1000+ Multiple Choice Questions and Answers.
Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!