Department of Computer Science
Computer Science 101: Mid Term Exam I (60 minutes)
![]()
Name: NetID:
Lab
Section: Date:
![]()
No questions
will be answered during this examination.
If you do not understand a question, read it again. If you still do not understand it, make reasonable assumptions and write them
down. (Points will be deducted if unreasonable assumptions are made.)
DO NOT CHEAT: Cheating includes not only copying from another person but
also allowing someone to copy from you.
Anyone who copies or allows someone to copy will receive a score of
zero. So be defensive and protect your
work.
This examination contains 9 pages including this page. Check that your copy is complete, and ask for a replacement if it is not. Do all your work on these pages. For your own protection, in case pages come apart, write your NetID at the TOP of each page before beginning work.
The number of points for a question is roughly proportional to the amount of time you may need for it. Dont spend too much time on any one question.
Do not forget to sign the attendance list and to write your signature on the line below:
_______________________________________________________________________
(If your exam is misplaced and you did not sign the attendance list then you
will receive a zero score for the exam.)
You may not use any electronic devices, book, notes or other references during this examination.
Section |
Possible Score |
Score |
Grader |
|
1 |
4 |
|
|
|
2 |
6 |
|
|
|
3 |
5 |
|
|
|
4 |
5 |
|
|
|
5 |
9 |
|
|
|
6 |
10 |
|
|
|
7 |
8 |
|
|
|
8 |
6 |
|
|
|
9 |
6 |
|
|
|
10 |
10 |
|
|
|
11 |
6 |
|
|
|
12 |
6 |
|
|
|
13 |
6 |
|
|
|
14 |
6 |
|
|
|
15 |
6 |
|
|
|
16 |
12 |
|
|
|
17 |
6 |
|
|
|
18 |
8 |
|
|
|
Total |
125 |
|
|
1. You type the following commands at the Matlab prompt:
>>
x = 4;
>>
y = 5;
>>
clear
>>
y = 6;
>>
y
ans =
6
>>
who
Write down the variables listed from the who command:
______y_____(or ans y )_________________________________
2. a) Write
Matlab statement(s) to plot the graph for the function y = x2 + cos(x) using the Matlab plot
command. Use
100 equally spaced values of x from -1 to 1.
x = linspace(-1,1,100);
y = x.^2 +cos(x);
plot(x,y)
b) Use the Matlab function fplot to plot the graph for the function cos(x) for x from -π to π.
>> ___fplot(cos,[-pi,pi])_or _ fplot(cos(x),[-pi,pi]) _____
3. Write the result of typing the following Matlab command at the Matlab prompt:
>> y = -1 : 0.5 : 1
______-1 -.5 0 .5 1___________________________
4. Assuming that you have typed (at the Matlab prompt)
>> vec = [1 2 3 4 5 6];
Which of the following commands will produce the output [ 1 2 3] ? (Circle any/all correct answers):
a) vec(1:3)
b) vec([1 2 3 ])
c) vec[1:3]
d) vec(1:3,1:3)
e) vec([1,2,3])
5. Assume that you have typed and saved the following function do_something:
function result = do_something(x)
y = x + 2;
z = 3
result = y * z
Suppose the following commands are typed at the Matlab
prompt:
>> clear
>> x = 1;
>> who
Write down the variable(s) listed from the who command:
________x___________________________________
You proceed to type more commands at the Matlab prompt:
>> result = do_something(10)
>> who
Write down the variable(s) listed from the who command:
___________x__result_______________________________________
6. Write a complete Matlab function named avg that finds the average of two numbers. The function avg has two input parameters x and y where x and y can be either scalars or vectors of the same length.
For example, after you write your function avg and then type:
>> avg(2,4)
ans =
3 % since (2+4)/2 = 3
or
>> avg([2 3] , [ 4 5])
ans =
3 4 % since (2+4)/2 = 3 and (3+5)/2 = 4
Do not include comments.
function result = avg(x,y)
result = (x+y)/2;
7. Write the MATLAB command(s) to create the following 100x100 matrix:
1 1 1 1
1 2 1 1
1 1 3 1
1
1 1 1 100
(Hint: you may use ones , diag and
the vector 0:99 )
ones(100) + diag(0:99,0) or ones(100,100) + diag(0:99,0)
8. Given the row vector y
of length 1000, complete the MATLAB command below, to zero the elements in even
positions. That is if y has the values [y1 y2
y3 y4
y5
y1000] then after you write
your command y now has the values [y1 0 y3 0 y5
0].
Assume that values have previously been assigned to y.
>> y( __2:2:1000__ ) =
____0_______or zeros(1,500)________ ;
9. Write MATLAB command(s) to find all the roots of the polynomial:
![]()
(Do not calculate the roots by hand)
roots([1 0 1 1 1 1])
10.
Let P be a 3 x 4 matrix. Which of the following are valid operations?
Circle any/all correct answers.
P*P
P*P' P'*P P'*P' P.*P
11. Write the output produced by the following MATLAB commands. If an error would occur, write
ERROR!
a)
>>
A=[ 1 2 3 ; 4 5 6 ; 7 8 9];
>>
A(1:2 , 1:2:3)
1 3
4 6
_________________________________________________________
b)
>>
[4.*eye(2,2), [ 2
2]]
4 0 2
0 4 2
________________________________________________________
12. Write a single Matlab command to solve the following equation for x near -1 :
x - sin(2x)
+ 1 = 0
>> ________fzero(x-sin(2*x)+1,-1)_____________________________________________
13. Fill in the blanks with the correct Matlab commands to draw a surface plot of the function
z = x3y2
over the rectangle in the x-y plane where x varies from -3 to 3 and y varies from
-3 to 3. You must use the functions meshgrid and surf
You should start with the following commands:
>>
x = linspace(-3,3,100);
>>
y = x;
>> [ XMAT , YMAT] = ____meshgrid(x,y)______________________________;
>> ZMAT = ____(XMAT.^3) .* (YMAT.^2)_______________________________;
>> ___surf(XMAT, YMAT, ZMAT)____________________________________;
14. Given the following Matlab commands and the corresponding output, fill in the blank with the correct Matlab output.
>> fzero('foo', 5)
ans =
7
>> foo(7)
ans =
________________0________________________________________
15. Transform the following set of linear equations
into a Matlab matrix and vector. Matrix "A" should contain the
coefficients of the equations and column vector "C" should contain
the values on the right hand side of the equations. The system thus looks like
A* B = C .
3x
+ 8y + 1z = 62
2x - 1y - 3z = -5
4x + 1y - 2z = 21
>> A = ________[3 8 1;
2 -1 -3 ; 4 1 -2]_______________
>>
C = _____[62;
-5; 21]__or [62 -5 21]_____________________
Now write the Matlab statement to solve the system of linear equations.
>> B = _____A\C______________________________________
16. Write the Matlab commands to approximate
the integral of

