CS 105 Midterm Exam #1
CS 105 Midterm Exam #1
Page 1 of 10
CS 105: Sample Midterm Exam #1
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”);
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]
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
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 }
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 )
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:
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)
Page 6 of 10
CS 105: Sample Midterm Exam #1
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.
Page 8 of 10
CS 105: Sample Midterm Exam #1
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
Page 10 of 10