MATLAB Questions and Answers Part-17

1. What is the output of the following code?
T=rand + i*rand
a) A complex number whose real and imaginary parts are whole numbers
b) A complex number whose real and imaginary parts are fractions
c) A complex number whose real and imaginary parts are in the interval (0,1)
d) A complex number whose real and imaginary parts in the interval [0,1]

Answer: c
Explanation: The rand function generates numbers in the interval (0,1). Hence, the complex number whose real and imaginary parts are in the interval (0,1)is correct. A complex number whose real and imaginary parts in the interval [0,1]would’ve been correct if the command randi(0,1) was used.

2. What is the output of the following command?
randi(10 5,3)
a) Error
b) A 5*3 matrix of random numbers
c) A 5*3 matrix of random numbers in the interval [1,10]
d) A 583 matrix of random numbers evenly spaced in the interval [1,10]

Answer: a
Explanation: The above command is syntactically incorrect. A 5*3 matrix of random numbers in the interval [1,10] would’ve been the correct option if the code was randi(10,5,3) but one comma is missing between 10 and 5. This will give an error in MATLAB.

3. What is the output of the following code?
rng(‘shuffle’)
a) Control the random number generator
b) Control the filter design process
c) Control the IIR design process
d) Control the FIR design process

Answer: a
Explanation: Commands like rand, randi use the random number generator to generate the random numbers. This can be controlled by using the rng command. The above command does not generate a result in the command window but it changes the working of the rand command.

4. The randn command generates random numbers by following a _________
a) Normal distribution
b) Normalized Normal Distribution
c) Uniform Distribution
d) Bernoulli’s Distribution

Answer: b
Explanation: The Normalized normal distribution is used to generate random numbers random numbers using the randn command- this is the inbuilt working of the randn command. Hence, Normalized Normal Distribution is correct.

5. The output of the following code
randn[(3,4)]
a) Error
b) A random set of numbers from the normalized normal distribution
c) A random set of numbers from the range 3,4
d) A random set of numbers from the normal distribution ranging from 3 to 4

Answer: a
Explanation: The above command is syntactically incorrect. The input to the randn command should be within parentheses.

6. What is the class of the variable r after the following code is run?
r=rand(1,4,’double’)
a) Array
b) Single
c) Double
d) Integer

Answer: a
Explanation: After the execution of the following command, r will be a 1*4 vector with a class of double. This is because we have defined the class within single inverted commas as double. R is a row vector so, and hence, it is an integer array. But it’s class is double.

7. The linspace command generates pseudorandom numbers.
a) True
b) False

Answer: b
Explanation: The linspace commands generate numbers which are equidistant from each other and contain within a closed range. Hence, they are not pseudorandom numbers and the statement is false.

8. What is the error in the following command?
randn[Inf]
a) Error due to Inf
b) Syntactical error
c) Misspelled command
d) No error

Answer: b
Explanation: The first error MATLAB sees is that there is a syntactical error. This will be the error which will pop up in the command window. Hence, the Syntactical error is correct. Error due to Inf would’ve been correct if there was no syntactical error. MATLAB returns the first error it founds.

9. The range of numbers returned by the following code is:
randperm(1,9)
a) [1,9]
b) (1,9)
c) [1,9)
d) (1,9]

Answer: a
Explanation: The randperm command would return 9 unique values with the permutation of numbers ranging from 1 to 9. The interval is inclusive of both 1 and 9. Hence only option [1,9] is correct. The rest are wrong.

10. What is the output of the following code?
randi(10,1,9)
a) 9 unique values with replacement in the interval [1,10)
b) 9 values with replacement in the interval (1,10]
c) 9 unique values with replacement in the interval (1,10)
d) 9 values with replacement in the interval [1,10]

Answer: d
Explanation: We have mentioned the desired number of values within the interval. Hence MATLAB will return only 9 values. The randi command generates random numbers within the specified interval which are not unique.