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 16, 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 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.

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 and to write your signature on the line below:

 

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

Score

Grader

1

6

 

 

2

6

 

 

3

6

 

 

4

4

 

 

5

6

 

 

6

6

 

 

7

4

 

 

8

9

 

 

9

4

 

 

10

4

 

 

11

8

 

 

12

9

 

 

13

10

 

 

14

4

 

 

15

8

 

 

16

9

 

 

17

9

 

 

18

13

 

 

Total

125

 

 

 

 

 

1. Fill in the blanks below using Matlab expressions to flip the values of y = [ 1 3 5 7 11].  

 

>> flipy = y(  __5__or__end__ : _____-1________  : ____1____ )

    

    flipy =

                11 7 5 3 1

 



 



2.      Write the output the following Matlab commands produce.

>> x = linspace(100,200,300);

>> x(1)

ans =
             ______100___________________________________

>> length(x)

ans =

   ______300____________________________________

>> x(end)


ans =

   ______200____________________________________




 

 

 

 

 

3.  Write the output the following Matlab commands produce.

>> x = 1:3:11;

>> x(x(1)+1)

ans =
       ______4_____________________________________

>> x(length(x))
    
     ans =

  ______10____________________________________



 

 

4.      Write the output the following Matlab commands produce.

    >> p = [1 0 5 -4];
    >> polyval(p, 2)

    ans =

           _______14______________________________________________




5.      Given a matrix:

 

 

fill in the blanks below to change A , so that it looks as below:

 

 

 

   >> A = [ 1 1 1 1; 1 1 1 1; 1 1 1 1; 1 1 1 1];

   >> A( _  [1 4] ___ , _ [1 4] ___ )  = __ 0  _____

 

 

 

 

6.      Given the Matlab matrix A , shown below, write the output that each of the following commands would produce.

 

      >> A = [0 1 2 3; 4 5 6 7; 8 10 11 12]

 

>> size(A(1:2:3 , 1:3))

__________2______3______________________________________________

 

>> size(A( : , : ))

__________3_____4______________________________________________

 

>> size([A; ones(1,4)])

_________4______4______________________________________________

7.      Which of the following is a correct function definition line (first line) for a function that has two input  parameters? The parameters will hold vectors. The function returns two values, the smallest value found in the first parameter and the largest value found in the second parameter. Circle all correct answers.

a) function min,max = minmax(first, second)



b)   function min,max = minmax([first second])



c)  function [min,max] = minmax(first, second)



d)  function [min,max] = minmax([first, second])

 

 

 

 

 

 

8.      Transform the following set of linear equations into a Matlab matrix and vector. Matrix  
      should contain the coefficients of the equations and column vector  should contain the right hand
      side of the equations.

 

 

   >> A = ____[6 28  -1; 0 5 -3; -5 -4 3];_________

 

    >> c = ____[5; 11; 12];__________________________

 

   Note that the above system of equations can be written as A*X = C. Now write a single Matlab

      command to solve the system of equations.

 

   >>  X = ____A \ c _________________________________

 

 

 

 

 

9.      Write a single Matlab command to find all the roots of 8x4 + x2 + 7. (Don’t write the roots.)

 

 

 

       roots( [ 8 0 1 0 7])

 

 

10.  Which of the following will produce a plot equivalent to the one produced by this code:
     
   >> y = 1:10;
   >> x = [5 2 3 7 3 7 9 1 4 6]

   >> plot(y, x);

      (Circle all correct answers.)

 

a) >> plot(1:10, x);

 

 

b)   >> plot(x, y);

 

 

c)   >> plot(x);

 

 

d)   >> plot(y);

 

 

 

11.  Using the built-in function trapz complete the following Matlab commands by filling in the blanks

       to compute the definite integral of the function y = x2 – 2000 ln(x) on the interval [1, 10],

      using 1000 points.

 

>> x = ______ linspace(1,10,1000); _____

 

>> y = ______ x.^2 – 2* log(x); ________________



