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 =
_______4 3 2 1___________________
>> mysterious = vec( end : -2
: 1 )
mysterious =
________________4 2_________________________________
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 = _____vec / d
or vec ./ d______(; optional)________________
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.
_________vec = vec'_________________(; optional)______________________________
3. Write a single Matlab command that does the following:
a) Save all the workspace variables to the file la3.mat.
______save la3 or save
la3.mat_____________________
b) Delete only the variables x and y from the workspace.
________clear x y _______________________
c) Clear the command window.
_________clc_____________________________________________
4. Write the result of executing the following Matlab command:
>> vec
= linspace(5,5,5)
vec =
_______5 5 5 5 5________________________
5. Write a single Matlab command that creates a vector of 100 equally spaced values from -2*π to π.
_______linspace( -2*pi, pi, 100)______________________________________
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 =
_______5 2 6____________________________
b)
>> clear
>> x = 4;
>> y = 5;
>> z = 6;
>> y = fnc(3)
y =
___________1 2 3 2 3 0________
7. Write
Matlab command(s) to create a 4 by 100 matrix mat with the values as follows:

[ 1:100 ; 1:100 ; 1:100 ; 1:100 ] may use linspace(1,100,100)
rather than 1:100
or
cumsum(ones(100,4))'
8. Fill in the blank below with a single Matlab command that plots the function cos(x) on the interval