# COMP 110 Midtem Exam

This exam is closed book, closed notes, closed computer, closed classmates, closed everything.

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.

When done, please staple together your exams sheets and any paper you use for work and place it face down on the lecture table. 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. (5%) Consider the number 205 in decimal (base 10). Write the binary (base 2) representation of it. Show your work or explain your reasoning for partial credit.
```

```
2. (5%) Consider the number 10110110 in binary (base 2). Write the decimal (base 10) representation of this number. Show your work or explain your reasoning for partial credit.
```

```
3. (10%) What is printed in the "alert" box at the end of running this JavaScript code fragment?
```   var top = 9;
var num = 4;
var i = 3;
var done = false;
while (!done) {
num = num + i;
i = i + 3;
if (i > top) { done = true; }
}

```
4. (10%) What is printed in the "alert" box at the end of running this JavaScript code fragment?
```   var end = 5;
var res = 10;
var k ;
for (k = 2; k < end; k++) {
res = res + (k*k) ;
}

```
5. (10%) What is printed in the "alert" box at the end of running this JavaScript code fragment?
```
var a = 4;
var x = 10;

if ( x < 10 ) { x = x - 3; a-- ; }
else { x = 22 - a ; a = a*2 ; }

if ( a <= 0 ) { a = a + 4; }
else if ( a == 4 ) { a = a - 10 ; }
else if ( a > 7 ) { a = a * 2; }
else { a = -8;  x = 12; }

if ( x >= a ) { x = x - a  ; }

alert("a: " + a + ", x: " + x);

answer: __ a: 16, x: 2 _____

```
6. (4%) What is printed in the "alert" box at the end of running this JavaScript code fragment?
```
var tag = 5;
var val = 2;

tag = tag-val;
switch (tag) {
case 0: val = 5; break;
case 1: val = val + 4; break;
case 2: val = val / 3; break;
case 3: val = val * 2; break;
case 4: val = val - 1; break;
case 5: val = val * tag; break;
}

```
7. (10%) What is printed in the "alert" box at the end of running this JavaScript code fragment?
```
var high = 4;
var arr = [ ];
var j;

arr[0] = 1;
for (j=1; j <= high; j++) {
arr[j] = arr[j-1] * j + 2 ;
}

```
8. (10%) What is printed in the "alert" box at the end of running function "main()" ?
```   function main() {
var max = 5;
var tot = 10;
tot = 2 * mash ( max );
alert("tot: " + tot + ", max: " + max);
}

function mash ( tot ) {
var tweak = 7 ;
var res = -2 ;
res = (tot + tweak) * res ;
tot = 12;
return res;
}

answer: __ tot: -48, max: 5 _____

```
9. (3%) Which of the following are NOT in the group of 6 fundamental concepts of programming that we have studied (write the 4 letters for your choices in the blank)?
```(A) loops and repetition      (F) data abstraction
(B) conditionals              (G) browser
(C) prompt box                (H) expressions and data retrieval
(D) HTML                      (I) procedure abstraction
(E) variables and assignment  (J) JavaScript

answers: __ C, D, G, J ______
```
10. (10%) For this question, you will select from this list of items:
```(A) byte           (G) world wide web     (M) CPU (or ALU)
(B) internet       (H) output             (N) definite loop
(C) string         (I) RAM memory         (O) gigabyte
(D) function       (J) indefinite loop    (P) expression
(E) bit            (K) integer            (Q) input
(F) array          (L) disk               (R) boolean
```
For each of the following definitions, select the item above that best matches (put the corresponding letter in the blank):
```   i) __ L __ slow non-volatile storage

ii) __ F __ grouping of variable locations in a program that
can be systematically used by subscript

iii) __ E __ single datum of binary information: 0 or 1

iv) __ R __ basic data type in JavaScript that comprises
exactly two values

v) __ M __ place where all arithmetic operations are
performed in the computer

vi) __ D __ abstraction of a sequence of program statements
that can be run by naming it and passing arguments

vii) __ J __ repetition where we dont know in advance how
many times we need to repeat a task

viii) __ A __ basic collection of 8 binary data values

ix) __ P __ combination of values and variables with operators that
reduces to a value under control of precedece rules

x) __ I __ fast volatile main storage

```
11. (3%) Select the option that best answers this question: If I need to keep track of how many times a particular data item is given by the user as input, I use this programming pattern.
```(A) terminator
(B) counter
(C) accumulator
(D) indicator
(E) declarator

```
12. (3%) Select the option that best answers this question: The manner in which we decide what storage location is denoted when we see a named used in a program.
```(A) precedence rules
(B) switch statement
(C) function call
(E) scope rules

```
13. (11%) For this question, put in the space given the result produced by evaluating each function call or expression:
```
i) __ 24 ___ Math.round(23.77318);

ii) __ 23 ___ Math.floor(23.77318);

iii) __ 4 ____ Math.sqrt(16);

iv) __ 3 ____ 18 % 5

v) _ true __ isNaN("javascript");

vi) _ fasle _ isNaN(25);

vii) _ fasle _ true && false

viii) _ true __ !true || !false

ix) _ -225 __ Number("-225");

x) __ 14 ___ 4 + 5 * 2

xi) __true __ 1 > Math.random()

Note: what we are asking is what would be printed if the expression was in an
alert statement... for example, for questions (vii), what would get printed for

```
14. (3%) Select the option that best answers this question: If I need to sum up the total of all values stored in an array, I use this programming pattern.
```(A) terminator
(B) counter
(C) accumulator
(D) indicator
(E) declarator

```
15. (3%) Which of the following options is the odd one... meaning which one doesn't belong with the others?
```(A) x += 1 ;
(B) x++ ;
(C) x = x + 1 ;
(D) x % 1 ;
(E) ++x ;