Department of Computer Science
Computer Science 101: Mid Term Exam I (60 minutes)
![]()
Name: NetID:
Lab Section: Date: 2/24/11
![]()
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.
Do not forget to sign the attendance list. 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 |
Deduction |
Grader |
|
6 |
|
|
|
|
2 |
6 |
|
|
|
3 |
8 |
|
|
|
4 |
8 |
|
|
|
5 |
6 |
|
|
|
6 |
6 |
|
|
|
7 |
8 |
|
|
|
8 |
6 |
|
|
|
9 |
6 |
|
|
|
10 |
8 |
|
|
|
11 |
7 |
|
|
|
12 |
8 |
|
|
|
13 |
6 |
|
|
|
14 |
6 |
|
|
|
15 |
8 |
|
|
|
16 |
6 |
|
|
|
17 |
8 |
|
|
|
18 |
8 |
|
|
|
Total |
125 |
|
|
|
|
|
Exam Scoreΰ |
|
A
1.
Given
A=[1;2], B=[3;4], circle the result MATLAB will
return when you type the following
command in the MATLAB command window. There is only one correct answer.
>> A*B
a)
ERROR
b)
1
c)
2 2
d)
3 4
6 8
2. You are given the
following system in the standard form A*b = c.
A =
b =
c = ![]()
Once A and c have been initialized
in the MATLAB workspace, circle the command that will allow you to solve for
the vector b. There is only one correct answer.
a) c/A
b) c./A
c) A\c
d) c\A
3.
Write the results Matlab returns if you type the following command at the Matlab prompt.
>> [linspace(-2,2,5), 3:2:7]
_____________________________________________________
4.
Given
the array A =
answer the following questions:
a)
Write
the results Matlab returns if you type the following
command at the Matlab prompt.
>> A(1:2,:)
_____________________________________________________
b)
Using
subscripting, write a single Matlab command to
extract all the values in the first column of A .
_____________________________________________________
a)
Write
the results Matlab returns if you type the following
command at the Matlab prompt.
>> A'
_____________________________________________________
5. Write the results Matlab returns for the following commands typed in the Matlab command window.
>> A = [1; 2; 3];
>> B = A.*A.*A
B =
_____________________________________________________
6. Write a single Matlab command to create the following matrix.
A
99 x 99 matrix as follows:

(Hint: you might use the ones function and the cumsum function.)
>> _____________________________________________________
7. For the given y and x values
below, find the x values that correspond to the smallest value in y. The answer
is [ 0.2 0.8 1.0]. Write the Matlab code to
produce this answer. Your answer should work for any values of y and x, assuming y and x are of the same size. Hint:
consider using subscripting and the Matlab function
min.
>> y =
[ 2 1 5 4 3 5
4 1 5 1 ] ;
>> x = [ .1 .2 .3 .4 .5 .6
.7 .8 .9 1.0 ] ;
>>r = _____________________________________________________
r =
0.2000
0.8000 1.0000
8.
Fill in the blanks to write a Matlab expression to display a row vector with 5 random
integers in the range from -10 to 10 inclusive.
>> random =ceil(______________+_____________* rand(1,5))
9. We have two functions named f1 and f2 saved in two separate files f1.m and f2.m as follows:
|
function y = f1(x) global g g = g + x.^2 + 3*x +1; y = g + 7;
|
function y =
f2(x) g = 50; y = g + 10*x;
|
The following sequence of commands is entered at the
Matlab prompt. Fill in the blanks with the correct
values that Matlab returns for the variables named a, b, and g.
>> clear
>> global
g
>> g = 0;
>> a = f1(5);
>> b = f2(g+2);
>> a
a = _____________________________________________
>>
b
b = _____________________________________________
>> g
g = _____________________________________________
10. Fill in the blanks below to use the built-in function fplot to plot the function
on the interval
[1,2].
>> fplot(__________________________________, _________________________________);
11. Write a single Matlab command to find a root of the function
![]()
near x = 2.
Do NOT calculate the actual root, just write
a Matlab command that would produce
the root.
>> ___________________________________________________________
12. We would like to plot the following 3D surface:
Fill
in the blanks to plot the surface above a grid of points in the x-y plane with
50 equally spaced
values for x between -5 and 5 and 50 equally spaced values
for y between -3 and 3 .
>> x = linspace(-5, 5, 50);
>> y = linspace(-3, 3, 50);
>> [XMAT ,YMAT] = meshgrid(_______________,___________________);
>> ZMAT = exp(____________).*(_________________+_________________);
>> surf(_______________,
________________, ___________________)
13. Given the code for the function named what ,
function
i = what(x, y)
i = x*(y >= x) + y*(x > y);
Write the output the function produces when it is called from the Matlab prompt with the arguments shown below.
>> z = what(2,3);
z =
_____________________________________________________
>> z = what(3,2);
z =
_____________________________________________________
>>
z = what(2,2);
z =
_____________________________________________________
14.
Consider a cylinder with length L and radius R. Fill in the blanks below to
complete the code for a function that calculates the volume and area of a cylinder.
Recall that for an opened ended cylinder area= 2* π *R*L
and volume= π
*R^2*L.
function [volume, area] =
cylinder(________,________)
__________________ = pi*R.^2.*L;
__________________ = 2*pi.*R.*L;
15.
Which of the following Matlab code fragments is a correct
way of computing an approximation of
the definite integral
of the function f(x) = sin(x) on the real interval [0,
π]? Circle each of the correct answers. (Note: there may be more that one
correct answer.)
a)
>>
x = linspace(0,pi,100)
>>
y = sin(x)
>>
trapz(x,y)
b)
>>
quadl(@sin,0,pi)
c)
>>
trapz(@x,'sin(x)',0,pi)
d)
>>
x = linspace(0,pi,100)
>>
y = sin(x)
>>
quad(x,y)
16.
Write the result Matlab returns when the following sequence of commands
are typed in
the Matlab command window.
>>
r = [-10 1 7
0 6 9];
>> s = [ 4 7 1
10 6 7];
>> t = s(r <=
min(s))
t =
_____________________________________________________
17.
Consider solving the following initial value ODE problem using the ode45 function:
where x(0)=1,for
0≤t≤10. We create the following .m
file called ex1.m:
function xprime=ex1(t,x)
xprime=x*sin(t);
Fill in the blanks below to solve the above ODE.
>>
[t,x] = ode45(___________,[_________
,__________], _______);
18. Complete the code below for a function named convert that converts grams to pounds and
pounds to grams. Recall that each pound is approximately equal to 453 grams.
function [pound,gram] = convert(pound,gram)
if isempty( ________________________________
)
pound = _______________________________ /453;
elseif isempty(________________________________)
gram = ________________________________ *453;
end