This set of MATLAB Multiple Choice Questions & Answers (MCQs) focuses on “Commands for Parsing Input and Output – 1”.
1. What is the working of the nargin keyword?
a) Returns the number of inputs to a function as an output of the same function
b) Gets assigned to a value equal to the number of inputs of a function
c) Gets assigned to a value equal to the number of inputs+1 of a function
d) Gets assigned to a value equal to the number of inputs-1 function
View Answer
Explanation: The nargin command takes a value which is equal to the number of inputs given to the function.
2. What is the working of the varargout command?
a) Returns a 1*N cellular matrix
b) Returns a 1*N cell array of outputs
c) Returns a 1*N cell array of inputs given
d) Doesn’t exist
View Answer
Explanation: The varargout command returns a set of output elements in a 1*N cell array. It doesn’t return the number of inputs.
3. What is the working of the nargout command?
a) Gets assigned to a number which is equal to the number of outputs-1 sought from a subfunction
b) Gets assigned to a number which is equal to the number of outputs-1 sought from a function
c) Gets assigned to a number which is equal to the number of outputs sought from a function
d) Gets assigned to a number which is equal to the number of outputs+1 sought from a function
View Answer
Explanation: The nargout command gets assigned to a number which is equal to the number of outputs sought from a function. It will control the nature of outputs produced by the function where it is present.
4. What is the working of the varargin command?
a) takes 1*N inputs after the defined inputs in a function
b) takes N*1 inputs after the defined inputs in a function
c) takes N*N inputs after the defined inputs in a function
d) takes 1*1 inputs after the defined inputs in a function
View Answer
Explanation: The varargin command takes 1*N inputs after the defined inputs to a function. It allows the function to take many input arguments and control the output of the function.
5. What is the output of the following command?
function s= average(varargin) celldisp(varargin) s=sum(varargin) end p=average(1:3)
a) Error in sum
b) Error using varargin in function definition
c) Error calling the average function
d)
1 2 3 p=6View Answer
Explanation: The input is acellular array called varargin. The input to the sum function in the average function is should be varargin{1} to indicate which element of the array varargin is to be taken.
6. What is the output of the following command?
function wish= ful(p,varargin) celldisp(p) wish=sin(varargin{1}) end p=ful(1:3)
a) Error in calling the function
b) Error in celldisp
c) Error in the function definiton
d) Error in the wish variable
View Answer
Explanation: p is treated to a function which takes inputs as cellular arrays. In the above code, if the input to the ful command was {1:3}, the output would have been
p{1}= 1 2 3
Hence, the error is in using celldisp command, only disp command is to be used.
7. What is the output of the following command?
function mad= heart(p,varargin) disp(p) wish=sin(varargin{0}) end p=heart(1:3,0)
a) Error in wish command
b) Error while calling heart command
c) Error while calling disp command
d)
1 2 3 wish=0View Answer
Explanation: The arguments to a cell array start from the index 1. Here, varargin{0} will create an error and hence, error in wish command is correct.
8. What is the output of the following command?
function mad= rush(p,varargin) disp(p) wish=cos(varargin[0]) end p=rush(1:3,pi/2)
a) Error due to element index
b) Error due to syntax of varargin
c)
1 2 3 wish=0
d) Error due to disp()
View Answer
Explanation: The first error MATLAB finds is that the index of the element of the varargin array is given within []. This creates the error and MATLAB returns the first error it invokes The next error is the fact that there are no elements in varargin whose index number is 0.
9. What is the output of the following code?
function mad= rush(p,varargin) disp(p) wish=cos(varargin{1}) end p=rush(1:3,Inf)
a) Error due to Inf
b) -1
c)
p{1}= 1 2 3 wish=0
d)
p{1}= 1 2 3 wish=NaNView Answer
Explanation: There is no error due to Inf. The disp function will display 1 2 3 due to the input vector 1:3 and wish will be assigned to NaN since cos(Inf) is NaN.
10. What is the output of the following code?
function maha= orchid(polo,varargin) disp(polo) wish=sum(varargin{1,2}) end p=rush(-1:1,Inf)
a) Logical error in varargin
b) Syntactical error in varargin
c)
p{1}=-1 0 1 wish=0
d) No output
View Answer
Explanation: Varargin is a 1*N cellular array. The index number given as input to the varargin command looks like we are calling the element in the 2nd row and the 1st column which is not there. Hence, there is logical error in using the varargin keyword.
11. What is the output of the following code?
function [die,absdif] = BDO(y,x) die = y-x; if nargout > 1 disp('Boo !') absdif = abs(die); end end [p,q]=BDO(Inf,Inf)
a)
Boo! p=NaN q=NaN
b)
Boo! p=0 q=0
c) Error
d) p=NaN
View Answer
Explanation: Since the number of outputs sought are greater than 1, the if condition gets satisfied and ‘Boo !’ is printed. Inf-Inf will be NaN and hence both p and q will be equal to NaN.
12. What is the output of the following code?
function [die,af] = Bod(y,x) die = y*x; if nargout < 1 error('Boo !') else disp(‘Yo !’) af = abs(die); end end [p]=Bod(1,[1 -2])
a) Error with Boo! displayed
b)
Yo ! p=1
c)
Yo ! p=2
d)
Yo ! p=-2View Answer
Explanation: Only one output is sought from the Bod function. The first argument it returns is die=-2 and the else condition is also satisfied so Yo! will be printed.
13. What is the output of the following code?
function [die,af] = lola(y,x) die = y*x; if nargout < 1 error('Boo !') else disp(‘Yo !’) af = abs(die); end end p=lola(1,[1 -2])
a) Error with Boo ! written
b)
Yo ! p=-2
c)
Boo ! p=-1
d)
Boo ! p=1View Answer
Explanation: Since the number of argument sought are less than 1, nargout is less than 1 and the if condition is satisfied so the function terminates with an error.
14. A function ______________
a) can be run in the command window by defining it in the window itself
b) can be run in the command window by defining it in a cpp file
c) can be run in the command window by defining it in a script file
d) can be run in the script file by defining it in the command window
View Answer
Explanation: A function has to be saved as a M-file before it can be run in the command window. We will have to take MATLAB to the directory where it is saved and it will run the function from the directory itself.
15. What is the output of the following code?
function c = addem(ap,q) p=nargn-1 switch p case 2 c = ap*q; case 1 c = sin(q) otherwise c = 0; end addem(1,2,3,4,5,6,7,8)
a) 2
b) Error due to too many inputs
c) 28
d) Error due to nargn
View Answer
Explanation: The first error MATLAB invokes is the fact that too many values are given as input. So it returns that as input. The next error would be that the nargin keyword is misspelled as nargn. But MATLAB returns the first error only.
Sanfoundry Global Education & Learning Series – MATLAB.
To practice all areas of MATLAB, here is complete set of 1000+ Multiple Choice Questions and Answers.