Page:of 20 Automatic Zoom Width 200% 300% CGS 3066 Practice Test 2 1 50% Actual Size 75% 400% Page Fit 100% Page 125% 150% CGS 3066: Practice Test 2 The questions below are indicative of the questions that you may be asked on the test; the length of this practice test is not indicative of the length of the unit test. Answers are included at the end of this document; you are encouraged to attempt all questions before reviewing the answers. Part 1: Multiple Choice 1. Consider a page that has 2 forms on it. The first is named ”getContact”. The second is named “getInfo”. If the second form has a text box named “marbles”, which of the following correctly references value of the text box? a. document.forms[0].getInfo.marbles b. document.forms[1].marbles.value c. document.forms[getInfo].marbles.value d. document.getContact.marbles.value 2. Consider the JavaScript statement var s = 5 + “5”. What value is assigned to s? a. 55 b. 10 c. statement is illegal and causes an error d. none of the above 3. Consider the JavaScript statement var s = 5 + parseInt(“5”). What value is assigned to s? a. 55 b. 10 c. statement is illegal and causes an error d. none of the above 4. JavaScript functions are required to include a return statement a. False b. True 5. The JavaScript statement s *= (y++ % 3) is equivalent to which of the following? a. s = s * ((y + 1) % 3) b. s = s * (y +1 % 3) c. y = y + 1; s = s * (y % 3) d. s = s * (y % 3); y = y + 1 6. Consider the following JavaScript statements: x = 5 and y = 6. What will the expression (x == y) || (x < 13) evaluate to? a. False b. True 7. Within JavaScript, the body of a do...while loop is guaranteed to execute at least once. a. False b. True CGS 3066 Practice Test 2 2 8. The Document Object Model provides access to every element in a document. a. False b. True 9. Inside which XHTML element is JavaScript placed? a. <script> b. <javascript> c. <js> d. <scripting> 10. Where is the correct place to insert a JavaScript? a. The <head> section b. The <body> section c. Any of the above 11. How can you add a comment in JavaScript? a. //This is a comment b. ‘This is a comment c. <!--This is a comment--> 12. An external JavaScript file must contain the <script> tag a. False b. True 13. JavaScript is case-sensitive a. False b. True 14. Within JavaScript, variables that are declared (using the keyword var) within a function: a. can only be accessed within that function. b. when you exit the function, the variable is destroyed. c. these variables are called local variables. d. all of the above 15. Which of the following property, of the window object, sets/returns the text of the status bar? a. window.statusBar b. window.status c. window.status() d. window.statusBar() 16. window.alert() and window.open() create dialog boxes t hat require user input for the page to continue processing a. False b. True CGS 3066 Practice Test 2 6 25. Within the function "func", which of the following would return the value of the selected option of the select drop-down menu? a. f.size[f.size.selectedIndex].value b. f.size.selectedIndex.value c. f.size.options[f.size.selectedIndex].value d. f.size.value 26. What does the following code output? var s = "Hello. "; s = "Hello and Goodbye. "; s = "Goodbye. " + s; document.write(s); a. Goodbye. Hello and Goodbye. Hello. b. Hello and Goodbye. Goodbye. c. Hello. Hello and Goodbye. Goodbye. d. Goodbye. Hello and Goodbye. 27. Consider the JavaScript statement below. How would you return the last, and only the last, character of string? var s = "red blue green"; a. s.charAt(s.length); b. s.charAt(s.length-1); c. s.charAt(s.length+1); d. s.charAt("n"); 28. Which of the following creates a dialog box that allows the user to type in input? a. window.alert() b. window.confirm() c. window.prompt() d. window.opener() 29. How do you write "Hello World" in an alert box? a. window.alert.value ="Hello World" b. window.alert("Hello World") c. window.alert.msg = "Hello World" d. window.alert() = "Hello World" 30. Consider the following JavaScript statements. What does the script output? var str = "some string" if (str.indexOf("ring") != -1) { document.write("True"); } else { document.write("False"); } a. False b. True CGS 3066 Practice Test 2 7 31. Consider the following JavaScript statements: x = 5 and y = 6. What will the expression (x == y) || (!x < 13) && (y > y) evaluate to? a. False b. True 32. Consider the following JavaScript statements: x = 5 and y = 6. What will the expression (x = y) || (x > 13) evaluate to? (Be careful). a. False b. True 33. Assume that a browser window was opened by another window. Which of the following returns the name of that window? a. window.opener b. window.open c. window.opener.name d. window.open.name 34. Consider the following JavaScript statements: var w = window.open("somepage.html", "win4"). How would you close the newly opened window? a. w.close() b. w.closed = true c. win4.closed = true d. win4.close() 35. Consider the following JavaScript statements: var x = 5; x += 6 + ++x; What is the value of x after these statements are executed? a. 5 b. 6 c. 16 d. 17 36. A JavaScript function, which is placed within the body section, is guaranteed to be executed at least once a. False b. True 37. A JavaScript function may be called by which of the following: a. an event handler b. other JavaScript code c. a link d. any of the above CGS 3066 Practice Test 2 8 Part 2: Code Writing Write code to produce the indicated output. Given the for-loop below, write an equivalent while loop. var p = 1; for (i = 0; i < 15; i++) { p = p * i; } Given the if-else if-else construct below, write an equivalent switch statement. if (c == "a" || c == "b") { document.write("matched a lowercase letter"); } else if (c == "A" || c == "B") { document.write("matched an uppercase letter"); } else { document.write("no match"); } CGS 3066 Practice Test 2 9 Write JavaScript code that will be placed in head section. The code should set the document title to be the filename. I.e., if the file is named somepage.html, then the document title should become somepage.html You are given 2 functions, named f1() and f2(). Write JavaScript code that will call f1() if some variable, call it y, is less than or equal to 3 and some variable, call it x, is greater than or equal to 13. CGS 3066 Practice Test 2 10 Write a JavaScript function that takes one argument, call it n . The script determines if n is a power of two. If n is a power of two, it should return the power. If not, it should return -1. The script need only consider numbers greater than or equal to 1. For example, isPowTwo(1) returns 0 isPowTwo(2) returns 1 isPowTwo(248) returns -1 isPowTwo(256) returns 8 Write a JavaScript function that sets the value of the status bar to the name of the current window. If the current window has no name, then the function sets the value of the status bar to the current URL. CGS 3066 Practice Test 2 20 Write a JavaScript function that determines how many times a given word is included within a larger piece of text. The function should take 2 arguments: the first is the text to be searched and the second is the word that you are looking for. Example function calls: s = "ring around the rosey" n = "ring" countWord(s, n) returns 1 s = "ring ring ring" n = "ring" countWord(s, n) returns 3 s = "ring around the ringy rosey" n = "ring" countWord(s, n) returns 2 s = "wrong wrong wrong" n = "ring" countWord(s, n) returns 0 //solution 1 function countWord(s1, s2) { var n = 0; var x = 0; while (s1.indexOf(s2, x) != -1) { x = s1.indexOf(s2, x) + s2.length; n++; } return n; } //solution 2 function countWord(s1, s2) { var n = 0; var x = 0; while (s1.indexOf(s2) != -1) { x = s1.indexOf(s2) + s2.length; s1 = s1.substr(x); n++; } return n; }