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?
sprint(‘%i’,91293)
a) 91293
b) Syntactical Error
c) The format specifier does not exist
d) ‘91293’
View Answer
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’.
Output: ‘91293’
2. What is the output of the following code?
sprintf(‘%E’,912)
a) 9.120000E+02
b) 9.120E+02
c) 9.1200E+02
d) 9.12000E+02
View Answer
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.
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) []
View Answer
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?
disp({12 })
a) [12]
b) 12
c) [12 ]
d) Error
View Answer
Explanation: 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?
size(‘’)
a) 0
b) Error
c) 0 0
d) 1 1
View Answer
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?
sprintf('%04.2f',2.9121)
a) ‘2.91210000000000000000’
b) ‘2.91’
c) 2.91
d) Error
View Answer
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?
sprintf(); disp();
a) Error due to disp
b) Error due to sprintf()
c) Both gives an error
d) No output is displayed
View Answer
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=disp(234)
a) Error
b) P=234
c) 234
d)
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
View Answer
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?
disp(“12”)
a) ‘12’
b) “12”
c) 12
d) Error
View Answer
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?
fprintf(“%f”,.123)
a) .123000
b) %f
c) Error
d) .123
View Answer
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
c) Error
d) 1*5
View Answer
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.
If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]