Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
46 views

CS 105 Midterm Exam #1

The document is a sample midterm exam for CS 105 that contains 22 multiple choice questions testing JavaScript fundamentals like manipulating the DOM, arrays, loops, conditionals, and more. It provides the questions, possible answers for each, and context like code snippets needed to understand some questions.

Uploaded by

Ana Ochoa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

CS 105 Midterm Exam #1

The document is a sample midterm exam for CS 105 that contains 22 multiple choice questions testing JavaScript fundamentals like manipulating the DOM, arrays, loops, conditionals, and more. It provides the questions, possible answers for each, and context like code snippets needed to understand some questions.

Uploaded by

Ana Ochoa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

CS 105 Midterm Exam #1

CS 105: Sample Midterm Exam #1


Annotated Copy of Spring 2014’s Midterm Exam #1

Page 1 of 10
CS 105: Sample Midterm Exam #1

This page was intentionally left blank.

Page 2 of 10
CS 105: Sample Midterm Exam #1

1. Which of the following lines of JavaScript code will change the HTML inside of an element
with the attribute id="myElement" to "CS 105"?
A. document.getElementById(“myElement”).innerHTML = “CS 105”;
B. html[“myElement”] = “CS 105”;
C. html[“myElement”].innerHTML = “CS 105”;
D. document.html[“myElement”].innerHTML = “CS 105”;
E. html.myElement(“CS 105”);

2. Which term is not synonymous with the term JavaScript?


A. ECMAScript
B. JS
C. Java
D. All of the terms above are synonymous with JavaScript.

3. Given the array, var a = [20, 40, 60, 80, 100], which line of code accesses the value
40?
A. a[0]
B. a[1]
C. a[2]
D. a[3]
E. a[i]

For the next two questions, consider the following code:

1 var x = 20;
2 while (x < 10)
3 {
4 print("Hello");
5 x *= 2;
6 }

4. Assuming that the function print() has been defined that prints the value passed in as the
parameter to it, how many times is “Hello” printed?
A. 0
B. 1
C. 2
D. 3
E. Infinity (or at least until you kill the program)

