Department of Computer Science

University of Illinois at Urbana-Champaign

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

 


Name:                                                                         NetID:
 

Lab Section:                                                                Date: 2/25/10

 


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.

DO NOT WRITE IN THIS SPACE

Section

Possible Score

Deduction

Grader

1

5

 

 

2

6

 

 

3

8

 

 

4

8

 

 

5

8

 

 

6

6

 

 

7

4

 

 

8

6

 

 

9

6

 

 

10

5

 

 

11

8

 

 

12

6

 

 

13

8

 

 

14

8

 

 

15

8

 

 

16

8

 

 

17

5

 

 

18

6

 

 

19

6

 

 

Total

125

 

 

 

 

Exam Scoreà

 

A

1.      Write the results Matlab returns for the following commands typed in the Matlab command window.

a)  >> x1 = 0:2:4

   x1 =

       ____________________________________________

 

 

b)  >> x2 = linspace(0,4,2)

   x2 =

       ____________________________________________

 

 

 

 

 

2.      Write the results Matlab returns for the following commands typed in the Matlab command window.

 

>> x = [0, 1, 2];

>> x = x + x.*x

x =

       ____________________________________________

 

 

 

3.      Write the results Matlab returns for the following commands typed in the Matlab command window.

 

>> x = [0,3,9];

>> z = cumsum(x)

z =

       ____________________________________________

 

>> a = diff(diff(x))

a =

       ____________________________________________

 

 

 

4.      Write a single MATLAB command to find all the roots of  .

 

 

 

___________________________________________________________________________

 

 

 

5.      Circle each of the choices below that correctly defines a function named calc_v which has two input parameters,  v0 and time. The function calc_v returns the current velocity (in one dimension) based on a fixed acceleration, a = 2.354   . There may be more than one correct answer.

 

a)      function calc_v = velocity(v0,time)

a=2.354;

velocity=a*time+v0;

b)      function calc_v(v0,time,acceleration) = velocity

velocity=a*time+v0;  

c)      function velocity = calc_v(v0,time)

a=2.354;

velocity=a*time+v0;

d)    function velocity = calc_v(v0,time,acceleration)

velocity=a*time+v0;  

 

 

6.      Given the following command is typed at the Matlab prompt:

    

       >> a = [ 1  2  3; 4  5  6; 7  8  9];

 

      circle each of  the following statements that are correct Matlab responses. There may be more than
      one correct answer.

a) >> sum( a’ )  

   ans =

  6   15   24

 

b) >> sum( a )  

   ans =

  6   15   24

 

 

c) >> sum( sum( a ) )  

   ans =

  45

 

7.       Complete the Matlab command below by filling in the blanks. The command should evaluate the polynomial x5 -3x4 +5 x2 +x-6  at x = 3:10.

 

>> polyval( ________________________ , ________________________)

 

 

 

 

8.   Given two matrices A and B each being a 4 row by 4 column matrix, circle each of the following commands that will always return a 4 row by 4 column matrix of zeros. There may be more than one correct answer.

 

 

a) >> (A*B) – (B*A)

 

 

 

b) >> (A*eye(4)) - A

 

 

 

c) >> (B’)’ - B

 

 

 

 

 

9.      Assume that matrix A has been declared as follows:

 

>> A = [2 4 6; 5 7 9];

 

Write the result of the following expression.

 

>> A(1,:) .* A(2,:)

   ans =

 

 

 

_______________________________________________

10.  Answer both questions a) and b) below. Write down the output that Matlab displays after you type each of the following commands.

 

>> Q = [1,2,3,4,5; 6,7,8,9,10; 11,12,13,14,15; 16,17,18,19,20]

   Q =


     1     2     3     4     5
     6     7     8     9    10
    11    12    13    14    15
    16    17    18    19    20

a)

>> v=[1:3];

>> Q(v,2)

   ans =

 

 

 

_______________________________________________

 

b)

>> v = [2, 5];

>> Q(4, v)

   ans =

 

 

 

_______________________________________________

 

 

 

 

 

 

 

 

 

 

11.  Write a single Matlab command to compute the following sum,

 which represents the sum    .

 

>> result = __________________________________________________

 

 

 

12.  Circle each answer choice below that will NOT produce a Matlab error. Assume that there is a function named func whose code is located in a file named func.m.  The code for func is shown below. There may be more than one correct answer.

 

function out = func(x, y)

out = x + y

 

 

a)  >> x = 3;

>> y = 4;

>> out = func(x,y)

 

 

b)  >> x = 3;

>> y = 4;

>> z = func(x,y)

 

 

c)  >> x = 3;

>> y = 4;

>> out = func([x,y])

 

 

 

13.   Assuming that y = [ 2  1  2 ], circle each  Matlab expression below that will produce the result:

       [4   1   4]

There may be more than one correct answer.

 

a) >> y .^ 2


b) >> y .^ y


c) >> y .* y


d) >> y * y

 

 

 

14.  Circle each of the following answer choices that will plot a reasonable approximation of the graph of

y = sin(x) from - to π. There may be more than one correct answer.

 

 

a) >> x = -pi : 2*pi/100 : pi;

   >> y = sin(x);

   >> plot(x, y)

 

 

b) >> fplot('sin(x)', [-pi, pi]);

 

 

 

c) >> fplot(sin, -pi, pi);

 

 

 

d) >> fplot(@sin, [-pi, pi]);

 

           

 

 

15.  Write the results Matlab returns for the following command typed in the Matlab command window.

 

>> y = fzero('x-2',3)

   y =

 

a) 0

 

 

b) 1

 

 

c) 2

 

 

d) 3

 

 

 

16.  Circle the result Matlab returns for the following sequence of commands typed in the Matlab command window. There is only one correct answer.

 

>> x = [2 8 15 6];

>> y = [4 3 13 7];

>> z = max(y(x <= y ))

   z =

 

 

a) 2   6

    

 

 

 

b) 7

 

 

 

 

c) 13

 

 

 

 

d) 15

 

 

17.  Fill in the blanks below to approximate the definite integral:

 

 

>> x = linspace(_______________ , ________________ , 1000);

 

 

>> y = ____________________________________________;

 

 

>> area = trapz( __________________ , _____________________)

 

 

 

 

18.  Complete the Matlab code below by filling in the blanks to solve the system of linear equations shown below. You may assume that the system has a unique solution (so that Matlab does not display an error message). Do NOT write the solution to the system of equations, just write the code necessary for Matlab to find the solution.

 

 



 


>> A = [ 2   3   -7 ;  5   2   3; ______________________ ];

 

 

>> c = ______________________________________________________

 

 

     >> solution = ________________________________________________

 

 

 

 

 

 

 

 

19.  Assume that you are given the code for the function named main in the file main.m shown below.

 

function c = main(a)

 mp_data

 b = 2;
 c = a+b;

 

and you are also given the code for the script named mp_data in the file mp_data.m shown below.

 b = 4;

 c = 5;

 

Write the results Matlab returns for the following sequence of commands typed at the Matlab prompt.

 

>> a = 11;

>> b = 12;

>> c = main(1)

   c =

 

        __________________________________

>> b

   b =

      

        ___________________________________