Department of Computer Science

University of Illinois at Urbana-Champaign

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

 


Name:                                                                         NetID:
 

Lab Section:                                                                Date: February 15, 2006

 


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

5

 

 

3

5

 

 

4

5

 

 

5

7

 

 

6

5

 

 

7

9

 

 

8

9

 

 

9

9

 

 

10

8

 

 

11

8

 

 

12

8

 

 

13

11

 

 

14

4

 

 

15

11

 

 

16

4

 

 

17

6

 

 

18

6

 

 

Total

125

 

 

 

 

Exam Scoreà

 

A

  1. Write the result produced when the following Matlab command is executed,
    >>
    0:5:20

     ans =

 

    ____________0____5______10_____15_____20______________________

 

 

 

  1. Write a Matlab command that displays all the variables in your current workspace.

 

 

>>  ________who___________or______________whos_____________________

 

 

  1. Write a Matlab command that deletes the variable x from your current workspace.

 

 

>>  _________clear   x_____________________________________________

 

 

 

 

  1. Circle each correct statement about the meaning of the semicolon in MATLAB. There may be more than one correct answer.

 

 

a)      The semicolon is required at the end of each statement.

 

 

 

b)      The semicolon is optional at the end of statements, and if present, will suppress the output from that statement.

 

 

 

c)      The semicolon is only required for statements in scripts or functions, but is optional for statements typed in main MATLAB window.

 

 

 

d)      [1 ; 2 ; 3 ] is identical to [1 , 2 , 3 ] in Matlab.

 

 

 

e)      [1 ; 2 ; 3 ] is identical to [1   2   3] in Matlab.

 

 

 

  1. Write a single Matlab command to find all the roots of x3+1 . (Don't write the roots, just write the Matlab command that gives the roots.)

            >>  ________roots([ 1   0   0   1])______________________________________________

 

 

 

 

  1. Write a single Matlab command to compute the range of the values in the given vector x. For example, if the vector x has the value [1, 4, -5, 5, 10], the range of x is 10-(-5) = 15. i.e maximum value minus the minimum value.  However your code should work for any non-empty vector of values for x and not just for this example. You may assume that x has already been given.

  1.  >> __________max(x)   -  min(x)_________________________________

 

 

 

 

  1. Write a single Matlab command to evaluate a polynomial x4 + 4x2 – x + 2  at x = 1:100.

 

 

 

>> _______polyval([ 1   0   4   -1   2],   1:100)______________

 

 

 

 

 

  1. Write the complete code for a Matlab function named force. The function force has two input parameters m and a. The function force has one output value F which is computed by means of the formula,

 

                                     F = ma

 

For example, when the user enters the following command at the Matlab prompt,

>>  F = force(10,2);

F will have the value 20. However your code for the function force should work not just for the values 10 and 2 but for any scalar values for m and a. Do not include comments in your code.

            function F = force(m,a)

                         F = m * a;    ( or F = m .* a;)

 

 

 

 

 

 

 

  1. Write the complete code for a Matlab function named  jump. The function  jump is defined as follows:

 

jump(x) = 1  if   x  >  0

jump(x) = 0 if    x <= 0.

 

For example, when the user enters the following command at the Matlab prompt,

>> y = jump(3);

y will have the value 1. The function  jump has one input parameter x. The output of the function is either 1 or 0 depending on the input value x and the rules above. Do not include comments in your code.

     function y = jump(x)

     y = x > 0;

 

 

 

 

 

 

 

 

  1. Fill in the blanks to create a Matlab 100 by 100 matrix named mat. Create this matrix in three steps. First step, create vector named K with values [100 99 98 … 3 2 1] (a vector with integer values from 100 to 1). Second step, put the values of K in a matrix along on the main diagonal and zeros everywhere else. Third step, modify the matrix by putting the value 100 along the last column.

 

 

 

    >> K =  ___100:-1:1___or___linspace(100,1,100)_________;



    >> mat = diag( ______K___________ , _______0___________);

 

 

 

     >> mat( __:_____ , ___end__or__100______ ) =  ______100_____ ;

 

11.  Given

 

>> m = [ 1  2 ; 3  4 ; 5  6 ];

 

Recall that size(m) returns a vector containing the number of rows and colums in m. For example,

 

>> size(m)

 

ans =   3   2

 

Fill in the blanks below with the results that Matlab would produce. If the command would produce an error then write ‘ERROR’.

 

>> size(m * m’)

                                                        

ans = ______3______3____________________________

 

 

>> size(m’ * m)

 

ans = ______2______2____________________________

 

 

>> size(m – m)

                                                          

ans = ______3______2____________________________

 

 

>> size(m .* m)

                                                          

ans = ______3______2____________________________

 

 

 

 

12.  Transform the following system of linear equations into a Matlab matrix and vector. Matrix A should contain the coefficients of the equations and column vector v should contain the right-hand side values from the equations:

 

x + y = 5

z - y = 7

2x + y + 3z = 17

 

>> A =  ___[ 1   1   0 ; 0   -1   1 ; 2    1    3];_______________

 

>> v =  ___[5   ; 7   ; 17] ;___or___[ 5    7    17]’  ;_____________

 

Write a single Matlab command to solve the system of equations using matrix A and vector v. Do   

not write the solution to the system of equations, just the Matlab command that would give the

solution.

 

>> __________A   \   v ;__________________________

 

 

 

13.  Write the values that Matlab would produce after executing each of the following commands.

>> v = 2*ones(4,1)
 
            2

2        
 
       2

2


     >> w = cumsum([1 2 3 4])
       

 _________1     3     6     10__________________________________

>> x = diff([1 2 3 4])
   

 

_________1        1       1__________________________________

  1. A function named fun is saved in the file fun.m as shown below:

 

function result = fun(x)

 global g

 g = g + x;

 result = g;

 

 

The following sequence of commands are entered at the Matlab prompt:

 

>> global g

>> g = 0;

>> i = fun(10);

>> g = g + 2;

>> j = fun(9);

>> g

 

Circle the correct output that Matlab produces. Circle only one choice.

 

 

a)      g = 19

 

 

 

 

 