a) You
must use the trapz function with 1000
equally spaced points.
>> ____x = linspace(0,1,1000);____________________________
>> ____y = x.^2 +sin(x) + 2.^x ___________________
>> ____trapz(x,y)__________________
b)
Evaluate the same integral above but now use the Matlab quad (or quadl) function.
>> ____quad(x.^2+sin(x)+2.^x,0,1)_______
c) Fill
in the blanks below to use Matlabs symbolic functions in order to compute the
indefinite
integral,

>> syms x
>> int(
___x^2__or x^2 or x.^2__ , ______x________
)
17. Assume we have created a file named data.m
with the contents:
r = [ 1 2 ];
y = [1 2 3];
The following commands are then typed at the
Matlab prompt. Fill in the blanks with the correct
output that
Matlab would display.
>> clear
>>
r = 5;
>> data
>> r
r
=
______1____2_______________________
>> who
______r____y____________________________
18. A drunken guy starts walking from position 0
on the x-axis. Every second, he either steps one foot forward ( +1 ) along the
x-axis, or one foot backward ( -1 ) along the x-axis or he stands still ( 0 ). Each of the three actions is equally likely to be performed
by the drunken guy.
Fill in the blanks to complete the function named walk. Given n, the function walk outputs a vector named result showing his position at the 1st, 2nd, 3rd... nth second.
For example, if n = 4 and his steps were [ -1 0 1 -1] then the walk function would return
result = [-1 -1 0
-1] which is a cumulative total of the steps. The values for the steps are
generated by the Matlab function rand
which generates random real numbers between zero and one.
function result = walk(n)
% In lab we used the
command
% floor(1+rand(1,n)*6)
% to
generate n random integers from the set {1,2,3,4,5,6}
%
Complete the code by filling in the blank to generate n random numbers
from the set { -1, 0, 1}.
steps = floor( __-1+rand(1,n)*3___________);
% Now compute
a cumulative total for the steps.
% Fill in the blank with the correct name of
a Matlab function.
result = ________________cumsum_(steps);