COMP 110 Spring 2007

Final Exam

This exam is closed book, closed notes, closed classmates. You have 180 minutes to complete it, though I expect if you are well prepared you will need no more than 120 minutes.

Please put all answers on the test sheets. Make sure your answer is clearly marked and easily distinguished from work you do to get to the solution. You should show your work for partial credit. This especially applies to the program execution simulations. I showed you in class how to do this in a way that will assist you in getting the correct answer, as well as allow partial credit.

When done place your exam face down on the lecture table up front.

Please print your name clearly below, and sign when you hand in the exam, signifying your adherence to the honor code and attesting to the work being yours alone.



Name (print CLEARLY) --------------------------------------------------------



Signed  ---------------------------------------------------------------------

  1. (3%) I want to make my browser display a phrase of text is "typewriter font", that is, a font where every character is the same width so columns will line up properly. See for example the program code shown in problems 14-18, where I needed the columns to format lined up exactly as I typed them. I should use which of these tag pairs?
    (a) < ul >  < /ul >
    (b) < p >  < /p >
    (c) < pre >  < /pre >
    (d) < ol >  < /ol >
    
    
    answer: __C_______________
    

  2. (3%) I want to make my browser display a list of items, but have the items bulleted and not numbered. I should use which of these tag pairs?
    (a) < ul >  < /ul >
    (b) < p >  < /p >
    (c) < pre >  < /pre >
    (d) < ol >  < /ol >
    
    
    answer: __A_______________
    

  3. (3%) Select the item that best defines "fetch and execute cycle"
    (a) in the CPU, get an instruction from memory, decode it, get data from 
        memory, compute and return the result, then do it all again
    (b) the process by which a "for loop" computes on an array
    (c) the process of getting a program from a source file, compiling it 
        to bytecode, then running the virtual machine to execute it
    (d) the process of getting command line arguments into variables, then
        passing them to a method or function call
    
    
    answer: __A_____________
    

  4. (3%) Select the item that best completes the following.
    I have a JavaScript program that I want to put into an HTML document. I want it to run after the web page of information is rendered to the screen by the browser. I should put that script
    (a) in the head section
    (b) between the head section and the body section
    (c) right after the < h1 > header in the body section
    (d) at the end of the body section 
    
    
    answer: __D_____________
    
    

  5. (3%) I want to make my browser to show the line "Simple Program" at the top bar on the browser frame. I should use which of these tag pairs?
    (a) < title >  < /title >
    (b) < br >  < /br >
    (c) < table >  < /table >
    (d) < head >  < /head >
    
    
    answer: __A_______________
    

  6. (3%) Which of the following items is in Java but not in JavaScript?
    (a) strong variable typing
    (b) while loops
    (c) freedom from logical errors
    (d) functions
    (e) arrays
    
    
    answer: __A_____________
    

  7. (3%) Which of the following best defines "class variable" ?
    (a) a variable that is created when a method runs, and vanishes when
        the method ends
    (b) a variable declared in a class such that each object created from 
        that class shares a single "global" copy 
    (c) a variable that is created automatically by the compiler to contain 
        the name of a class at runtime
    (d) a variable declared in a class such that each object created from 
        that class gets its own copy
    
    
    answer: __B_____________
    

  8. (3%) Which of the following best defines "divide and conquer" ?
    (a) a method used in compilers to implement mathematical division 
        for double length floating point numbers
    (b) an approach to program writing that requires comments and code
        to be in the same source file
    (c) a programming technique in which the code for a function (method)
        involves calling the function itself
    (d) a problem solving strategy where the problem is too large to solve
        directly so it is broken into smaller solvable problems 
    
    
    answer: __D_____________
    

  9. (3%) Which of the following in NOT one of the 6 fundamental concepts of programming that we learned before we started Java?
    (a) variables and assignment of values to them
    (b) command line arguments
    (c) loops... repetition of blocks of code
    (d) selection... conditional statements
    (e) expressions, and retrieving data values from storage
    (f) functions... named abstracted blocks of code
    
    
    answer: __B_____________
    
    

  10. (3%) From the following list, which item is something NOT provided by an Object Oriented language like Java:
    (a) inheritance saves code development by class reuse
    (b) information hiding with variables wrapped in classes
    (c) objects created dynamically when the program is run
    (d) strong typing ensures freedom from logical errors 
    
    
    answer: __D_____________ 
    

  11. (4%) What is the binary representation of the decimal number 183? Show your work or explain your reasoning for partial credit.
    
    
    
    
    
    
               10110111             
    answer: ________________________
    

  12. (3%) Which of the following best defines "instance variable" ?
    (a) a variable declared in a class such that each object created from 
        that class gets its own copy
    (b) a variable that is allowed to contain data that does not match
        its type declaration
    (c) a variable that is created when a method runs, and vanishes when
        the method ends
    (d) a variable declared in a class such that each object created from 
        that class shares a single "global" copy 
    
    
    answer: __A_____________
    

  13. (3%) Which of the following best defines "recursion" ?
    (a) a problem solving strategy where the problem is too large to solve
        directly so it is broken into smaller solvable problems 
    (b) a method used in compilers to implement mathematical division 
        for double length floating point numbers
    (c) a programming technique in which the code for a function (method)
        involves calling the function itself
    (d) an approach to program writing that requires comments and code
        to be in the same source file
    
    
    answer: __C_____________
    

  14. (6%) What is printed when this Java code fragment is executed?
       final int MAX = 5;
       int num = 4;
       int k = 0;          // loop index
       while (k < MAX) {
          num = num + k;
          k++;
       }
       System.out.println(num);
    
    
    
    answer: __14______________
    
    
    
    

  15. (6%) What is printed when this Java program is executed?
    class DemoConcepts {
    
       int x = 0;
    	
       public int foo (int p) {
         x = 2;
         int x;
         x = p;
         p = 18;
         return x;
       }
    	
       public static void main (String[] args) {	
         int p = 0;
         x = foo(24);
         System.out.println("x: " + x+", p: "+p);
       }
    }
    
    
    answer: ___ x: 24, p: 0 _______
    
    

  16. (6%) What is printed when the following Java code fragment is executed?
       int x = 3;          
       int f = 8;
    
       if (x < 4) { x = x + 1; }
       else if (x == 4) { x = x - 2 ; }
       else if (x > 1) { x = x * 3; }
       else { x = -8 ; }
    
       if (f < 9) { f = f + 1; }
       if (f == 8) { f = f - 2 ; }
       if (f < 7) { f = f * 3; }
       else { f = - f ; }
    
       System.out.println("x: " + x + ", f: " + f);
    
    
    
    answer: ___ x:4, f: -9 _______
    
    
    

  17. (6%) What is printed when this Java program is executed?
    public class NumCrunch {
       public static int num = 50;
       int k = 4;
    
       NumCrunch() { k = num; num--; }
    
       public int getK () { return k; }
       public int getN () { return num; }
    
       public double munch (double num) {
          return num + num;
       }
    }
    
    public class SomeThing {
    
       public static void main (String[] args) {
          NumCrunch nc1 = new NumCrunch();
          NumCrunch nc2 = new NumCrunch();
          NumCrunch nc3 = new NumCrunch();
    
          nc1.munch(5);
          nc2.munch(8);
          System.out.println( nc2.getK()+",  "+nc1.getN() );
       }
    }
    
    
    
    answer: __ 49, 47 ___________
    
    
    
    

  18. (6%) What is printed in the "alert" box at the end of running this JavaScript code fragment?
       var i;            // loop index
       var flag = false;
    
       for (i=0; i<5; i++) {
          flag = !flag ;
       }
    
       alert(flag);
    
    
    
    answer: __ true _________
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

  19. (10%) For this problem you will write a single line of a program in Java.
    A software engineer has written the program you see below; he told me the program's behavior is as follows:
      -- create three double variables named vOne, vTwo, and vThree.  
      -- give these variables values from command line arguments.
      -- determine which of the three variables contains the smallest value 
         and print out that smallest value (if two or more of the variables 
         happen to contain the same value, I don't care which one actually 
         has its value printed out).
    
    However, the program does not work correctly. There is one line that needs to be different. First indicate which line number is incorrect, and then give the correct code so that the program correctly computes the smallest value.

    Use the space below to show your work in figuring out how the program works, for partial credit.

    
    incorrect line #: __09 ___________
    
    
    correct line:  } else if (vTwo <= vOne && vTwo <= vThree) {
                                           ^^
                                           logical AND
    
    01: public class Smallest {
    02:   public static void main (String[] args) {
    03:       double vOne, vTwo, vThree;
    04:       vOne = Double.parseDouble(args[0]);
    05:       vTwo = Double.parseDouble(args[1]);
    06:       vThree = Double.parseDouble(args[2]);
    07:       if (vOne <= vTwo && vOne <= vThree) {
    08:          System.out.println("smallest value is " + vOne);
    09:       } else if (vTwo <= vOne || vTwo <= vThree) {
    10:          System.out.println("smallest value is " + vTwo);
    11:       } else if (vThree <= vOne && vThree <= vTwo) {
    12:          System.out.println("smallest value is " + vThree);
    13:       } else {
    14:          System.out.println("this case is impossible!!");
    15:       }
    16:    }
    17: }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

  20. (10%) For this problem you will write a single line of a program in Java.
    A software engineer has written the program you see below; she told me the program's behavior is as follows:
      -- Create an array named A that contains integers; the array A 
         has length 25.  
      -- Fill the array with random integers between 0 and 100; the random
         numbers do not have to be unique.
      -- Then compute and print out the sum of the numbers in the array A.
    
    However, the program does not work correctly. There is one line that needs to be different. First indicate which line number is incorrect, and then give the correct code so that the program correctly computes the sum.

    Use the space below to show your work in figuring out how the program works, for partial credit.

    
    incorrect line #: __ 12 ___________
    
    
    correct line:  for (int k=0; k<MAX; k++) {
                                  ^
                                  don't go beyond the end of the array
    
    01: import java.util.*;
    02: public class Summer {
    03:    public static void main (String[] args) {
    04:       final int MAX = 25;
    05:       final int TOP = 100;
    06:       int sum = 0;
    07:       int A[] = new int[MAX];
    08:       Random gen = new Random(); 
    09:       for (int i=0; i
    
    

  21. (10%) For this problem you will write a single line of a program in Java.
    A software engineer has written the program you see below; he told me the program's behavior is as follows:
    -- To read a single command line argument (an integer) and put 
       that value into a variable called N   
    -- Then compute and print the value of N! (N factorial).
       Remember, factorial is defined as: n! = n * (n-1) * (n-2) * ... * 2 * 1
    
    However, the program does not work correctly. There is one line that needs to be different. First indicate which line number is incorrect, and then give the correct code so that the program correctly computes factorial.

    Use the space below to show your work in figuring out how the program works, for partial credit.

    incorrect line #: __ 04 _________
    
    
    correct line:   int fac = 1;
                              ^
                              to accumulate with *, you have to init to 1
    
    01: public class Factorial {
    02:   public static void main (String[] args) {
    03:     int N;
    04:     int fac = 0;
    05:     N = Integer.parseInt(args[0]);
    06:     for (int k=1; k<=N; k++) {
    07:        fac = fac * k;
    08:     }
    09:     System.out.println(N + " factorial is " + fac);
    10:   }
    11: }