b)      g = 21

 

 

 

 

 

c)      g = 11

 

 

 

 

 

d)      g = 2

 

 

 

 

 

 

 

 

 

 

 

 

  1. Given the matrix 

 

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

         A = 

     1   2   3 

 4   5   6 

 7   8   9 

 

 

            Write the output of the following commands. If the command results in an error, write ‘ERROR’.

 

            >> B = A(1:2:3,1:2:3)

 

 

                                    1          3

                                    7          9

 

           

            _____________________________________________

 

 

            >> C = sum(A)

 

 

 

 

 

 

            _____________12_____15______18_____________________

 

            >> D = [ A ,  [10  11  12] ]

 

 

 

 

 

 

            _____________ERROR________________________________

 

            >> A( 2, : ) = [13   13   13]

 

 

                                    1      2      3

                                    13    13    13

                                    7      8      9

 

 

            ____________________________________________

 

 

 

 

 

  1. Assume x and y are defined as follows:

 

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

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

 

Which of the following produce the same figure as plot(x,y)?
 Circle each correct answer. There may be more than one correct answer.

 

a)      plot(1:9, y)

 



b)      plot(y, x)

 



c)      plot(x, [5 1 3 9 7 2 4 5 6])



 

d)      plot(y)

 

 

 

  1. Fill in the blanks below to approximate the definite integral,





>> x = linspace(____0_____ , ____2_____ , 2000);

 

>> y = _______x.^2______________;

 

>> area = trapz( ______x_______________ , _______y____________)

 

 



  1. Fill in the blanks to use the Matlab function fplot to plot the function  2 e2x  -  ln(x)  on the interval [2,3].

 

 

>> fplot(____2*exp(2*x) – log(x)_ , ___[ 2 , 3 ]______)