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 _____________________________________________________________________
(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) switchFor 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
(A) scope rules (B) punctuation rules (C) syntax rules (D) precedence rules (E) function definition answer: __ D ________________
(A) scope rules (B) punctuation rules (C) syntax rules (D) precedence rules (E) function definition answer: __ A ________________
(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 ________________
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 )
(A) erroneous (B) recursive (C) nested (D) compound (E) reflective answer: __ B _________________
(A) counter (B) declarator (C) terminator (D) accumulator (E) prime factor (F) constructor answer: __ A, D _______________
(A) counter (B) declarator (C) terminator (D) accumulator (E) prime factor (F) constructor answer: __ D, F _______________
(A) counter (B) declarator (C) terminator (D) accumulator (E) prime factor (F) constructor answer: __ A ________________
answer: __ 11000101 ______
answer: ___ 211 _________
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: ________________
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: ________________
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: ________________
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: ________________
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: ________________
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: ________________
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: ________________
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: ________________
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: ________________
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: ________________
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: ________________
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: ________________
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: ________________
function myMain() { alert( recMash(5) ); } function recMash ( arg ) { var prod; if (arg == 2) { return 2 ; } else { prod = arg * recMash(arg-1) ; return prod; } } answer: ________________
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: ________________