Department of Computer Science

University of Illinois at Urbana-Champaign

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 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

6

 

 

4

8

 

 

5

8

 

 

6

6

 

 

7

10

 

 

8

6

 

 

9

8

 

 

10

5

 

 

11

6

 

 

12

6

 

 

13

6

 

 

14

8

 

 

15

5

 

 

16

6

 

 

17

5

 

 

18

5

 

 

19

10

 

 

Total

125

 

 

 

 

Exam Scoreŕ

 

A

1.   Circle the correct output the following command produces when typed at the Matlab prompt:

 

>> linspace(2,5,4)

 

 

a) 2   2.5   3   3.5   4

 

 

 

b) 2   3   4   5

 

 

 

c) 2

 

 

 

d) 5   4   3   2

 

 

 

2.   Given that the variable x contains a row vector of numbers, write a single Matlab statement that would square each number in x and store the result in a variable y?



__________y = x .^ 2 ;  (dot required but semicolon not required)_____or  _y = x .* x ;______

 

 

 

 

3.      Write the results Matlab returns for the sequence of commands typed in the Matlab command window:

  >>clear;

  >>cat = [1  3  5  7  11];

  >>cat + 3;

  >>who

 

 

 

 

            _______ans      cat __(no deduction for cat  ans)_____________________________________

 

4.      Given that dog = [2 4 6 10 14], which Matlab expression below would give you the vector

[4 10]?  Please circle each correct answer.

 

 

a)  dog(2:2:5)

 

 

 

b)  dog(2:4)

 

 

 

c)  dog(4:2:10)

 

 

 

d)  dog(4:-2:2) 



 

 

 

 

 

5.      Write a Matlab expression to display a single random integer (not real) in the range from 0 to 5,including 0 and 5. You must use the rand and floor functions.

 

 

 

            _______floor( 6* rand(1)) __or   rand()____________________________________

 

 

 

 

 

 

 

 

 

6.       Write a single Matlab command to compute the second derivative of x4. (You must use the diff function. Assume x has been previously declared as a symbol, with the syms x command).

 

 

 

            ____diff(x^4,2) _or _diff(x.^4,2)_or __diff(x^4,2,x)_or _ diff(x.^4,2,x)_or__ diff(diff(x^4))_or_diff(‘x^4’,2) _but not_diff(‘x.^4’,2)___________________

 

 

7.      Write the results Matlab returns for the sequence of commands typed in the Matlab command window:

 

a)

>> x = sum([2  4  6])

   x =

 

 

 

            ___________12______________________________________________________________

 

 

b)

>> x = floor([ 1.7   3.6    .8 ] )

   x =

 

 

 

            ___________1            3          0_______________________________________

 

 

c)

>> x = min([6  4  2; 2  4  6])

   x =

 

 

 

            _________2       4       2______________________________________

 

 

d)

>> x = roots([1  0  -4])

   x =

 

 

            _______2        -2_______________________________________________________

 

 

e)

>> x = cumsum([2  4  6  8])

   x =

 

           

________2        6        12        20___________________________________________

8.      A Matlab programmer downloaded a Matlab file named avg.m from Internet, which contains a function named avg. But for some reason, the definition line (first line) of this function is missing. Read the program and comments and then write out the missing function definition line.

 

%input: a non-empty vector, x

%output: the average value of the vector

n = length(x);

avg = sum(x)/n;

 

 

            _____function avg = avg(x)______________________________

 

 

 

 

 

9.      Assume the function named func is defined below and saved in the file func.m :

 

function z = func(x, y)

global g

g = [g, x, y];

z = x+y;

 

At the Matlab prompt you type the following commands. Fill in the blanks with the correct values that Matlab would return.

 

>> clear

>> global g

>> g = [];

>> z = func(1, 2);

>> x = func(z, 3);

>> who

 

 

 

            _____g            x          z___(no deduction for different order)_____________________

 

>> g

   g =

 

 

            _____1            2          3          3_______________________________________

 

>> x

       x =

 

 

            ______________6___________________________________________________________

10.  Let A be a 2 x 3 matrix and B be a 3 x 4 matrix that contain numerical values. Which of the following are valid operations, that is, which of the following will not produce Matlab errors? Circle each valid operation. There may be more than one valid operation.

 

B * A        A * B        A * A        B * [A, A]        B * [A ; A]

 

 

 

 

11.  Assume that file fnc.m has the following content:

 

function b = fnc(a)

b=2;

scr;

b = [b, a, c, 0];

 

and that the file scr.m has the following content:

 

c = 2;

c = [a, c];

 

