MATLAB Multiple Choice Questions 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
Answer: b
Clarification: 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
Answer: b
Clarification: 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
Answer: c
Clarification: 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
Answer: a
Clarification: 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=6
View Answer
Answer: a
Clarification: 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
Answer: b
Clarification: 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=0
View Answer
Answer: a
Clarification: 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()
