You are on page 1of 4

1.

You are given the function:


function [ ] = weirdMultiplier(num)
fprintf(num = %f \n, num);
if num <= 100
weirdMultiplier(2*num);
end
end
A user enters the command weirdMultiplier(25) in MATLABs command window. How many recursive calls are made? (That is, how many
calls are made to the function weirdMultiplier from within itself?)
(a) 2
(b) 3
(c) 4
(d) 25
(e) none of the above
2. Consider the following function:
function [result] = RecursiveFunction(n,p)
if n==p
result=0;
elseif n>p
result=RecursiveFunction(n-1,p)+1;
elseif p>n
result=RecursiveFunction(n,p-1)-1;
end
What is the value of x after the following is executed?
x = RecursiveFunction(3,6)
(a) 3
(b) 9
(c) 3
(d) 9
(e) 0
3. Consider the following lines of code:

t = linspace(0, 10, 100);


y1 = 2*t;
y2 = 5*(t.^2);

% Line 1
% Line 2
% Line 3

figure(1);
plot(t, y1, ro, LineWidth, 2)
grid on;
plot(t, y2, bo, LineWidth, 2)

%
%
%
%

Line
Line
Line
Line

4
5
6
7

After executing the code above, how many lines will be present on Figure
1?
(a) 1 line (y1 vs t)
(b) 1 line (y2 vs t)
(c) 2 lines (y1 vs t and y2 vs t)
(d) 0 lines
(e) 2 lines (t vs y1 and y2 vs y1)
4. We would like to analyze data which varies over 2D (x, y) space. Before
making a plot, we execute the following code.
x = linspace(-50, 50, 200);
y = linspace(150, 300, 200);
[xx, yy] = meshgrid(x, y)
After executing the code, which of the following statements is true?
(a) Every element of x is equal to at least one element of xx.
(b) x has 200 elements and the size of xx is 200 200.
(c) x contains values ranging from -50 to 50, while xx contains values
ranging from -50 to 300.
(d) Both (a) and (b) are true
(e) All of the above are true
5. After the following code is executed, what are the values of s1 and s2,
respectively?
X = [1 2 3; 4 5 6];
Y = [6 5; 4 3; 2 1];
Z1 = X*Y;
Z2 = Y*X;
s1 = size(Z1)
s2 = size(Z2)
(a) [2 2] and [2 2]
2

(b) [2 2] and [3 3]
(c) [3 3] and [2 2]
(d) [2 3] and [3 2]
(e) [3 3] and [3 3]
6. Consider the following lines of code:
A = [2 1; 0 1];
I = [1 0; 0 1];
B = (A+I)^2-(A+I).^2;
What is the value of B?
(a) [0 4;0 0]
(b) [0 0;0 0]
(c) [0 -4;0 0]
(d) [2 6;4 2]
(e) Error: Matrix dimensions dont agree.
7. The root mean square error (RMSE) is a common metric used to evaluate
a regression model based on how well it fits the observed data. It is defined
by
v
u
m
u1 X
t
(yi yi )2
RM SE =
m i=1
where yi , i = 1, 2, ..., m represent values predicted by the regression model,
and yi , i = 1, 2, ..., m represent the observed data points. In MATLAB, y
is represented by the 1 m array y_hat and y is represented by the 1 m
array y. To compute RMSE using MATLAB, you could use which of the
following commands?
(a) RMSE = sqrt(mean((y_hat-y).^2))
(b) RMSE = sqrt((1/length(y))*sum((y_hat-y).^2))
(c) RMSE = (mean((y_hat-y).^2))^(0.5)
(d) All of the above
(e) None of the above
8. Consider the following piece of code, given data points stored in the column
vectors x and y.
A = [ones(size(x)), x];
alpha = A\y
alpha =
-4.33
3.56
3

According to MATLAB, what is the equation of the least squares best fit?
(a) y = 4.33 + 3.56x
(b) x = 4.33 + 3.56y
(c) y = 4.33x + 3.56
(d) y = 4.33x + 3.56x2
(e) y = 4.33x

You might also like