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 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.
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. 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 |
|
5 |
|
|
|
|
2 |
5 |
|
|
|
3 |
5 |
|
|
|
4 |
5 |
|
|
|
5 |
7 |
|
|
|
6 |
5 |
|
|
|
7 |
9 |
|
|
|
8 |
9 |
|
|
|
9 |
9 |
|
|
|
10 |
8 |
|
|
|
11 |
8 |
|
|
|
12 |
8 |
|
|
|
13 |
11 |
|
|
|
14 |
4 |
|
|
|
15 |
11 |
|
|
|
16 |
4 |
|
|
|
17 |
6 |
|
|
|
18 |
6 |
|
|
|
Total |
125 |
|
|
|
|
|
Exam Scoreà |
|
A
ans =
____________0____5______10_____15_____20______________________
>> ________who___________or______________whos_____________________
>> _________clear
x_____________________________________________
a) The
semicolon is required at the end of each statement.
b)
The
semicolon is optional at the end of statements, and if present, will suppress
the output from that statement.
c) The
semicolon is only required for statements in scripts or functions, but is
optional for statements typed in main MATLAB window.
d) [1 ; 2
; 3 ] is identical to [1 , 2 , 3 ] in Matlab.
e) [1 ; 2
; 3 ] is identical to [1 2 3] in Matlab.
>> ________roots([
1 0
0 1])______________________________________________
>>
_______polyval([ 1 0
4 -1 2],
1:100)______________
F = ma
For
example, when the user enters the following command at the Matlab prompt,
>>
F = force(10,2);
F will
have the value 20. However your code for the function force should work
not just for the values 10 and 2 but for any scalar values for m and a.
Do not include comments in your code.
function
F = force(m,a)
F = m * a; ( or F = m .* a;)
jump(x)
= 1 if
x > 0
jump(x)
= 0 if x <= 0.
For
example, when the user enters the following command at the Matlab prompt,
>> y = jump(3);
y will
have the value 1. The function jump
has one input parameter x. The output of the function is either 1 or 0
depending on the input value x and the rules above. Do not include comments in
your code.
function y = jump(x)
y = x > 0;

>> K = ___100:-1:1___or___linspace(100,1,100)_________;
>> mat = diag( ______K___________ , _______0___________);
>> mat( __:_____ , ___end__or__100______ ) = ______100_____
;
11. Given
>> m = [ 1 2 ; 3 4
; 5 6 ];
Recall that size(m)
returns a vector containing the number of rows and colums in m. For
example,
>> size(m)
ans = 3 2
Fill in the blanks below with the results that
Matlab would produce. If the command would produce an error then write ‘ERROR’.
>> size(m * m’)
ans = ______3______3____________________________
>> size(m’ * m)
ans = ______2______2____________________________
>> size(m – m)
ans = ______3______2____________________________
>> size(m .* m)
ans = ______3______2____________________________
12. 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 v should contain the right-hand side values from the equations:
x + y = 5
z - y = 7
2x + y + 3z = 17
>> A = ___[ 1
1 0 ; 0 -1 1
; 2 1 3];_______________
>> v = ___[5
; 7 ; 17] ;___or___[ 5 7 17]’
;_____________
Write a single Matlab command to solve
the system of equations using matrix
A
and vector v. Do
not write the solution to the system of
equations, just the Matlab command that would give the
solution.
>> __________A \ v ;__________________________
13.
Write the values that Matlab would produce after
executing each of the following commands.
>> v = 2*ones(4,1)
2
2
2
2
>> w = cumsum([1 2 3 4])
_________1
3 6 10__________________________________
>> x = diff([1 2 3
4])
_________1 1 1__________________________________
function result = fun(x)
global g
g = g + x;
result = g;
The following sequence of commands are entered at the Matlab prompt:
>> global g
>> g = 0;
>> i = fun(10);
>> g = g + 2;
>> j = fun(9);
>> g
Circle the correct output that Matlab produces. Circle only one choice.
a) g = 19
b)
g = 21
c) g = 11
d) g = 2
>> A = [ 1 2 3 ; 4 5 6 ; 7 8 9]
A =
1
2 3
4 5
6
7 8
9
Write the output of the following commands. If the command results in an error, write ‘ERROR’.
>> B = A(1:2:3,1:2:3)
1 3
7 9
_____________________________________________
>> C = sum(A)
_____________12_____15______18_____________________
>> D = [ A , [10 11 12] ]
_____________ERROR________________________________
>> A( 2, : ) = [13 13 13]
1
2 3
13 13
13
7 8
9
____________________________________________
>> x = [ 1 2 3 4 5
6 7 8 9 ];
>> y = [ 5 1 3 9 7
2 4 5 6 ];
Which of the following produce the
same figure as plot(x,y)?
Circle each correct answer. There may be
more than one correct answer.
a)
plot(1:9, y)
b) plot(y, x)
c)
plot(x, [5 1 3 9 7 2 4 5 6])
d)
plot(y)

>> x = linspace(____0_____ , ____2_____ , 2000);
>>
y = _______x.^2______________;
>> area = trapz( ______x_______________ , _______y____________)
>> fplot(____2*exp(2*x)
– log(x)_ , ___[ 2 , 3 ]______)