>> trapz( __ x __, __ y ____________);

 

 

 

 

 

12.  Assume you have saved the file ‘f.m’ with the code:

 

    function y = f(x)

    y = 2*x - 4;

 

      a) Complete the Matlab command using fplot to plot this function for x between 0 and 10.

 

      >> fplot (________’f’ or ‘f(x)’ or @f ____, _[0 10] ______);

 

 

   b) Assuming that the fzero function works correctly, fill in the blank below with the correct

           response Matlab would return.

 

>>  f(fzero('f', 1))

      ans =

         

                  ________ 0 _________________________________

13.  Fill in the blanks to complete the code to plot the surface z  = cos(x) + cos(y) as a surface.

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

     >> y = linspace(-5,5,100);

            >> [XMAT,YMAT] = ____ meshgrid( x , y ) __________________________


            >> ZMAT = _____ cos(XMAT)+cos(YMAT) ____________________________



           
>> surf(___ XMAT ___, _ YMAT _____, ___ ZMAT _____________)

 

 

 

 

 

14.  Which of the following is the correct syntax for writing a comment in a function or script in Matlab? Circle only one answer.

a) # this is a comment


b) // this is a comment


c) (this is a comment)


d) % this is a comment


e) None of the above

 

 

 

 

 

 

 

15.  Given the matrix A = [9 8 7; 6 5 4; 3 2 1] compute the following values:

    a) >> A( 2:3 , 1:2:3)
 

    _______ [6  4 ; 3 1] _________________________________

    b) Complete the Matlab command to replace the second column of the matrix A with the values in
         the vector [-1 0 1] .
 

     >> A( __ : _______, ____ 2 ________) = ______ [-1 0 1] _________

 

16.   The file data.m contains the script,

       x = 1;

       y = 2;

      The file g.m contains the function code,

 

      function y = g(x)

      y = x + 1;

 

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

      Matlab would return.

 

    >> clear

    >> y = g(2);

    >> who

            _________ y _____________________________________________

 

    >> data

    >>  y

        y =

 

            __________ 2 _____________________________________________

   >> who

            ___________ x   y _____________________________________

 

 

 

 

17.  Fill in the blanks below to create the 100 x 100 matrix named mat with values [ 1 -1  1 -1 …1 -1] (100

      values) down the main diagonal and zeros elsewhere,

 

 

     

>> K = (-1).^(2:101)  or cos((0:99)*pi) or 1:2:199 - 4*floor((1:100)/2) or repmat([1 -1],1,50) or [1 -1 … 1 -1]___ ;


>> mat = diag( ______ K _________________ , ____ 0 _____________);

 

 

 

 

18.  Write a complete Matlab function named sums that has two parameters, a vector x and a positive

   integer n . The function sums returns the sum of the first n values of x. You may assume that n

      is less than or equal to the length of x.  Do not  include comments in  your code.

     Your function should work as the following examples show.

   >> x = [ 1 2 3 4]

   >> sums(x,3)

      ans =
            6

   >> sums(x,1)

      ans =

            1

      However your function should work not just for x = [1 2 3 4] but for any vector x.

 

 

 

  function y = sums(x,n)

   y = sum(x(1:n));

 

 

  

 

18.                      Write a complete Matlab function named maxcomp that has two parameters, a row vector x and a

            row vector y where x and y are of the same length . The function maxcomp returns a vector

           whose i-th value is the maximum of x(i) and y(i). See the examples below.  Do not  include

           comments in  your code.

           Your function should work as the following examples show.

      >> x = [ 1   2   3   4];

      >> y = [ 4   0   6   0];

      >> maxcomp(x,y)

      ans =
            4   2   6   4

      >> x = [  1    2   -3];

      >> y = [ -4    0   -6];

      >> maxcomp(x,y)

      ans =

 

            1     2   -3
            

     However your function should work not just for the above examples but for any row vectors x and  

     y of  the same length.

 

 

    function z = maxcomp(x,y)

    z = max([x ; y]);