Department of Computer Science

University of Illinois at Urbana-Champaign

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

 


Name:                                                                         NetID:
 

Lab Section:                                                                Date: 9/30/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 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.



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

6

 

 

2

6

 

 

3

5

 

 

4

5

 

 

5

6

 

 

6

7

 

 

7

7

 

 

8

9

 

 

9

7

 

 

10

8

 

 

11

8

 

 

12

9

 

 

13

8

 

 

14

5

 

 

15

6

 

 

16

9

 

 

17

8

 

 

18

6

 

 

Total

125

 

 

 

 

Exam Scoreà

 


A

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

>> clear

>> x=4;

>> who

       _____________________________________________________

 

 

 

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

a)  >> z = 0:0.3:1

   z =

       _____________________________________________________

 

 

b)  >> w = linspace(3,8,2)

   w =

       _______________________________

 

 

 

 

 

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

 

>> x = [1 2 3 4];

>> y = [5 6 7 8];

>> z = [x(end:-1:end-2),y(2:3)]

 

   z =

       _____________________________________________________

 

 

 

 

4.      If  X = [ 1  3  5  7  9  11];

Write a Matlab command to display:

flipX =

9  7  5  3

Your answer must use X or you will not receive any credit.


>> flipX = ___________________________________________________

 

 

 

 

 

5.      Given the code for the function named rolldice as shown below:

function c = rolldice(n)

         % function c = rolldice(n)

         % rolldice: simulate the roll of a six sided die.

         % returns a random integer between 1 and 6 inclusive.

        

         c = ceil(6*rand(1,n));

 

               How would you call the rolldice function to simulate 4 die rolls?

 


>> _____________________________________________________

 

 

 

 

 

6.      Write the results Matlab returns when you type the following commands in the command window.

 

a)  >> x = ceil([1.9   3.4   -4.9   -5.3])

 x =

 

____________________________________________________________

b)  >> y = diff([2    4   -5   -6])
   y =

____________________________________________________________

 

 

 

 

 

 

 

7.      Given A=[1; 2], B=[3,4], circle the result Matlab will return when you type the following

command in the Matlab command window. There is only one correct answer.

 

>> A*B

 

 

a) ERROR


 

b) 1


 

c) 2 2




d) 3 4

6 8

 

 

 

 

 

 

 

8.      Complete the Matlab code below by filling in the blanks to solve the following linear system of

equations:

     100x + y + z = 1

     x + 20y + z  = 20

     x + y + 50z  = 10

where x,y and z are variables. We consider the problem mathematically as A*b = c .

 

            >> A = ____________________________________________________;



>> c = ____________________________________________________;  



     >> b = ____________________________________________________;

 

 

 

 

 

 

9.      Given that Mat =

2     4      6

3     10    14

5     7     10

 

 

circle the result Matlab will return for the following Matlab command typed in command window.

There is only one correct answer.

>> Mat(1,:) + Mat(:,2)'

     

a) 6    14    13


b) ERROR


c) 5    14    20


d) 6    13    12

 

 

 

10.  Given that A = [2 4 6] and B=3, which Matlab expression below would you fill in the blank to give you the column  vector shown below?

>> ___________________

     ans=
 

        6

        12

        18

 

Please circle each correct answer. (Note that there may be more than one correct answer.)

 

a) A'*B



b) A'.*B



c) A*B'



d) (A*B)'

 

 

11.  Fill the the blanks below to plot function: f(x) = sin2(x) between -π and π inclusive. Note that the function is fplot and NOT the plot function.


      >> fplot( ___________________, _____________________);

 

 

 

 

12.  Plot the 3-D surface described by the equation, z = x + y. In the region in the x-y plane from x = -4 to 4 and y = -5 to 5

>> x = linspace( _________________, _________________, 100);

>> y = linspace( _________________, _________________, 100);                                                                    >> x = linspace(___, ___, 11);

                                                                         >> y = linspace(___, 5, 9);

     >> [xMAT, yMAT] = meshgrid(x, y);

     >> zMAT = ______________________________________________ ;

     >> surf(xMAT, ________________________, ________________________)


 

 

 

13.  Given the code for the function named func as shown below,

        

            function out = func(x, y)
     out = x + y;

            circle each correct way to call the function named func. By correct way we mean any way that

            does not produce a Matlab ERROR. (Note that there may be more than one correct answer.)


           
a) >> result = func(1)



     b) >> result = func([1 2], [3 4])



     c) >> func(2, 1)

 

 
     d) >> func(1+2, 3/4)

 

 

 

 

14.  There is a symbol in Matlab such that when it is typed within a function, all the following text on that line is ignored by Matlab. This line is called a comment. Circle the answer choice for the symbol that will create a comment in Matlab. There is only one correct answer.

a) #

b) %

c) “

d) $

 

 

 

15.  Match the following incomplete lines of code with the appropriate function name that would NOT produce an error when typed in the Matlab command window. You may assume that the sin and deriv functions are previously declared functions and work without error.

a) >>_______(@sin, 0, pi)             1) quadl


b) >>_______([-1 0 1], [1 0 1])       2) ode45



c) >>_______(@deriv, [0, 500], 0)      3) trapz

 

 

 

 

 

 

16.  Complete the function named find_x by filling the in blank. The function find_x has two input arguments, x and y that will hold vectors of equal length. The function find_x  returns the  x value(s) whose position equals the maximum value of y.

For example, given x and y below,


>> y = [  1   0   5   0   3   0   4   0   5    0  ];

>> x = [ .1  .2  .3  .4  .5  .6  .7  .8  .9  1.0  ];

 

then calling the function find_x produces the following result.

 

>> result = find_x(x,y)

       result =

                 0.5 

Of course your code should work correctly for any choice of vectors x and y of equal length.

 

function result = find_x(x,y)

 

result = ____________________________________

 

17.  Given the vector p below,

 

            >> p = [  2  3  4  5  2  4  2  ];

 

write the output produced by typing the following Matlab command in the command window. If the command produces an error write ERROR.

 

>> x = p( p ~= 2 )

   x =

 ___________________________________

 

 

 

 

18.  Given the vectors p and y below,

>> p = [  1  0  0  0  1  1  1 ]
>> y = [  0  1  1  1  0  0  0 ]

 

 

write the output produced by typing the following Matlab command in the command window.

If the command produces an error write ERROR.

 



>> x = y(p)


          x =   ____________________________________________