This set of MATLAB Multiple Choice Questions & Answers (MCQs) focuses on “Commands for Parsing Input and Output – 2”.
1. The working of the varargin command is dependent on the nargin command implicitly.
a) True
b) False
View Answer
Explanation: The varargin command takes the values as a cellular array. It takes values independent of the output of the nargin keyword.
2. The nargout command is dependent on the number of outputs sought.
a) True
b) False
View Answer
Explanation: The nargout command acts according to the number of outputs sought from a function. It will change the output according to the value it gets assigned.
3. What is the output of the following command?
function eclairs= honey(p,q) narginchk(2,5) disp(‘Yo, G!’) c=q+p; end honey(1)
a) Syntactical Error
b) Yo, G!
c) input error
d) 2 3 4 5
View Answer
Explanation: The minimum number of inputs as suggested by the narginchk keyword is 2. But we have given only 1 input to the narginchk keyword. Hence, MATLAB gives an error in the input given to the function honey().
4. What is the output of the following command?
function p= marie(p,q) narginchk[2,3] disp(‘Yo, G!’) a=p+q end marie(1)
a) Syntactical Error in narginchk
b)
Yo, G! a=1
c) Input error in narginchk
d) 2 3 4 5…Inf
View Answer
Explanation: There has been syntactical error while using the narginchk command in the marie function. The input to this keyword is given within a parenthesis. If the input was given within parentheses, the output would’ve been
Yo, G!
a=1
5. What is the output of the following command?
function ca= ola(p,q) narginchk(0,1) disp(‘Stick!’) o=p-q; end ola(1,2)
a) o=-1
b) o=1
c) Error in inputs
d) Syntactical error while calling function
View Answer
Explanation: The narginchk command has limited the number of inputs to the ola function to 1. But the ola function has been called and 2 inputs are given to it. This gives an error while getting an output from the function.
6. What is the output of the following command?
function pu= hu(p,varargin) narginchk(0,1) disp(‘Stick!’) o=sin(varargin{1}); end ola(1,p/2)
a) Error due to line 2
b) Error due to line 5
c) Error due to syntax
d)
Stick! o=1View Answer
Explanation: The varargin command restricts the input to the function to only 1 argument. Hence, the function stops working and we will get an error due to line 2 of the above code.
7. The narginchk command can limit the size of which cellular array?
a) fun
b) rand(2)
c) eye(3)
d) varargin
View Answer
Explanation: The varargin keyword is used to take inputs to function as a 1*N cellular array. The value of N can be limited by uing the narginchk command. The rand(2) command generates 2 random numbers and the narginchk command has no effect on it. Same goes for the rest of the options since they are separate operations.
8. The nargoutchk command limits the length of which cellular array?
a) varargout
b) i(2)
c) random(69)
d) laplace(s)
View Answer
Explanation: The varargout command allows 1*N outputs to be asked from the function where it is situated. But the nargchkout command restricts the total number of outputs that can be sought from the function. So it really puts a limit to the value of N. Hence, the correct option is varargout.
9. The following operation results in?
narginchk(2,Inf)
a) Error due to Inf
b) Limits the length of nargin from 2 to Inf
c) Logical Error
d) Limits the length of varargin from 1 to Inf
View Answer
Explanation: The narginchk keyword, if present in a function, limits the input to the same function within a range. According to the above operation, if we use the varargin array while takin’ input in a function, the function will return an error if the number of inputs to the function are less than 2.
10. The following operation result in
Function p= ol(q,varargin) narginchk(45,Inf)
a) Error due to Inf
b) The range of the varargin array becomes (44,Inf)
c) The range of the varargin array becomes (45,Inf)
d) The range of the varargin array becomes [45,Inf]
View Answer
Explanation: The narginchk keyword limits the size of the varargin array to atleast 44 or the function won’t run. This is because the first input to a function does not go in to the varargin array but to q and hence, the minimum number of elements the varargin array has to take is 44.
11. The following operation results in
Function p= ol(q,varargin) narginchk(45,NaN) end
a) Error due to NaN
b) Syntactical error
c) Logical Error
d) Nothing happens
View Answer
Explanation: We have only written the code for the function but we did not call the function. So nothing will really happen although there is an error due to NaN. This doesn’t affect the saving of the function. It gets compiled at runtime.
12. What is the minimum number of arguments after the following operation occurs in any function?
narginchk(-2,0)
a) 0
b) 1
c) -2
d) Error
View Answer
Explanation: The minimum number of arguments to be given to the function will become 0. This renders the function totally useless. One has to be careful while giving input to the narginchk command.
Sanfoundry Global Education & Learning Series – MATLAB.
To practice all areas of MATLAB, here is complete set of 1000+ Multiple Choice Questions and Answers.