Exam (elaborations) CS 10787
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. ElementById(“myElement”).innerHTML = “CS 105”; B. html[“myElement”] = “CS 105”; C. html[“myElement”].innerHTML = “CS 105”; D. [“myElement”].innerHTML = “CS 105”; E. Element(“CS 105”); Explanation As part of doing MP3, you learned that the JavaScript code: ElementById( ___1____ ).innerHTML = ____2_____; ..changes the element in the HTML document which has an attribute id="____1____" to the contents of 2. The only option that follows this syntax is (A). Additionally, you specifically programmed this line of code as part of completing MP3. 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. Explanation In Lecture 4.2, you learned that JavaScript, JS, and ECMAScript are all correct terms for JavaScript. However, Java is a completely different language than JavaScript. Saying “Java” when referring to “JavaScript” is incorrect. 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] Explanation When accessing elements inside an array, we access them by their index. The first index in every array is zero (0). Therefore a[0] == 20, a[1] == 40, a[2] == 60, and so forth. This question is looking for 40, which is contained in a[1]. CS 105: Sample Midterm Exam #1 Page 3 of 21 For the next two questions, consider the following code: 1 2 3 4 5 6 var x = 20; while (x 10) { print("Hello"); x *= 2; } 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) Explanation Line 1 initially sets our variable x to 20. Line 2 checks if x is less than 10. …is 20 less than 10? No. Since our conditional is false, we skip the code inside of the curly braces. Since there is nothing after Line 6, our program is finished. “Hello” is printed zero (0) times. 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) Explanation Line 1 initially sets our variable x to 5. Line 2 checks if x is less than 10. …is 5 less than 10? Yes. Since our conditional is true, we run the code inside of the curly braces. Line 4 prints “Hello” Line 5 multiples x by 2. Since the old value was 5, the new value is now 5*2, so x = 10. Line 6 reaches the end of our loop, so the program checks Line 2 again. …is 10 less than 10? No. Since our conditional is false, we skip the code inside of the braces. Since there is nothing after Line 6, our program is finished. “Hello” is printed 1 time.
Written for
- Institution
- CS 10787
- Course
- CS 10787
Document information
- Uploaded on
- July 9, 2024
- Number of pages
- 21
- Written in
- 2023/2024
- Type
- Exam (elaborations)
- Contains
- Questions & answers
Subjects
-
cs 105 sample midterm exam 1 annotated copy of s