C Se 200 Practice Midterm
C Se 200 Practice Midterm
•! Ask the TAs or the instructor if you have any problems. Use MATLAB only
to VERIFY the correctness of your hand-written answer.
•! This practice midterm gives sample practice problems for the Fall 2015
Midterm of CSE 200. They DO NOT cover the full extent of the material
you need to know but they will be similar to the questions on the formal
examination. They do not resemble the number of problems or problem
distribution along topics in the midterm exam. The time it takes to solve the
problem can differ from the midterm exam.
•! Show your work to receive maximum credit. Partial credit will be given, but
only if work is shown.
•! If we cannot read your hand writing, we cannot give you credit.
•! Circle, mark, or otherwise indicate your answer to each question.
•! Please keep your written answers brief and to the point. Incorrect or
rambling statements can hurt your score on a question.
•! If the question requires you to provide code, you must use correct MATLAB
syntax and semantics to receive full credit. We should be able to type your
answer into MATLAB and verify that it works.
Please mark for each statement whether it is true or false. Make sure your choice is clear.
Correct answers will count as 1 point; wrong answers will count as -1 point. The minimum total amount
of points for this problem is 0 points.
true false
1. For loops use a conditional statement to determine the number of iterations.
2. Else blocks do not require a conditional statement.
3. Using element by element operations when they are not required will cause an
error.
4. Helper functions are used to isolate recursion.
5. Function definitions require you to define an output.
6. The statement x == 7 will set x to have a value of 7.
7. If x is a matrix, x(:,:) can be used to address all of the rows and columns.
8. The plot3 command is used to generate planes in 3D space.
9. Anything that can be written with a loop can also be written using recursion.
10. The ~= operator tests to see if two values are not equal to each other.
Part II: Multiple Choice (xx points, 1 point per correct choice, wrong choices will count as -1 point,
The minimum total amount of points for every problem is 0 points.)
P1. Suppose x is a new variable, with the following Matlab statement, x = [-10: -1: -15; -2: 3]; How many
elements are generated in x?
(B) 10
(C) 12
(D) 5
(E) 6
P2. A matrix was generated using M = rand(10); Which of the following statements is not valid in
Matlab?
(A) M+M*2
(C) sin(M)
(D) exp(M)
(E) M(10,10)+M(3,6)
00007
00008
00009
00010
for i=7:10
fprintf(?????)
end
(A) fprintf('%5.5f\n', i)
(B) fprintf('%5.2d\n', i)
(C) fprintf('%05d', i)
(D) fprintf('%5.5d\n', i)
(E) fprintf('%d\n', i)
P4. Given the following Matlab code, what is the value of k after executing the code?
k= 0;
while k^0.5<k
k = k + 1;
end
k
(A) 0
(B) 1
(C) 2
(D) 3
Part III: Computations and Commands (xx points, xx points per blank)
x = [0 4 7 -1; 2 7 9 -5];
y = [8 4 0 10; 18 6 -5 2];
z = [4 3 9 -3];
u = (x + y) * 2;
v = x.^2 – 8;
w = x(1,:) .* z;
u = __________________________
v = __________________________
w = __________________________
x = __________________________
y = __________________________
P3. Given vectors x=[-1, 2, 3, -2], y=[0.2, 3.1, 0, -3] and z=[3, 0, 1, 0.1], provide answers to the
following operations
Answer: __________________________
(b)! x + ~y > z
Answer: __________________________
(c)! x == y ~=z
Answer___________________________
P3. Write the code to generate a 10 x 10 matrix of pseudorandom uniformly distributed independent
random numbers on the open interval (0,1).
Answer: ____________________________________________
(b)! Write the command that you would use to write the matrix Nums to a file named
myRandomNums.csv
Answer: ____________________________________________
P1. What is the output when executing the following Matlab code?
clear;
for i=1:5
for j=i:5
M(i, j) = i+j;
end
end
M(4, 3)
Answer: ______________________________________
P2. Using nested for-loops to generate a matrix that has elements shown below (without typing
the numbers explicitly):
Answer:
(b)! Write a single line of code that would create a matrix b where the first row of b goes from -5 to -
17 with a spacing of -3, and the second row of b is the third row of x.
Answer: ______________________________________
Part V: Functions and Control Statements (xx points)
Answer:
P2. Involving fprintf and nested for-loop commands, write a Matlab script to print the following
pattern:
1
12
123
1234
123456
1234567
Answer:
P3. Write a while loop that displays 'I like cats' on the command window 50 times.
Answer:
P4. Write a recursive function called minR that takes in a vector of values and returns the smallest value
in the vector. If the input vector has a length of zero, your function should return two empty vectors ([]).
NOTE: You may not use the built-in min function anywhere in your implementation.
Answer:
Part VI: Plotting (xx points)
P1. Fill in the following function so that it creates a mesh plot of the input vectors.
function milesPerGallon(m, g)
% m is a vector representing miles driven, g is a vector
% representing the gallons of gas used
end
x = 1:.1:20;
(a)! Write a few lines of code that would plot the functions y = x^2 + 2 and z = (x+5)^(1/2) on the
same plot.
(b)! Write a few lines of code that will plot the two functions y = x^2 + 2 and z = (x+5)^(1/2) on
separate plots, using the fplot command to generate the plots on the interval from 1 to 20.
P3. Briefly discuss the differences between a mesh plot and a surface plot. What does a mesh plot
generally look like? What does a surface plot generally look like? How do each of them use their unique
attributes to display data? Please limit your answer to 3 sentences.
P1. You are given the following function stored in myfunction.m in a folder in your MATLAB path:
function y = myfunction(x)
x=x/2;
y = x;
end
x = 1;
y = 0;
y = myfunction(x);
x = ________________________________________
y = ________________________________________
P2. You are given the following function stored in myfctn.m in a folder in your MATLAB path:
function myfctn(x)
global x;
x=x-5;
y = 0;
end
x = 10;
y = 1;
global x;
myfctn(x);
x = ________________________________________
y = ________________________________________
function y = mysteryFunction(x)
%The x input must be an array of numeric values
if isempty(x)
y = 0;
else
y = mysteryFunctionHelper(x);
end
end
function y = mysteryFunctionHelper(x)
if length(x) == 1
y = x;
else
y = x(1) + mysteryFunctionHelper(x(2:end));
end
end
(a)! Label the base case and the recursive step in the code above.
(b)! How do you know that this function will eventually terminate, i.e., hit the base case?
(c)! Evaluate mysteryFunction([2 6 -5 11 2]) and state the result. What is it the function
computes?
(d)! Rewrite the function using loops such that it computes the same value as the original.
function y = mysteryFunctionI(x)
end