Write the results Matlab returns for the sequence of commands typed in the Matlab command window:

 

>> clear

>> a = 1;

>> b = 2;

>> c = 3;

>> d = fnc(c);

>> d

 

   d =

 

            _____2            3          3          2          0__________________________

 

 

12.  Fill in the six blanks to create the following matrix named bigmat.

   >> bigmat = [eye(_2_),zeros(_2_ , _3_);3*ones(__3__),zeros(_3_ , __2__)]

 

  bigmat =

 

     1     0     0     0     0

     0     1     0     0     0

     3     3     3     0     0

     3     3     3     0     0

3     3     3     0     0

 

 

13.  Given that the file named fancy.m contains the following code:

function y = fancy(x)

y = x - 4;


Fill in the blank below with the correct Matlab response.

 

>> y = fancy( fzero(@fancy, 1) )
   y =

   
              _________0____________________________________________________________

 

 

13.  Given that the file named fancy.m contains the following code:

function y = fancy(x)

y = x - 4;


Fill in the blank below with the correct Matlab response.

 

>> y = fzero(@fancy, fancy(1))
   y =

   
              __________4___________________________________________________________

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

14.  Assume that the following Matlab commands have been typed at the Matlab command prompt:

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

>> y = sin(x);

Which of the following Matlab commands will plot the function y = sin(x) where x takes values from the interval [-pi , pi]. Circle each correct answer. There may be more than one correct answer.

 

a)  

     >> plot(x,y)


        

 

 

b)  

      >> plot(y,x)

 


          

 

c)    

     >> plot( x , sin(x))

 

 

 

 

d)

   >> plot(sin(x))

   

 

 

    

15.  Would the following Matlab commands produce an error?

 

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

>> y = [-2,-1,0,1,2];

>> [X,Y] = meshgrid(x,y);

>> surf(X,Y,cos(X.^3-Y.^3))

 

 

(Circle one)        YES                              NO     

 

 

 

 

 

 

16.  What is the result Matlab returns for the following sequence of commands typed in the Matlab command window? Circle the correct answer.  (Note: there is only one correct answer.)

 

>> x = [2 3 5 7 4];

>> y = [4 9 13 49 16];

>> z = max(y(x.^2 == y))

   z =

 

 

a)  4   9   25   49   16

 

 

 

 

 

b) 4

 

 

 

 

 

c) 49

 

 

 

 

 

d) 16

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

17.  Assume that the file myscript.m has the following contents:

 

temp = y;

y = x;

x = temp;

 

What is the value of the three-element row vector z after the following sequence of commands has been typed in the Matlab command window? Circle the correct answer.  (Note: there is only one correct answer.)

 

>> x = 5;

>> y = 3;

>> myscript

>> myscript

>> z = [x y temp]

 

a) 5  3  5

 

 

b) Undefined function or variable ‘temp’.

 

 

c) 3  5  3

 

 

d) 3  5  5

 

 

e) 5  3  3

 

18.  Which of the following Matlab commands is a correct way of computing an approximation of the definite integral of the function f(x) = cos(x) + sin(x) on the real interval [0,5]? Circle each of the correct answers. (Note: there may be more that one correct answer.)

 

a) >> quadl('sin + cos',[0,5])

 

 

 

b) >> quadl('sin(x) + cos(x)',0,5)

 

 

 

c) >> quadl(@(x) sin(x) + cos(x),0,5)

 

 

 

d) >> trapz('sin(x) + cos(x)',0,5)

 

 

 

e) >> trapz(@(x) sin(x) + cos(x),0,5)

19.  Write a complete Matlab function named f_to_c that converts Fahrenheit temperature into Celsius temperature. The function f_to_c  has one input parameter fahr (a value in degree Fahrenheit), and the function returns the equivalent value in degree Celsius. Do not include comments in your function.

 

 Use the following conversion formula:

celsius = (fahr - 32) * 5/9  

 

 

 

   function celsius = f_to_c(fahr)

   celsius = (fahr – 32) * 5/9;

 

 

 

 

 

 

 

 

19.  Write a complete Matlab function named electron_ke that computes the kinetic energy of an electron using the formula,



 The function electron_ke  has one input parameter v (the velocity of the electron in units of meters/second), and the function computes and returns the value ke  by using the above formula. Do not include comments in your function.

 

 Use the following constants in your function for  c2 and   m*c2:

c2 = 9.0e16    (units meters2/second2)

m* c2 = 8.2e-14  (units Kg*meters2/second2 )

 

 

 

 

    function ke = electron_ke(v)

    ke = (1./sqrt(1 – v.^2/9.0e16)-1)*8.2e-14;