Department of Computer Science

University of Illinois at Urbana-Champaign

Computer Science 101: Mid Term Exam I (60 minutes)

 


Name:                                                                         NetID:
 

Lab Section:                                                                Date: September 28, 2005

 


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.

DO NOT WRITE IN THIS SPACE

Section

Possible Score

Score

Grader

1

6

 

 

2

6

 

 

3

6

 

 

4

6

 

 

5

8

 

 

6

11

 

 

7

8

 

 

8

8

 

 

9

6

 

 

10

6

 

 

11

8

 

 

12

8

 

 

13

6

 

 

14

6

 

 

15

8

 

 

16

4

 

 

17

4

 

 

18

10

 

 

Total

125

 

 

 

 

 

 

1.      Write a single MATLAB expression that creates a row vector with

values [1000  999  998  997  3 ...  2   1]  ,(1000 to 1 in descending order).  

You must use the : operator in your answer.

 

 

>> x = ____________________________________________________________

 

 

 

 

 

 

 

 

2.      Write a single MATLAB expression that takes the vector x from problem #1

and reverses the order of all but the first and last elements. The result should be

      [ 1000 2 3 4 ... 997  998 999 1 ].

 

 

>> y = ___________________________________________________________

 

 

 

 

 

 

 

 

 

 

3.      Write a single MATLAB expression that creates a row vector of 10

evenly spaced values from 1 to e2. You must use the exp function in your answer.

 

 

>> ________________________________________________________________

 

 

 

 

 

 

 

 

 

4.      Write the single MATLAB command to find all the roots of 8x4 + x2 + 7. (Don't write the roots)

>> ____________________________________________________________________________

 

 

 

 

 

 

5.      Circle (all) possible functions that would correctly return the sum of the values of its two input parameters:

 

a)     
function sum(y,z)

   y + z;

 



b)        
function x = sum(y,z)

   sum = y + z;


 

 

c)     
function x = sum(y:z)

     x = y + z;


 

 

d)        
function x = sum(y,z)

     x = y + z;

 

 

6.      Write a Matlab function named cm2i that converts centimeters into inches. The function cm2i has one input parameter z  a value in centimeters, and the function returns the equivalent value in inches. Do not include comments in your function.
Use the following conversion formula:

 

1 inch = 2.54 cm

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

7.      Which of the following is TRUE about MATLAB functions? (Circle all correct answers):

 

a)      A function always return a value;


b)      A function always contains a non-zero number of parameter variables;


c)      A function has local variables only, unless the global command is used;


d)      A function can return at most one array(vector or matrix).

 

 

 

 

 

 

 

 

 

 

 

 

 

8.      Let A be a 10x10 matrix. Write the output of the following Matlab commands. If the result would produce an error then write ERROR. Hint: the size function returns the number of rows and the number of columns in an array. For example
>> size(A)
         10 10


 >>
size(A .^ A)

  _____________________________________________

 
 >>
size(A([1,5,7],[2,3,1,5]) * A(1:3,1:3))

 _____________________________________________
 

 >>
size(A(2:8,:) * A(end:-1:1,:))
 
_____________________________________________


>>
size(A * A)

_____________________________________________

9.      Write the output the following Matlab commands produce.

>> C = [2;3];
>> D =  C' * C + ones(2)


__________________________________________________

>> E = [1;2];
>> F =  [E .* E , ones(2,1)
]



__________________________________________________

 

10.  Complete the Matlab code by filling in the blanks to plot the graph for the function

           

     

   Use 100 equally spaced values for each of x and y from -3 to 3.

 

   >> x = linspace(-3,3,100);

   >> y = x;

   >> [ XVAL  , YVAL ] = meshgrid( _________ , _________)


   >> ZVAL = _____________________________________________________

   >> surf( _____________ , _____________ , ZVAL)
          

 

11.  A digital voltmeter records the voltage values starting at time t = 0 and ending at time  t = 100 (seconds). The voltmeter makes a voltage reading every .01 seconds and stores all the voltage values in the variable V.
Assume that the Matlab variable
V holds the voltage values recorded by the voltmeter.

a) Write the Matlab commands to plot the voltages
V (on the vertical axis) versus time t ( on the horizontal axis).

    t = _________________________________

        
 plot(_________________, _________________)

b) Compute the definite integral of the function V(t) from t = 0 to 100 seconds. Remember  

     that we do not have an explicit formula for the function V(t) so choose the correct Matlab   

     function to perform the definite integral.

         integral  = ______________________________________________

12.  Which of the following are valid Matlab commands that can be used to compute the definite integral of the function y = sin(ex) on the interval (-,)? Circle all correct answers.

       a)
            >> quadl('sin(exp(x))', -pi, pi)


       

b)
     >> quadl('sin(ex)', -pi, pi)



       c)
            >> quadl('sin(exp)', -pi, pi)
      


       d)
            >> quadl('sin(exp(x))', linspace(-pi,pi,1000))

 

 

 

 

 

 

13.  Assume the function named f is defined below and saved in the file f.m