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 10 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. 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 |
|
4 |
|
|
|
|
2 |
8 |
|
|
|
3 |
6 |
|
|
|
4 |
6 |
|
|
|
5 |
6 |
|
|
|
6 |
8 |
|
|
|
7 |
8 |
|
|
|
8 |
9 |
|
|
|
9 |
6 |
|
|
|
10 |
6 |
|
|
|
11 |
6 |
|
|
|
12 |
8 |
|
|
|
13 |
6 |
|
|
|
14 |
6 |
|
|
|
15 |
6 |
|
|
|
16 |
6 |
|
|
|
17 |
12 |
|
|
|
18 |
8 |
|
|
|
Total |
125 |
|
|
A
1. Assume the vector v has been
created as follows:
>>
v = [6 7 8 9];
Write the output of the following Matlab command:
>> v([3 1 4 2])
______________________________________________________
2. Write a single Matlab
command to produce the vector [ 1 3 5 7
999] , all the odd integers
from 1 to 999:
>> ____________________________________________________
3. Assume that x has been
declared a symbolic value as follows:
>>
syms x
Write a single Matlab command to find the second derivative of
the function sin(x).
Do not write the answer -sin(x), write the Matlab command that will
give you the correct result.
>> ___________________________________________________
4. Write a single Matlab command
to find the root of cos(x) nearest to 2.
Do not write the root.
>>
_________________________________________ ________
5. Write Matlab command(s) to create
a 100 by 100 matrix named mat with the vector
[1 2 3
98 99 100] (a vector with the integer values from 1 to 100) along on
the main diagonal
and 100 along the bottom row and zeros everywhere else:

6. Given vectors v1, v2 :
>> v1 = [1; 2; 3];
>> v2 = [1 2 3];
which of the following is a legal expression,
that is, will NOT produce a Matlab error? There may be
more than one legal expression. Circle
each expression that is legal. Do not compute the value of the
expression.
a)
v1 + v2
b)
v1 v2
c)
v1 * v2
d)
v1 .* v2
7. Given
>> mat1 = [ 1 2 ; 3 4 ; 5 6 ];
write the output the following Matlab commands produce.
>> size(mat1)
ans = ________________________________________
_
>> mat1(1:2
, : )
ans = ________________________________________
8. Transform the following system 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 right hand side of the equations.

>> A = ____________________________________________________
>> c = ____________________________________________________
Write a single Matlab command to solve the
system of equations using matrix
A
and vector c.
>> ________________________________________________________
9. Assume the vector x has been
created as follows:
>>
x = [4 2 -1 3];
Write the output of the following
expressions.
>> min(x)
ans
=
______________________________________________________________________
>>
max(x.^2)
ans
=
_____________________________________________________________________
10. Assume that matrix A has been
declared as follows:
>> A = [1 2 3; 2 4 6];
Write the result of the following
expression.
>> A(1,:) .* A(2,:)
ans =
_____________________________________________________________________
11. Given a vector v, write a single
Matlab expression to obtain the truncated vector of v, that is,
the
vector whose ends are truncated from the
original vector v.
(For example, if the vector v is [1 2 3 4 5]
then the truncated vector is [2
3 4], but your
Matlab expression should work for any
length vector.)
>> ____________________________________________________________________
12. Given
>> x = [1 3 5 7 9 11 13];
which one of the expressions below (choices a thru d) is equivalent to the following Matlab command?
>> y
= [13 9 5 1];
That is, which of the Matlab commands below assigns the vector of values [13 9 5 1] to y?
There may be more than correct answer. Circle each correct answer.
a)
y =
x(end:-2:1);
b) y = x(length(x):-2:1);
c) y = x([13 9 5 1]);
d) y = x(13:-4:1);
13. Write a single Matlab command to find all the roots of the polynomial y = x3 - 5x2 + 5x .
(Don't write the roots.)
>> ____________________________________________________________________
14. The file data.m contains the script,
x = 2;
y = 1;
The file f.m contains the function code,
function y = f(x)
global
z
z = [z, x];
y = x * 3;
At the Matlab prompt you type the following commands. Fill in the blanks with the correct values that
Matlab would return.
>>
clear
>> global z
>> f(1);
>> who
________________________________________________________________
>>
data
>> who
_________________________________________________________________
15. Write the values of v, w and x after entering the following sequence of Matlab commands.
>> v = 2*ones(4,1)
v =
_____________________________________________________
>> v
= [ 1 2 3 4];
>> w = cumsum(v)
w =
_____________________________________________________
>> w = [5 4 3
2 1];
>> x = diff(w)
x
=
______________________________________________________
16.
Using the built-in function quadl
complete the Matlab command by filling in the blanks, to compute
the definite integral :

>> quadl( _______________________________________
, ______________ , _____________ )
17. Assume that you have a script in the file named tower_data.m with the contents:
HL = 10;
HR = 20;
Write a complete Matlab function named appendvalues. The function has one input parameter, h
that will hold a row vector. The function appendvalues will run the script named tower_data
and then prepend the value of HL to the vector h and append the value HR to h. The function
appendvalues has one return value, this modified row vector.
For example, at the Matlab prompt if we assigned h the following values then we would get the
results as shown below:
>> h = [ 3
4 5
4 3];
>> newh = appendvalues(h)
newh =
10 3
4 5
4 3 20
Of course the code you write for appendvalues should work not just for the example above but for
any vector h.
18. The function named diff_L has the following properties:
· diff_L has two input parameters x and y that hold vectors of equal lengths.
· diff_L calls the function xy_length and passes this function x and y.
·
diff_L
computes and returns the difference between the target value L = 10 and the value returned from the
function xy_length.
Assume that you have a function named xy_length in the file xy_length.m with contents:
function calc_length =
xy_length(x,y)
calc_length = sum( sqrt( diff(x).^ 2 + diff(y).^ 2 ) );
For example, at the Matlab prompt if we assigned x and y the following values then we would get the
results as shown
below:
>>
x = [ 0 1 2];
>>
y = [ 2 0 3];
>>
delta_L = diff_L(x,y)
delta_L =
4.6017
Which of the choices below satisfy the properties for diff_L as described above and would return the
results as shown above? There may be more than one correct answer. Circle each correct answer.
a)
function delta_L = diff_L(x,y)
L = 10;
xy_length(x,y)
delta_L = L - calc_length;
b)
function delta_L = diff_L(x,y)
L = 10;
delta_L = L xy_length(x,y);
c)
function delta_L = diff_L(x,y)
L = 10;
calc_length = xy_length(x,y);
L - calc_length
d)
function delta_L = diff_L(x,y)
L = 10;
calc_length = xy_length(x,y);
delta_L = L - calc_length;