5. If the initial value of x (set on Line #1) is now 5 instead of 20, how many times is “Hello”
printed?
A. 0
B. 1
C. 2
D. 3
E. Infinity (or at least until you kill the program)

Page 3 of 10
CS 105: Sample Midterm Exam #1

For the next two questions, consider the following code:

1 function foo(x)
2 {
3 var ct = 0;
4 for (var i = 0; i < x.length; i++)
5 {
6 if (x[i] >= 70)
7 {
8 ct++;
9 }
10 }
11 return ct;
12 }

6. Based on its usage in the function, what type of variable must x be?
A. A string
B. A number
C. An array of strings
D. An array of numbers
E. An array of arrays

7. Which of the following statements are true about the code above?
A. The function will always return the value 0.
B. The function will never return the value 0.
C. The function will never return.
D. The function only returns if x is a number.
E. The function will never return a negative number.

[This question refers to Spring 2014’s MP1, may not make sense outside of Spring 2014.]
8. In MP1, you solved a maze using calls to isWall(direction) and move(direction),
where the direction parameter was either “Forward”, “Left”, “Right”, or “Backward”.
Which of the following lines of code will never result in moving into a wall?
A. if (isWall(“Forward”) && isWall(“Left”) && isWall(“Right”))
{
move(“Backward”);
}
B. if (isWall(“Left”) || isWall(“Right”))
{
move(“Left”);
}
C. if (isWall(“Left”) && isWall(“Right”))
{
move(“Right”);
}
D. Both (B) and (C) are correct.
E. All of (A), (B), and (C) are correct.

Page 4 of 10
CS 105: Sample Midterm Exam #1

For the next four problems, consider a payment such as a rent or a credit card payment. The
variable daysLate stores the value of the number of days you are late on a payment.

9. If the penalty is $20 per day late, which of the following lines of code accurately calculates
the late penalty?
A. var penalty = daysLate + 20;
B. var penalty = daysLate * 20;
C. var penalty = daysLate ^ 20;
D. var penalty = daysLate && 20;
E. var penalty = daysLate;

10. For a different payment, the penalty is calculated with the following code:

1 var penalty;
2 if (daysLate < 3)
3 {
4 penalty = 0;
5 }
6 else
7 {
8 penalty = daysLate - 3;
9 }

What is an accurate English description of the code displayed above?


A. If the payment is less than three days late, the penalty is the number of days the
payment was late. Otherwise, there is no penalty.
B. If the payment is more than three days late, the penalty is the number of days the
payment was late. Otherwise, there is no penalty.
C. If the payment is less than three days late, the penalty is three dollars less than the
number of days the payment was late. Otherwise, there is no penalty.
D. If the payment is more than three days late, the penalty is three dollars less than the
number of days the payment was late. Otherwise, there is no penalty.
E. The penalty is always $20. That’s simple enough, right?

11. For yet another payment, this payment offers a grade period of gracePeriod days where no
penalty will be given if the payment is made within the number of grace period days. (This
means that, if the grace period is three days, no penalty is given if the payment is made is
made one, two, or three days late.) What is the conditional that is true if the payment is
made within the grace period?
A. if ( gracePeriod < daysLate )
B. if ( gracePeriod <= daysLate )
C. if ( gracePeriod > daysLate )
D. if ( gracePeriod >= daysLate )
E. if ( gracePeriod == daysLate )

12. What type of data is stored in the variable daysLate?


A. A number
B. A string
C. An array of numbers
D. An array of strings
E. An array of arrays

Page 5 of 10
CS 105: Sample Midterm Exam #1

13. What is the decimal (base 10) value of the following binary number: 101
A. 3
B. 5
C. 6
D. 7
E. 9

14. What is the decimal (base 10) value of the following binary number: 011
A. 3
B. 5
C. 6
D. 7
E. 9

15. In investment banking, one property of a good loan is a healthy Loan-To-Value ratio (LTV).
An LTV is calculated by taking the value of the loan and dividing it by the value of the
property. An LTV of 0.8 or less is considered safe, an LTV of 1.0 or greater is considered
dangerous, and all other LTVs are considered risky. Consider three functions that return
the safety of a given property given its LTV:

function safety(ltv) function safety(ltv) function safety(ltv)


{ { {
if (ltv >= 1.0) if (ltv <= 0.8) if (ltv <= 0.8)
{ { {
return “dangerous”; return “safe”; return “safe”;
} } }
else if (ltv <= 0.8) else if (ltv > 1.0)
{ { if (ltv > 1.0)
return “safe”; return “dangerous”; {
} } return “dangerous”;
else else }
{ {
return “risky”; return “risky”; return “risky”;
} }
} } }
(i) (ii) (iii)

Which one of the function(s) will always return the correct safety rating?
A. Only (i)
B. Only (i) and (ii)
C. Only (i) and (iii)
D. Only (ii)
E. (i), (ii), and (iii)

16. What is ASCII code?


A. A special version of JavaScript that we are using in CS 105
B. A universally recognized translation between letters and numbers
C. An encryption technique that makes text hard to read
D. A programming language used to make Android and iPhone apps
E. The five primary colors of light used on a computer screen

Page 6 of 10
CS 105: Sample Midterm Exam #1

For the next five questions, consider the following array:


var a = [“Red”, “Orange”, “Yellow”, “Green”, “Purple”];

17. What is value of a.length?


A. 5
B. 6
C. 7
D. 8

18. What is the value of a[3]?


A. “Red”
B. “Orange”
C. “Yellow”
D. “Green”
E. “Blue”

For the next three questions, consider the following code that continues to use the array a that
is defined at the top of this page:
1 var result = "";
2 for (var i = 0; i < a.length; i++)
3 {
4 var s = a[i];
5 var c = s.charAt(i);
6 if (c == "e")
7 {
8 result += s;
9 }
10 }

19. What is the value of s the second time the loop is run?
A. “Red”
B. “Orange”
C. “Yellow”
D. “Green”
E. “Blue”

20. What is the value of c during the third time the loop is run?
A. “P”
B. “u”
C. “r”
D. “l”
E. “e”

21. What the value in result after the code has finished running?
A. “Green”
B. “RedYellow”
C. “RedOrangeYellow”
D. “RedYellowGreenPurple”
E. “RedOrangeYellowGreenPurple”

Page 7 of 10
CS 105: Sample Midterm Exam #1

22. Which of the following is not a primary color used by computers to represent every possible
color on a traditional computer screen?
A. Blue
B. Green
C. Red
D. Yellow

23. Which of the following array(s) can be searched using a linear search?
A. [“apple”, “banana”, “blackberry”, “grape”, “kiwi”]
B. [“city”, “crow”, “country”, “cow”]
C. [“university”, “of”, “illinois”, “at”, “urbana”, “champaign”]
D. Both (A) and (B), but not (C)
E. (A), (B), and (C)

24. Which of the following array(s) can be searched using a binary search?
A. [“apple”, “banana”, “blackberry”, “grape”, “kiwi”]
B. [“city”, “crow”, “country”, “cow”]
C. [“university”, “of”, “illinois”, “at”, “urbana”, “champaign”]
D. Both (A) and (B), but not (C)
E. (A), (B), and (C)

25. After one pass (iteration) of a standard selection sort (as shown in lecture), what is always
true about the array?
A. The first element in the array is in the correct location.
B. The middle element in the array is in the correct location.
C. The last element in the array is in the correct location.
D. Half of the array has been thrown out, since it is no longer needed.

26. Consider the following three code snippets:

var a = [2, 4, 6]; var a = [1, 3, 5]; var a = [3, 6, 9];


var b = a[0] + a[1]; var b = a[1] + a[2]; var b = a[2] – a[0];

(i) (ii) (iii)

Which code snippet(s) will have the variable b equal to 6?


A. Only (i)
B. Only (i) and (ii)
C. Only (i) and (iii)
D. Only (ii)
E. (i), (ii), and (iii)

Page 8 of 10
CS 105: Sample Midterm Exam #1

Stenography is the practice of concealing data within


another form of data. You saw this in lecture with 4
images, but now you will practice stenography right here y
on your CS 105 midterm exam. - 3
a
2
In the JavaScript below, you will notice that there is a x
call to the function fill(x, y). When this function is i 1
called by the code, you should fill in the cell at the s
location specified. For example, if the variable A has the 0
value of 1 and B has the value of 0, a call to fill(A, B) 0 1 2 3 4
should fill the cell at (1, 0). We have filled this cell for x-axis
you already as an example.

Consider the following code for the next three questions:


1 var a = [1, 2, 2, 2, 1];
2 for (var i = 0; i < a.length; i++)
3 {
4 if (a[i] == 1)
5 {
6 fill(1, i);
7 fill(3, i);
8 }
9 fill(2, i);
10 }

27. When the for-loop runs for the first time, how many times is the fill() function called?
A. One time
B. Two times
C. Three times
D. Four times
E. Five times

28. When the for-loop runs for the first time, which of the following is happening?
A. Several cells within a single row are filled.
B. Several cells within a single column are filled.

29. After the program has completed, what is the state of the cell at (2, 2)?
A. It is filled.
B. It is not filled.

30. When the program has completed, what letter is made on the grid?
A. I
B. O
C. A
D. H
E. K

Page 9 of 10
CS 105: Sample Midterm Exam #1

This page was intentionally left blank.

Page 10 of 10

You might also like