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 |
4 |
|
|
|
5 |
6 |
|
|
|
6 |
6 |
|
|
|
7 |
4 |
|
|
|
8 |
9 |
|
|
|
9 |
4 |
|
|
|
10 |
4 |
|
|
|
11 |
8 |
|
|
|
12 |
9 |
|
|
|
13 |
10 |
|
|
|
14 |
4 |
|
|
|
15 |
8 |
|
|
|
16 |
9 |
|
|
|
17 |
9 |
|
|
|
18 |
13 |
|
|
|
Total |
125 |
|
|
1. Fill in the blanks
below using Matlab expressions to flip the values of y = [ 1 3 5 7 11].
>> flipy =
y( _____________ : _______________ : _______________ )
flipy =
11 7 5 3 1
2.
Write the output the
following Matlab commands produce.
>> x =
linspace(100,200,300);
>> x(1)
ans =
____________________________________________
>> length(x)
ans =
_____________________________________________
>> x(end)
ans =
_____________________________________________
3. Write the output the following
Matlab commands produce.
>> x = 1:
>> x(x(1)+1)
ans =
____________________________________________
>>
x(length(x))
ans =
____________________________________________
4.
Write the output the
following Matlab commands produce.
>> p = [1 0 5 -4];
>> polyval(p, 2)
ans =
_______________________________________________________
5. Given a matrix:

fill in the blanks below to change A , so that it looks as below:

>> A = [ 1 1 1 1; 1 1 1 1; 1 1 1 1; 1 1
1 1];
>> A( __________ , ___________
) = ____________________________
6.
Given the Matlab
matrix A ,
shown below, write the output that each of the following commands would
produce.
>> A = [0 1 2 3; 4 5 6 7; 8 10 11 12]
>>
size(A(1:2:3 , 1:3))
______________________________________________________________
>>
size(A( : , : ))
______________________________________________________________
>>
size([A; ones(1,4)])
______________________________________________________________
7.
Which of the following is a correct function
definition line (first line) for a function that has two input parameters? The parameters will hold vectors.
The function returns two values, the smallest value found in the first parameter
and the largest value found in the second parameter. Circle all correct
answers.
a) function min,max = minmax(first,
second)
b) function
min,max = minmax([first second])
c) function [min,max] =
minmax(first, second)
d) function [min,max] = minmax([first, second])
8.
Transform the following set of linear equations into
a Matlab matrix and vector. Matrix
should contain the coefficients of
the equations and column vector
should contain the
right hand
side of the equations.

>> A = ________________________________________________
>> c = ________________________________________________
Note that the above system of equations can
be written as A*X = C. Now write a single Matlab
command to solve the system of equations.
>> X = ________________________________________________
9.
Write a single Matlab command to find all the
roots of 8x4
+ x2 + 7. (Don’t write the roots.)
10.
Which of the following will produce a plot equivalent
to the one produced by this code:
>> y =
>> x = [5 2 3 7 3 7 9 1 4 6]
>> plot(y, x);
(Circle all correct answers.)
a) >> plot(
b) >>
plot(x, y);
c) >>
plot(x);
d) >>
plot(y);
11.
Using the built-in function trapz complete the following Matlab
commands by filling in the blanks
to compute the definite integral
of the function y = x2
– 2000 ln(x) on the
interval [1, 10],
using 1000
points.
>> x = ___________________________________________
>> y = ___________________________________________
>>
trapz( _________________, _____________________);
12. Assume you have saved the file ‘f.m’ with
the code:
function y = f(x)
y
= 2*x - 4;
a) Complete the Matlab command using fplot to plot this
function for x between 0 and 10.
>> fplot (____________________, ________________________);
b) Assuming that the fzero function works correctly, fill in the blank below with the correct
response Matlab would return.
>> f(fzero('f', 1))
ans =
_________________________________________
13. Fill in the blanks to complete the code
to plot the surface z = cos(x) + cos(y)
as a surface.
>> x = linspace(-5,5,100);
>>
y = linspace(-5,5,100);
>> [XMAT,YMAT] = ______________________________
>> ZMAT = _________________________________
>> surf(_____________, ______________, ________________)
14. Which of the following is the correct syntax for writing a comment in a function
or script in Matlab? Circle only one answer.
a) # this is a comment
b) // this is a comment
c) (this is a comment)
d) % this is a comment
e) None of the above
15.
Given the matrix A = [9 8 7; 6 5 4; 3 2 1]
compute the following values:
a) >> A( 2:3 ,
1:2:3)
____________________________________________________________
b)
Complete the Matlab command to replace the second column of the matrix A with the values in
the vector [-1 0 1] .
>> A( _________, ____________) = ________________________
16. The file data.m contains the script,
x = 1;
y = 2;
The file g.m contains the function code,
function y = g(x)
y = x + 1;
At the Matlab prompt you type the following commands. Fill in the blanks with the correct values that
Matlab would return.
>> clear
>> y = g(2);
>> who
______________________________________________________
>>
data
>> y
y
=
_______________________________________________________
>> who
_______________________________________________________
17. Fill in the blanks below to create the 100 x 100 matrix named mat with values [ 1 -1 1 -1 …1 -1] (100
values) down the main diagonal and zeros elsewhere,

>>
K = _________________________________________________ ;
>> mat = diag( _______________________ , _________________________);
18. Write a complete Matlab function named sums that has two parameters, a vector x and a positive
integer n . The function sums returns the sum of the first n values of x. You may assume that n
is less
than or equal to the length of x. Do
not include comments in your code.
Your function should work as the following
examples show.
>> x =
[ 1 2 3 4]
>> sums(x,3)
ans =
6
>> sums(x,1)
ans =
1
However your function should work not just for
x = [1 2 3 4] but for any vector x.