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 27, 2007

 


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.

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

6

 

 

2

13

 

 

3

3

 

 

4

9

 

 

5

4

 

 

6

5

 

 

7

7

 

 

8

14

 

 

9

4

 

 

10

8

 

 

11

8

 

 

12

6

 

 

13

6

 

 

14

8

 

 

15

9

 

 

16

5

 

 

17

10

 

 

Total

125

 

 

 

 

Exam Scoreà

 

A

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

    >>  A=[0 2 3 ; 1 3 0];

>>  B=[1 0 3 ; 1 5 0];

>>  A+B

    ans =

 

 

              1    2    6

              2    8    0

 

 

    ______________________________________________________

 

 

 

 

 

 

 

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

>> X=[1,2,3,4,5];

>> X(3)

        ans =

 

  ____________3__________________________________________

>> X(1:3)

   ans =

 

            _____________1        2          3______________________________

>> X(3:end)  

   ans =

 

  _____________3      4          5______________________________

 

>> [X(end:-1:3) ; X(1:2:5)]’  

   ans =

          5    1

          4    3

          3    5

 

  ______________________________________________________

 

 

 

  1. Assume that the variable y holds a scalar and the variable x holds a vector.

Write a single Matlab expression that will compute y divided by x :

For example, if  y = 3  and x = [ 1   3   6 ] then  y divided by x should return [ 3  1  .5 ] . However the Matlab expression you write should work for any scalar assigned to y and vector assigned to x. You may assume that x doesn’t contain the value 0.

 

 

  __________ y ./ x    or    x .\ y _________________________________________

 

 

 

 

  1. Fill in the blanks below to plot the function y = cos(x) using the Matlab plot function.  Choose a sample of 50 equally spaced values from 0 to π for x .

 

>> x = _____ linspace(0,pi,50);_________
       

     >> y = ______ cos(x)_________________

 

 

>> ___ plot(x,y)___________________________

 

 

  1.  The following function generates an error,  ( the line numbers are not part of the code)

 

 line 1)   function = dummy(parameter)
 line 2)  
global x
 line 3)  
result = x + parameter;

 

when the following commands are typed at the Matlab prompt:

 

 

>> clear

 

>> global x

 

>> x = 1;

 

>> dummy(5)

 

 

The command dummy(5) should produce the value 6 but instead generates an error. Write the line number in the function dummy that generates the error and the corrected line of code below:

 

Line # _______ 1_________________________________



Corrected Line: __ function result = dummy(parameter)________

6.      Fill in the blanks to call the function fplot to plot the function y = sin2(x) + sin()  on the interval [0,40].

 

 

>> fplot(___ ‘sin(x).^2 + sin(sqrt(x))’___ , ____ [0,40]____)

 >> fplot(___@(x) sin(x).^2 + sin(sqrt(x))___ , ____ [0,40]____)

 

 

 

7.      Write the single Matlab command to compute all the roots of function y =x5-1 . Do not write the roots just write the command that will produce all the roots.

 

 

 

>> _____________ roots( [ 1   0   0   0   0   -1] )________________

 

 

 

 

8.      We would like to plot the following 3D surface:

 

 

 

 

Fill in the blanks to plot the surface above a grid of points in the x-y plane with 50 equally spaced values for x between 1 and 10 and 50 equally spaced values for y between -3 and 3 .

 

>> x = linspace(___ 1____________, _____ 10____________,_____ 50____________);

 

>> y = ____ linspace( -3 , 3 , 50)_____________________;

 

>> [XMAT ,YMAT] = meshgrid(____ x___________,_____ y______________);

 

 

>> ZMAT = ____ exp( - XMAT.^2) .* cos(2.*YMAT)__________ ;

 

 

>> surf(___ XMAT__, ___ YMAT_____, ___ ZMAT______)

 

  1. We have two functions f1 and f2 saved in two separate files f1.m and f2.m as follows:

 

function y = f1(x)

 global g

 g = g + x;

 y = g;

 

function y = f2(x)

 g = 100;

 y = g * x;

 

 

The following sequence of commands is entered at the Matlab prompt. Fill in the blank with the correct value that Matlab returns for the variable named g.

>> clear

>> global g

>> g = 0;

>> a = f1(3);

>> b = f2(g);

>> g