COMP 110 Final Exam

You will have the full time period to complete this exam. This exam is closed book, closed notes, closed computer, closed cell phone, closed classmates, closed everything.

Please put all answers on the test sheets. If you need more space, use the backs of exam pages. Make sure your answer is clearly marked and easily distinguished from your work. You should show your work for possible partial credit.

When done, bring your exam to the front of the room, place it face down on the lecture table, and leave quietly so others can continue to concentrate. Please 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) ________________________________________________________________


Signed  _____________________________________________________________________

  1. (10%) For this question, you will select from this list of items:
    (A) method         (H) global variable    (O) merge sort 
    (B) function       (I) binary search      (P) object
    (C) bubble sort    (J) closure            (Q) data field 
    (D) local variable (K) constructor        (R) parameter
    (E) linear search  (L) indefinite loop    (S) sieve of eratostheses
    (F) quicksort      (M) return value       (T) data type
    (G) definite loop  (N) dot notation       (U) switch
    
    For each of the following definitions, select the item above that best matches (put the corresponding letter in the blank):
       i) _ Q ___ variable inside an object
    
      ii) _ A ___ function inside an object
    
     iii) _ K ___ function written to create, initialize, and return an object
    
      iv) _ I ___ an efficient search algorithm for ordered data sets
    
       v) _ C ___ an inefficient sorting algorithm
    
      vi) _ B ___ abstraction of a sequence of program statements
                  that can be run by calling it and passing arguments
    
     vii) _ L ___ repetition where we do not know in advance how 
                  many times we need to repeat a task
    
    viii) _ S ___ algorithm for generating prime numbers
    
      ix) _ P ___ a wrapper around data and functions to operate on that data
    
       x) _ N ___ syntax used to specify elements of an object in an expression
    
    
  2. (3%) Select the option that best answers this question: How we determine the order in which to perform operations when evaluating an expression.
    (A) scope rules
    (B) punctuation rules
    (C) syntax rules
    (D) precedence rules
    (E) function definition
    
    
    answer: __ D ________________
    
    
  3. (3%) Select the option that best answers this question: How we determine what storage location is denoted by a particular occurrance of a variable name in the source code.
    (A) scope rules
    (B) punctuation rules
    (C) syntax rules
    (D) precedence rules
    (E) function definition
    
    
    answer: __ A ________________
    
    
  4. (3%) Which of the following options is the odd one... meaning which one doesn't do what the others do?
    (A) x = x * x ;
    (B) x = ( function (n) { return n * n } ) ( x ) ;
    (C) x += x++  ;
    (D) x = Math.square(x) ; 
    (E) x *= x ;
    
    
    answer: __ C ________________
    
    
  5. (12%) For this question, put in the space given the result produced by evaluating each function call or expression:
    BRICKS FOR THESE
    
       i) ________ isNaN([3,4,5])
    
      ii) ________ isNaN(7+2.5)
    
     iii) ________ 30 % 8
    
      iv) ________ Math.round(3.1415927)
    
       v) ________ (5 > 3) && ("tar" < "heels") 
    
      vi) ________ false || true 
    
     vii) ________ 15 - 6 / 3
    
    viii) ________ [2,3,4].length 
    
      ix) ________ "tarheels".length
    
       x) ________ 100*Math.random() <= 100
    
      xi) ________ "one two three".split(" ")[1]
    
     xii) ________ Number("one");
    
    Note: what we are asking is what would be printed if the expression was in 
    an alert statement... for example, using item (x), what would get 
    printed for 
    
            alert( 15 - 6 / 3 )
    
    
    
  6. (3%) A function that has one or more calls to itself in its code body is called
    (A) erroneous
    (B) recursive
    (C) nested
    (D) compound
    (E) reflective
    
    
    
    answer: __ B _________________
    
    
    
  7. (3%) In question (12) following, which of these programming patterns is used (write the letter for any that apply)?
    (A) counter        (B) declarator       (C) terminator
    (D) accumulator    (E) prime factor     (F) constructor
    
    
    answer: __ A, D _______________
    
    
  8. (3%) In question (26) following, which of these programming patterns is used (write the letter for any that apply)?
    (A) counter         (B) declarator       (C) terminator
    (D) accumulator     (E) prime factor     (F) constructor
    
    
    answer: __ D, F _______________
    
    
  9. (3%) In question (18) following, which of these programming patterns is used (write the letter for any that apply)?
    (A) counter        (B) declarator       (C) terminator
    (D) accumulator    (E) prime factor     (F) constructor
    
    
    answer: __ A ________________
    
    
  10. (3%) Consider the number 197 in decimal (base 10). Write the binary (base 2) representation of it.
    
    
    answer: __ 11000101 ______
    
    
  11. (3%) Consider the number 11010011 in binary (base 2). Write the decimal (base 10) representation of this number.
    
    
    answer: ___ 211 _________
    
    
  12. (4%) What is printed in the "alert" box at the end of running this JavaScript code fragment?
       var max = 7;
       var tot = 2;
       var i = -2;  
       var done = false;
       while (!done) {
          tot = tot + i;
          i = i + 2;
          if (max < tot) { done = true; }
       }
       alert(tot);
    
    
    answer: ________________ 
    
    
  13. (4%) What is printed in the "alert" box at the end of running this JavaScript code fragment?
       
       var a = 7;
       var x = -3;
    
       if ( a < 0 ) { a = a + 4; }
       else if ( a >= 7 ) { x = a * 2; }
       else if ( a == 11 ) { a = a - 10 ; }
       else { a = -8;  x = a - x; }
    
       alert(a + ", " + x);
    
    
    answer: ________________ 
    
    
  14. (4%) What is printed in the "alert" box at the end of running this JavaScript code fragment?
       
       var a = 2;
       var x = 12;
    
       if ( x >= 12 ) { 
          x = x - 3; 
          a = a - x; 
          if ( a < 0 ) { x = x * a;  }
          else { x += 5; }
          a++ ; 
       }
       else { 
          a = a*3 ; 
          if ( a > 15 ) { x = x - a; }
          x = x + a ; 
       }
    
       alert(a + ", " + x);
    
    
    
    answer: ________________ 
    
    
  15. (4%) What is printed in the "alert" box at the end of running this JavaScript code fragment?
       
       var a = 7;
       var x = -3;
    
       if ( a < 0 ) { a = a + 4; }
       if ( a >= 7 ) { x = a * 2; }
       if ( a == 11 ) { a = a - 10 ; }
       if (true) { a = 8;  x = a - x; }
    
       alert(a + ", " + x);
    
    
    
    answer: ________________ 
    
    
  16. (4%) What is printed in the "alert" box at the end of running this JavaScript code fragment?
       
      var tag = -2;
      var val = 6;
      
      tag = tag+val;
      switch (tag) {
        case 0: val = "zero"; break;
        case 1: val = "one"; break;
        case 2: val = "two"; break;
        case 3: val = "three"; break;
        case 4: val = "four"; break;
        case 5: val = "five"; break;
      }
      switch(val.length) {
        case 2: tag *= 2; break;
        case 3: tag += 2; break;
        case 4: tag *= tag; break;
        default: tag += tag; break;
      }
    
      alert(tag);
    
    
    
    answer: ________________ 
    
  17. (4%) What is printed in the "alert" box at the end of running function "myMain" ?
       
      function myMain() {
        var bar = 8 ;
        var foo = [44,55,66,77,88,99] ;
        var i;
    
        i = 5 ;
        while (foo[i] > 75) {
          bar = bar + i;
          i--;
        }
        alert(bar);
      }
    
    
    
    
    
    answer: ________________ 
    
    
    
  18. (4%) What is printed in the "alert" box at the end of running function "myMain" ?
       
      function myMain() {
        var age = [51, 23, 65, 43, 18, 27];
        var i;  
        var temp;
    
        for (i=1; i < age.length; i++) {
          if (age[i-1] > age[i]) {
            temp = age[i];
            age[i] = age[i-1];
            age[i-1] = temp;
          }
        }
        alert(age[2] + ", " + age[5]);
      }
    
    
    
    
    
    answer: ________________ 
    
    
    
  19. (4%) What is printed in the "alert" box at the end of running function "myMain" ?
      function myMain() {
        var max = 2;
        var tot = 5;
        tot = mash (mash ( max ));
        alert(max + ", " + tot);
      }
    
      function mash ( tot ) {
        var M = 3 ;
        var res = 2 ;
        res = (tot + M) * res ;
        tot = M * tot ;
        return res ;
      }
    
    
    
    
    answer: ________________ 
    
    
    
  20. (4%) What is printed in the "alert" box at the end of running function "myMain" ?
       
    
      function myMain() {
        var max = 4;
        var num = 5;
        max = 2 * mosh ( max-2 );
        alert(num + ", " + max);
      }
    
      function mosh ( num ) {
        var res = 6 ;
        res = (num * res) + mish(res) ;
        num = 1;
        return res;
      }
    
      function mish ( arg ) {
        var num = arg + 2 ;
        var mosh = 4;
        return num + mosh;
      }
    
    
    
    
    
    
    
    answer: ________________ 
    
    
    
    
    
    
  21. (3%) What is printed in the "alert" box at the end of running function "myMain" ?
    
      function myMain() {
        var func;
        var n = 3;
        var fns = [ stomp, mash, tweak ];
        alert(fns[1](fns[2](n)));
        return;
      }
    
      function stomp (n) { 
        var x = n*n; return x + x; 
      }
    
      function mash (n) { 
        var x = n*3; return x - n; 
      }
    
      function tweak (n) { 
        var x = n+1; return x * n; 
      }
       
       
    
    
    
    
    
    answer: ________________ 
    
    
    
    
    
    
  22. (2%) What is printed in the "alert" box at the end of running function "myMain" ?
       
      var num = 4; // global variable
    
      function myMain() {
        var num = 3;
        var res;
        res = helper(num);
        alert(res + ", " + num);
      }
    
      function helper( arg ) {
        arg = num * arg;
        num = 5;
        return arg;
      }
    
    
       
    answer: ________________ 
    
    
    
  23. (2%) What is printed in the "alert" box at the end of running function "myMain" ?
       
      var num = 4; // global variable
    
      function myMain() {
        var res;
        res = helper(num);
        alert(res + ", " + num);
      }
    
      function helper( arg ) {
        arg = num * arg;
        num = 5;
        return arg;
      }
    
    
    
    
    answer: ________________ 
    
    
    
    
  24. (3%) What is printed in the "alert" box at the end of running function "myMain" ?
       
      function myMain() {
        var ob;
        ob = pair(5,10);
        alert(ob.two);
      }
    
      function pair( r, s ) {
        var pro, quo ;
        quo = s / r ;
        pro = s * r ;
        return { "one":pro, "two":quo } ;
      }
    
    
    
    
    
    
    
    
    answer: ________________ 
    
    
    
  25. (2%) What is printed in the "alert" box at the end of running function "myMain" ?
       
    
      function myMain() {
        alert( recMash(5) );
      }
    
      function recMash ( arg ) {
        var prod;
        if (arg == 2) { return 2 ; }
        else {
          prod = arg * recMash(arg-1) ;
          return prod;
        }
      }
    
    
    
    
    
    
    
    
    answer: ________________ 
    
    
    
    
    
    
    
  26. (3%) What is printed in the "alert" box at the end of running function "myMain" ?
       
    
      function myMain () {
        var ob = makeNewObj();
        ob.linc(); 
        alert(ob.lrep() + ", " + ob.num);
      }
    
      function makeNewObj() {
        var num = 6;
        var obj = {  
          num:3, name:"unc", done:false,
          linc: function() { num = num + this.num ; } ,
          lrep: function() { return num ; }
        };
        return obj; 
      }
    
    
    
    
    
    
    
    answer: ________________