# MATLAB Questions and Answers – Functions

«
»

This set of MATLAB Multiple Choice Questions & Answers (MCQs) focuses on “Functions”.

1. What is the name of a primary function?
a) Name of M-file
b) Name of Script File
c) Name of Help file
d) Name of Private-File

Explanation: M-files are text files which can be created in any text editor and it contains the description of a program written for a particular purpose. The program description is mainly a self-contained set of statements which comprises of lines of codes or different kinds of functions. Thus the name of the primary function, where the program description is described, is the M-file.

2. Predominantly, what are the two kinds of errors in MATLAB programs?
a) Syntax and runtime
b) Syntax and logic
c) Logic and runtime
d) Syntax and algorithmic

Explanation: Usually, there are two kinds of errors in any programming language. They are syntactical errors and runtime errors. Syntactical errors arise due to the programmer not following language specific syntaxes. Runtime errors rise due to faulty logic decided for the program, the error is called Runtime error.

3. Which code would you use to find the value of the function f?

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
`f(x)=sin(x) + cos (x) + tan (x) at x = π/4`

a) sin(45)+cos(45)+tan(45)
b) sin(pi/4)+cos(pi/4)+tan(pi/4)
c) sin(45®)+cos(45®)+sin(45®)
d) sind(45)+cosd(45)+tand(45)

Explanation: sin(45) would return 0.8509 and sin(pi/4) would return 0.7071. They won’t return exact value of sin 45®. But sind(45) will return 1, this is a pre-defined function in MATLAB. The same goes for cosd(45) and tand(45).

4. If the program demands evaluation of multiple, only decimal, values of the same function, what is the better way amongst the following?
a) Using anonymous functions or inline functions
b) Using str2func
c) Using private functions
d) Using any function

Explanation: The function str2cat() returns floating point or decimal expressions for integer values or fractional values. Anonymous functions and inline return decimal values for fractional values but not for integer values. Hence, str2cat is preferable.

5. What are persistent variables?
a) Variables which retain values between calls to the function
b) Variables which help in calling a function
c) Variables which deal with functions
d) Variables global to the function

Explanation: Persistent variables help to retain the value which has been produced by a sub-function. They are local to the sub-function but MATLAB reserves permanent storage for them so they might seem like global variables. They are never shown in the workspace so they differ from global variables.

6. Variables need to have same name while being passed from the primary function to the sub-function.
a) True
b) False

Explanation: It is dependent upon the programmer to choose the variable name while establishing a connection between a global and a persistent variable. Essentially, one may choose to keep the variable name same to avoid confusion in case of multiple sub-functions. But it is not necessary if there are a small number of sub-functions.

7. Find the error in below code.

`>>f = inline('t.^4', 't');		g = inline('int(h(t), t)', 't');`

a) There is no function as inline
b) The error will be in defining g
c) The error will be in evaluating a numeric value for the function g
d) Syntactical error in defining g

Explanation: While defining an expression using the function inline(), the input string argument placed within a pair of apostrophe has to contain characters or variables along with arithmetic or logical operator. The function ‘int()’ will evaluate the integration of the function h(t). So the error will be in introducing the function h(t) within the string input.

8. Which command can be used for single step execution in debugging mode?
a) dbstep
b) dbstepin
c) dbstatus
d) dbcont

Explanation: The function ‘dbstep’ helps in single step execution while ‘dpstepin’ helps to enter into a function. The function ‘dbstatus’ helps to list all the breakpoints in a function while ‘dbcont’ helps to continue execution. These are the pre-defined debugging commands in MATLAB.

9. How do we access a global variable in nested functions?
a) Simply seek the variable from the primary function
b) Make a copy of the global variables from the primary function
c) Declare the variable within the function
d) Declare the variable as global

Explanation: Any global variable, in the primary function, if required to be accessed by a nested function- the variable needs to be declared with the global keyword within the function which requires access to it. This allows sharing the variable amongst the primary and other functions. If the variable is declared without using the global keyword, the variable might bring an error to the function.

10. How does MATLAB help in passing function arguments?
a) By call by value and call by reference
b) Only by call by value
c) Only by call by reference
d) By call by address

Explanation: Like C, MATLAB allows to pass almost all arguments by value. But, if the argument passed into the function is only for mathematical purpose then it is passed as a reference. This helps in saving the memory reserved for the original value of the argument.

11. Which line is treated as H1 line?
a) Comment line succeeding function definition
b) Comment line preceding function definition
c) Comment line after function
d) All lines before and after function definition

Explanation: When we have many functions nested in a primary function, it is a good practice to place comment lines which would help anybody to understand the purpose of the function- making the function more versatile. This comment line introduced right after the function definition is treated as H1 line. All other comment lines after or preceding the function definition won’t be returned as H1 line when the H1 line is sought in MATLAB.

12. Find the error returned by MATLAB:

`syms a x y;	a = xˆ2 + yˆ2;	   subs(a, [x,y], (2,3,4))`

a) There is no subs command in MATLAB
b) The subs command has syntactical error
c) The subs command has extra input arguments
d) The subs command has incorrect arguments

Explanation: If we want to substitute the variable with constants to get the value of ‘a’, we will have to introduce the constants within third brackets. The subs command is pre-defined in MATLAB and hence MATLAB will produce a syntactical error. It shows the first error it sees while compiling our code so it won’t show that the subs command has extra input arguments.

13. Predict the error, MATLAB will generate, in the following line of code:

` g = inline(‘3x+5*x’, ‘t’)`

a) No error
b) Syntactical error
c) Wrong arguments
d) Expecting (,{ or [

Explanation: MATLAB will generate no error. But if we try to find an integral value of the function defined within the string argument, MATLAB will generate an error saying the variable t is defined. This is because we have used ‘x’ as the only variable within the string input. We can change‘t’ to ‘x’ or change the variable ‘x’ in the string input to ‘t’.

Sanfoundry Global Education & Learning Series – MATLAB.

To practice all areas of MATLAB, here is complete set of 1000+ Multiple Choice Questions and Answers. 