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 8 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. Don’t 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 |
6 |
|
|
|
2 |
6 |
|
|
|
3 |
6 |
|
|
|
4 |
6 |
|
|
|
5 |
8 |
|
|
|
6 |
11 |
|
|
|
7 |
8 |
|
|
|
8 |
8 |
|
|
|
9 |
6 |
|
|
|
10 |
6 |
|
|
|
11 |
8 |
|
|
|
12 |
8 |
|
|
|
13 |
6 |
|
|
|
14 |
6 |
|
|
|
15 |
8 |
|
|
|
16 |
4 |
|
|
|
17 |
4 |
|
|
|
18 |
10 |
|
|
|
Total |
125 |
|
|
1.
Write a single
MATLAB expression that creates a row vector with
values [1000 999 998 997
3 ...
2 1]
,(1000 to 1 in descending order).
You must use the : operator in your answer.
>> x = __________1000:-1:1__________________________________________________
2.
Write a single
MATLAB expression that takes the vector x from problem #1
and reverses the order
of all but the first and last elements. The result should be
[ 1000 2 3 4 ... 997 998 999 1 ].
>> y = ______[x(1),x(999:-1:2),x(1000)]__________________________________
or
>> y = ______[1000,2:999,1]__________________________________
3.
Write a single
MATLAB expression that creates a row vector of 10
evenly spaced values from 1 to e2. You must use the exp function in your answer.
>> __________linspace(1,exp(2),10)________________________________________
4.
Write the single
MATLAB command to find all the roots of 8x4 + x2
+ 7. (Don't write the
roots)
>>
_________roots([8 0 1
0 7] )_________________________________________
5. Circle (all) possible functions that would correctly return the sum of the values of its two input parameters:
a)
function sum(y,z)
y + z;
b)
function x = sum(y,z)
sum = y + z;
c)
function x = sum(y:z)
x =
y + z;
d)
function x = sum(y,z)
x = y + z;
6. Write
a Matlab function named cm2i
that converts centimeters into inches. The function cm2i has one input parameter z
a value in centimeters, and the function returns the equivalent
value in inches. Do not include comments in your function.
Use the following conversion formula:
1 inch =
function y = cm2i(z)
y
= z ./ 2.54 ;
7. Which of the following is TRUE about MATLAB functions? (Circle all correct answers):
a)
A function always return a value;
b)
A function always contains a non-zero number of
parameter variables;
c) A function has local variables only, unless the global
command is used;
d) A function can return at most one array(vector or matrix).
8. Let A be a 10x10 matrix. Write the output of the following
Matlab commands. If the result would produce an error then write ERROR. Hint:
the size function returns the number of rows and the number of columns in an
array. For example
>> size(A)
10 10
>> size(A .^ A)
_______10____10__________________________________
>> size(A([1,5,7],[2,3,1,5]) * A(1:3,1:3))
________ERROR_____________________________________
>> size(A(2:8,:) * A(end:-1:1,:))
________7___10__________________________________
>> size(A * A)
________10___10__________________________________
9. Write the output the following Matlab commands produce.
>> C = [2;3];
>> D = C' * C + ones(2)
14 14
_____14__ 14_____or___[14 14 ; 14 14]__________________________________
>> E = [1;2];
>> F = [E .* E , ones(2,1)]
1 1
4 1
_______________or________[1 1 ; 4 1]___________________________
10. Complete the Matlab code by filling in the
blanks to plot the graph for the function

Use 100 equally spaced values for each of x and y from -3 to 3.
>> x = linspace(-3,3,100);
>> y = x;
>> [ XVAL
, YVAL ] = meshgrid( __x_______ , __y_______)
>> ZVAL = ____(XVAL.*YVAL)./exp(XVAL.^2+YVAL.^2)_____________________
>> surf( ___XVAL____ , __YVAL_____ ,
ZVAL)
11. A digital voltmeter records the voltage
values starting at time t = 0 and ending at time t
= 100 (seconds). The voltmeter makes a voltage reading every .01 seconds and
stores all the voltage values in the variable V.
Assume that the Matlab variable V holds the voltage values recorded by the
voltmeter.
a) Write the Matlab commands to plot the voltages V (on the vertical
axis) versus time t ( on the horizontal axis).
t = _____0:.01:100____________________________
plot(_________t________, _____V____________)
b) Compute the definite integral of the function V(t)
from t =
0 to 100 seconds. Remember
that
we do not have an explicit formula for the function V(t) so choose the correct
Matlab
function
to perform the definite integral.
integral = ________trapz(t, V)___________________________