MATLAB Questions and Answers – User Input and Screen Output – 2

«
»

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?

advertisement
sprint(%i’,91293)

a) 91293
b) Syntactical Error
c) The format specifier does not exist
d) ‘91293’
View Answer

Answer: d
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’
advertisement
advertisement

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

Answer: a
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?

advertisement
advertisement
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

Answer: a
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?

advertisement
disp({12 })

a) [12]
b) 12
c) [12 ]
d) Error
View Answer

Answer: a
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

Answer: c
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

Answer: b
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

Answer: b
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=
        234
View Answer
Answer: a
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

Answer: d
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

Answer: c
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

Answer: a
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

Answer: b
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!

advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter