Department of Computer Science
Computer Science 101: Mid Term Exam I (60 minutes)
![]()
Name: NetID:
Lab Section: Date: February 21, 2008
![]()
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.
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 |
7 |
|
|
|
3 |
6 |
|
|
|
4 |
5 |
|
|
|
5 |
6 |
|
|
|
6 |
9 |
|
|
|
7 |
8 |
|
|
|
8 |
6 |
|
|
|
9 |
9 |
|
|
|
10 |
9 |
|
|
|
11 |
8 |
|
|
|
12 |
4 |
|
|
|
13 |
4 |
|
|
|
14 |
6 |
|
|
|
15 |
7 |
|
|
|
16 |
6 |
|
|
|
17 |
4 |
|
|
|
18 |
15 |
|
|
|
Total |
125 |
|
|
|
|
|
Exam Scoreà |
|
A
1.
The following commands
are typed in the Matlab command window. Write the results Matlab returns.
>> vec = [ 1 2 3 4];
>> mystery = vec( 4 :
-1 : 1 )
mystery =
________________________________________________________
>> mysterious = vec( end
: -2 : 1 )
mysterious =
________________________________________________________
2.
Consider the following
Matlab commands:
>> d = 5;
>> vec = [ 1 2 3 4];
a)
Write a single
Matlab command to divide vec by d and assign the result to a variable named
vec_d.
vec_d = _____________________________________________
b)
Write a single
Matlab command that assigns to vec the transpose of vec. After your command
is executed vec
should be a column vector.
________________________________________________________
3. Write a single Matlab command that does the following:
a) Save all the workspace variables to the file la3.mat.
______________________________________________________
b) Delete only the variables x and y from the workspace.
______________________________________________________
c) Clear the command window.
______________________________________________________
4. Write the result of executing the following Matlab command:
>> vec =
linspace(5,5,5)
vec =
___________________________________________________________
5. Write a single Matlab command that creates a vector of 100 equally spaced values from -2*π to π.
__________________________________________________________
6. Assume that file fnc.m has the following content:
function y = fnc(z)
y=1;
scr;
y = [y, x, z, 0];
and that the file scr.m has the following content:
x = 2;
y = [y, x, z];
Answer both questions a) and b) below. Write down the output that Matlab displays after you type each of the following commands:
a)
>> clear
>> x = 4;
>> y = 5;
>> z = 6;
>> scr;
>> y
y =
________________________________________________________________
b)
>> clear
>> x = 4;
>> y = 5;
>> z = 6;
>> y = fnc(3)
y =
_______________________________________________________________
7. Write
Matlab command(s) to create a 4 by 100 matrix mat with the values as follows:

8. Fill in the blank below with a single Matlab command that plots the function cos(x) on the interval
[-8,5] .
_________________________________________________________________
9. Fill in the blank below with the result of the following commands:
>> cumsum(diff([1,2,3,4]))
ans =
________________________________________________________________
>>
y = [ -1 ;
-2 ; -3]
>> [fliplr(y),flipud(y)]
ans =
_______________________________________________________________
10. Given the following matrix, write the output of each command.
>> matrix = [ 1 2 3; 4 5 6; 7 8 9]
matrix =
1 2 3
4 5 6
7 8 9
a) >> matrix(end,:)
_________________________________________________________
b) >> cumsum(matrix)
________________________________________________________
c) >> matrix(:,3) = -1
________________________________________________________
11. Fill in the blanks to create the following matrix named banded .

>> a = diag(1:4 , __________
);
>> b = diag(
__________ , 2);
>> c = diag(1:2,
___________);
>> banded =
_____________________________;
12. Given the following matrix named icky

write the output of the following Matlab command.
>> icky(1:3:4,1:2:4)
___________________________________________________________
13. Circle
each of the following commands that plots a graph of the function
on the interval
[-20 , 20]. There
may be more than one correct answer.
You may assume that the variable x has been assigned the following values:
>> x = linspace(-20,20,1000);
a)
plot( x,
'sin')
b)
plot(x, sin(x))
c)
plot('sin')
d)
plot(sin(x))
14. 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.
___________________________________________________________________
15. Write a single Matlab command to find all the roots
of the function
. Do NOT calculate the actual roots. Just write the
Matlab command that would produce the correct roots.
___________________________________________________________________
16. Write Matlab command(s) to compute the following integral.
Do NOT calculate the value of the integral, just write the Matlab command(s) that would produce the correct value.
17. Given the values for the matrix A and the vector C as shown below fill in the blank with the correct value that Matlab should return when computing A*B.


>> B = A \ C
>> A*B
___________________________________________________________________
18. Write
a complete Matlab function named equal
that takes two row vectors of the same length, named x and y, as inputs and
returns the count of all the values that are equal at the corresponding
positions in the vectors x and y . Use x and y as the input
parameters and count as the output
variable. Do not write any comments.
After you write your function it should give the following results:
>> equal([1 2 3] , [1 2 3])
ans =
3
>> equal([2 1 3] , [1 2 3])
ans =
1
>> equal([3 1 2] , [1 2 3])
ans =
0
The code for your function should work correctly with the input of any two row vectors that are of the same length and not just for the three examples shown above.