CISC106, Fall 2006

Final Exam (E03)

Exam Date: December 12, 2006

Name: ________________________________________________________________


UD Email: _____________________________________________@ udel.edu


Circle your section number:      

     010       011        012        013

     014       015        016        017

 

Do not write your name on any page except this one.

Answer all questions on this paper directly (including the multiple choice)

Total Points: ???


A hint about allocating your time:

You might try to answer 100 points worth of questions in 100 minutes, leaving you 20 minutes to review your answers or work on any questions you skipped the first thru.

So....


Multiple Choice: Easy questions about MATLAB and C++

  1. (4 pts) If you are in MATLAB and you want to type a Unix command, what symbol do you type at the start of the line after the MATLAB prompt?
    1. !
    2. #
    3. @
    4. >>
    5. %
  2. (4 pts) Which of the following is used to start a comment in MATLAB?
    1. !
    2. #
    3. /*
    4. //
    5. %
  3. (4 pts) Which of the following is true about the Unix command:

    CC -c lab09.cc
    1. It links (but does not compile) the file lab09.cc, and produces a file called a.out
    2. It links (but does not compile) the file lab09.cc, and produces a file called lab09.cc~
    3. It compiles (but does not link) the file lab09.cc, and produces a file called lab09.o
    4. It compiles (but does not link) the file lab09.cc, and produces a file called lab09.exe
    5. It executes (but does not compile) the file lab09.cc, and produces a file called lab09.exe

    Multiple Choice: What is the output?

    Each of the problems in this section gives you some input that is typed into MATLAB inside the box with the heavy border.

    Which of the boxes afterwards with the light border represents the output you will get back?

    Consider each problem as a new problem—that is, suppose that you do a clear command in between each problem.

    Feel free to use the blank space on the exam sheet for scratch work.

  4. (4 pts) What is the output?

    >> dataFile = 'project1.dat';   
    >> strrep(dataFile,'.dat','.jpg')
    1. ans =
      
      project1.jpg
      
      >> 
      
    2. ans =
      dataFile.jpg
      >> 
      
    3. ans =
      dataFile.dat.jpg
      >> 
      
    4. ans =
      project1.dat.jpg
      >> 
      
    5. No output—just a new >> prompt
  5. (4 pts) What is the output?

    >> dataFile = 'project1.dat';   
    >> [ dataFile '.jpg' ]
    1. ans =
      
      project1.jpg
      
      >> 
      
    2. ans =
      dataFile.jpg
      >> 
      
    3. ans =
      dataFile.dat.jpg
      >> 
      
    4. ans =
      project1.dat.jpg
      >> 
      
    5. No output—just a new >> prompt

    Multiple Choice—MATLAB code for a road project

    The multiple choice questions in this section deal with the following problem.

    You work for an engineering firm doing a study of three alternative routes for a new highway. The following table shows information about these three routes. The costs are in millions of dollars.

    Each route has a cost associated with construction. Because each route will involve some impact on wetlands, there is also a cost associated with mitigating the environmental impact by constructing artificial wetlands nearby.

    Code Name Construction Cost Environmental Mitigation Cost
    red 127.5 150.0
    blue 200.0 25.0
    green 250.0 0.0
    yellow 210.0 12.5


  6. (4 pts) This code initializes vectors for the construction cost and the mitigation cost:
    constructionCost = [ 127.5  200.0  250.0  210.0];	  
    mitigationCost = [150.0 25.0 0.0 12.5];

    Which of the following would be correct MATLAB code for create a vector to hold all of the code names for the project?

    1.  codeName = [ 'red' 'blue' 'green' 'yellow' ];
    2.  codeName = [ "red" "blue" "green" "yellow" ];
    3.  codeName{1} = "red";
       codeName{2} = "blue";
       codeName{3} = "green";
       codeName{4} = "yellow";
      
    4.  codeName(1) = 'red';
       codeName(2) = 'blue';
       codeName(3) = 'green';
       codeName(4) = 'yellow';
      
    5.  codeName{1} = 'red';
       codeName{2} = 'blue';
       codeName{3} = 'green';
       codeName{4} = 'yellow';
      
  7. (4 pts) The first two lines of the code in the box below defines two vectors for the costs. The third line (in bold) then uses a MATLAB vector operation to add the two cost vectors together, and gives a new vector with the total cost of each project:
    constructionCost = [ 127.5  200.0  250.0  210.0];	  
    mitigationCost = [150.0 25.0 0.0 12.5];
    totalCost = constructionCost + mitigationCost;

    Which of the following sequences of MATLAB code is equivalent to the third line (the one in bold?)

    1. for k=1:length(totalCost)
      totalCost(k) = constructionCost(k) + mitigationCost(k); end; % for loop
    2. for k=1:length(constructionCost)
      totalCost(k) = constructionCost + mitigationCost; end; % for loop
    3. for k=1:length(mitigationCost)
      totalCost(k) = constructionCost(k) + mitigationCost(k); end; % for loop
    4. for k=1:length(totalCost)
      totalCost = constructionCost(k) + mitigationCost(k); end; % for loop
  8. (4 pts) We want to build a MATLAB script file that will print the project name with the lowest total cost. Suppose we build it as follows:

    Which of the following answers would be correct for the "third part", so that the final script prints a message indicating which project has the lowest cost, e.g. The project with the lowest cost is yellow ?

    1. minIndex = 1;
      for k=2:length(totalCost)
         if ( totalCost(k) < totalCost(minIndex) )
            minIndex = k;
         end % if
      end %for
      
      fprintf('The project with the lowest cost is %s\n', ...
              codeName{minIndex});
      
      
    2. minIndex = v(1);
      for k=2:length(v)
         if ( v(k) < v(minIndex) )
            minIndex = k;
         end % if
      end %for
      
      fprintf('The project with the lowest cost is %s\n', ...
              codeName(k));
      
      
    3. min= totalCost(1);
      for k=2:length(totalCost)
         if ( min < totalCost(minIndex) )
            min = totalCost(k);
         end % if
      end %for
      
      fprintf('The project with the lowest cost is %s\n', ...
              codeName(min));
      
      
    4. min = 0;
      for k=1:length(totalCost)
         if ( totalCost(k) < min )
            min = k;
         end % if
      end %for
      
      fprintf('The project with the lowest cost is %s\n', ...
              codeName(min));
      
      
    5. minIndex = 1;
      for k=1:length(totalCost)
         if ( totalCost(k) < totalCost(minIndex) )
            minIndex = totalCost(k);
         end % if
      end %for
      
      fprintf('The project with the lowest cost is %s\n', ...
              codeName{minIndex});
      
      
  9. (4 pts) Which of the following would be reasonable as the first two lines for the MATLAB m file containing the script described in question 3? (Hint: note that a key word here is script ).
    1. % roadProject.m    Compute road project with lowest total cost
      % P. Conrad, 12/09/2006 for VeryFineEngineering, LLC.
    2. function result = roadProject(v)
      %roadProject  Compute road project with lowest total cost
      
    3. function result = roadProject(v)
      %P. Conrad, 12/09/2006  for  VeryFineEngineering, LLC.

    Multiple Choice—Recursion

    Below you will find code listings for four M-files, each of which defines a version of the factorial function. Among these four are:

    • (a) one that uses recursion, and is correct
    • (b) one that uses recursion, but is WRONG
    • (c) one that does NOT use recursion and is correct
    • (d) one that does NOT use recursion, and is WRONG

    Your job is to identify which one is which. You will use each answer exactly once.

  10. (4 pts) Choose one:
    1. fact1 uses recursion, and is correct
    2. fact1 uses recursion, but is WRONG
    3. fact1 does NOT use recursion and is correct
    4. fact1 does NOT use recursion, and is WRONG
    1function result = fact1(n)
    2%fact1 first version of factorial function
    3%
    4% Consumes: n, a scalar number
    5% Produces: result, a scalar number
    6% Examples:
    7% >> fact1(4)
    8% ans = 24
    9% >> fact1(0)
    10% ans = 1
    11% >> fact1(-1)
    12% Error: factorial of negatives is undefined
    13% >>
    14
    15 if (n<0)
    16 error('factorial of negatives is undefined');
    17 end;
    18
    19 result = 1;
    20 if (n<=1)
    21 return;
    22 end %if
    23
    24 result = fact1(n-1) * n;
    25 return
    26
    27end


  11. (4 pts) Choose one:
    1. fact2 uses recursion, and is correct
    2. fact2 uses recursion, but is WRONG
    3. fact2 does NOT use recursion and is correct
    4. fact2 does NOT use recursion, and is WRONG
    1function result = fact2(n)
    2%fact2 second version of factorial function
    3%
    4% Consumes: n, a scalar number
    5% Produces: result, a scalar number
    6% Examples:
    7% >> fact2(4)
    8% ans = 24
    9% >> fact2(0)
    10% ans = 1
    11% >> fact2(-1)
    12% Error: factorial of negatives is undefined
    13% >>
    14
    15 if (n<0)
    16 error('factorial of negatives is undefined');
    17 end;
    18
    19 result = 1;
    20 for k=1:n
    21 result = result * n;
    22 end % for
    23
    24 return
    25
    26end


  12. (4 pts) Choose one:
    1. fact3 uses recursion, and is correct
    2. fact3 uses recursion, but is WRONG
    3. fact3 does NOT use recursion and is correct
    4. fact3 does NOT use recursion, and is WRONG
    1function result = fact3(n)
    2 %fact3 third version of factorial function
    3%
    4% Consumes: n, a scalar number
    5% Produces: result, a scalar number
    6% Examples:
    7% >> fact3(4)
    8% ans = 24
    9% >> fact3(0)
    10% ans = 1
    11% >> fact3(-1)
    12% Error: factorial of negatives is undefined
    13% >>
    14
    15 if (n<0)
    16 error('factorial of negatives is undefined');
    17 end;
    18
    19 result = 1;
    20 for k=1:n
    21 result = result * k;
    22 end % for
    23
    24 return
    25
    26end


  13. (4 pts) Choose one:
    1. fact4 uses recursion, and is correct
    2. fact4 uses recursion, but is WRONG
    3. fact4 does NOT use recursion and is correct
    4. fact4 does NOT use recursion, and is WRONG
    1function result = fact4(n)
    2 %fact4 first version of factorial function
    3%
    4% Consumes: n, a scalar number
    5% Produces: result, a scalar number
    6% Examples:
    7% >> fact4(4)
    8% ans = 24
    9% >> fact4(0)
    10% ans = 1
    11% >> fact4(-1)
    12% Error: factorial of negatives is undefined
    13% >>
    14
    15 if (n<0)
    16 error('factorial of negatives is undefined');
    17 end;
    18
    19 result = 0;
    20 if (n<=1)
    21 return;
    22 end % if;
    23
    24 result = fact4(n-1) * n;
    25 return
    26
    27end


    (End of multiple choice)

    MATLAB—Fill in the blank coding

    (continuation of previous problem)

  14. Now fill in the missing code in the following script file which could be used to test fact1.m
    1% testFact1.m Run regression/acceptance tests
    2% for fact1.m
    3
    4fprintf('Test 1...');
    5if (fact1(4)==24)
    6 fprintf('passed\n');
    7else
    8 fprintf('failed\n');
    9end;
    10
    11fprintf('Test 2...');
    12if (fact1(0)==1)
    13 fprintf('passed\n');
    14else
    15 fprintf('failed\n');
    16end;
    17
    18fprintf('Test 3...');
    19if (fact1(3)==6)
    20 fprintf('passed\n');
    21else
    22 fprintf('failed\n');
    23end;
    24
    25%** (6 pts) Fill in the missing line of code below
    26%** so that Test 4 determines whether the
    27%** function correctly handles the error
    28%** condition
    29
    30fprintf('Test 4...');
    31try
    32
    33 fprintf('failed\n');
    34catch
    35 fprintf('passed\n');
    36end;

    MATLAB coding—Fill in the blank

  15. Below you will find a code listing for an M-file that defines a function.

    Some code has been removed after the bold comment.

    Fill in the missing code.

    1function result = sumOfPositiveElements(vec)
    2%productOfOddElements return the sum of all the positive elements
    3%
    4% Consumes: vec, a vector of numbers
    5% Produces: a scalar number
    6%
    7% Example:
    8%
    9% >> sumOfPositiveElements([3 -2.5 6 -1])
    10% ans =
    11% 9
    12% >> sumOfPositiveElements([])
    13% ans =
    14% 0
    15% >> sumOfPositiveElements([-2 -4])
    16% ans =
    17% 0
    18
    19 result = 0;
    20
    21
    22
    23 for k = 1:length(vec)
    24
    25 %** (10 pts) Fill in the missing code
    26
    27
    28
    29
    30
    31 end % for
    32
    33 return
    34end


    C++ coding—Fill in the blank

  16. Below you will find complete listings of several files, except that at a few places in the files code has been removed.

    Before this code was removed, these files contained complete correct source code for a C++ file to define a function to compute the area of a rectangle, and a driver program that uses that function to compute price per square inch for a rectangular pizza.

    Each place where code was removed is marked with comments containing //** and a point value.

    Fill in the missing code.

    1// areaOfRect.cc P. Conrad for CISC106 final exam, 12/10/2006
    2// compute area of a rectangle
    3//
    4// Consumes: length ( a double), 345 ( a double)
    5// Produces: area ( a double)
    6//
    7// Examples:
    8// Function call: cout << areaOfRect(7,5) << endl;
    9// Output: 35
    10//
    11// Function call: cout << areaOfRect(3,3.25) << endl;
    12// Output: 9.75
    13
    14double areaOfRect(double length, double width)
    15{
    16 //** (4 pts) Fill in the line that computes and returns the result
    17
    18
    19}


    There is more code to fill in on the next page...

    1// rectPizza.cc
    2// P. Conrad for CISC106 Exam
    3
    4#include <iostream>
    5using namespace std;
    6
    7//** (3 pts) Something must go after this comment---otherwise when you
    8//** compile rectPizza.cc, you get this error:
    9//** error: `areaOfRect' undeclared (first use this function)
    10//** Fill in what is missing.
    11
    12
    13
    14//** (3 pts) Fill in the missing line that starts the main program in C++
    15
    16
    17{
    18 //** (2 pts) Ask the user to input the length of the pizza
    19 //** (One additional line of code is needed here)
    20
    21 double length;
    22 cout << "Please enter length ";
    23
    24
    25 //** (6 pts) Fill in one or more lines of code as needed to
    26 //** ask the user to input the width of the pizza
    27
    28
    29
    30
    31
    32 //** (6 pts) Fill in one or more lines of code as needed to
    33 //** ask the user to input the price of the pizza
    34
    35
    36
    37
    38
    39 double ppsi; // price per square inch
    40
    41 //** (4 pts) Write a line of code that computes the price
    42 //** per square inch and assigns it to ppsi. Use a function
    43 //** call to areaOfRect() as part of your calculation
    44
    45
    46
    47 //** (4 pts) Output the result with an appropriate message; that is,
    48 //** tell the user what the price per square inch of the pizza is.
    49
    50
    51
    52 return 0; // normal successful completion of a C++ program
    53}
(End of Exam)