JavaScript Test Answer Key					
INDE 498 A
Winter 2003

CORRECT ANSWERS ARE IN GREEN.

1. Why would someone want to use JavaScript on a Web page?
(circle all letters before valid reasons for using JavaScript)

A. To react to events that occur with Web page use
B. To read and write HTML 
C. To validate data in a Web form
D. To pass data to a Java applet program
E. To run a clock or timer in a Web page

A, B, and C are straight from the introduction of the JavaScript tutorial.
Answer D is from lecture and E is from an example deeper in the tutorial.

2. Why would someone use a semi-colon when writing JavaScript?
(circle all letters before true statements)

A. To put more than one statement on the same line
B. To quickly reuse code when copying and pasting from JavaScript to Java
C. Because a for statement requires them
D. Because a switch statement requires them
E. Because a return statement requires them

A and B are from lecture. C is from the JavaScript tutorial.
       
3. How would you properly identify a segment of JavaScript to a Web browser?
(circle the letter before the one best answer)

A. Use an open SCRIPT tag with a type="text/javascript" attribute
B. Write JavaScript anywhere and the browser always recognizes it
C. Use an open JAVASCRIPT tag with a type="text" attribute
D. Use an open APPLET tag with a type="text/javascript" attribute

4. Evaluate the following String method uses by writing the value of s after the evaluation
(write one value on the line to the right of each evaluation)	

var str="Mix and Match"

A. s = str.indexOf("x")	    __2_
B. s = str.indexOf("g")	    _-1_
C. s = str.indexOf("M")	    __0_
D. s = str.lastIndexOf("a") __9_
E. s = str.indexOf("and")   __4_

5. How can someone properly open a second Web browser window with a JavaScript statement?
(circle the letter before the one best answer)

A. document.openWin("http://www.thedomain.com/")
B. openWindow("http://www.thedomain.com/") 
C. window.open("http://www.thedomain.com/")
D. document.open("http://www.thedomain.com/") 
E. document.location("http://www.thedomain.com/")

6. Which of the following JavaScript statements will assign the word "and" to variable s?
(circle the letter before the one best answer)

var str="Mix and Match"

A. s = str.substring(4,3);
B. s = str.substring(5,3);
C. s = str.substring(4,7);
D. s = str.substring(5,7);
E. s = str.substring(5,8);

7. Given the following valid HTML form, how would you assign the value typed into the owner 
    control to a variable ssn in a JavaScript statement?
(circle the letter before the one best answer)

<FORM Name=SSN Action=processme.html>
   <INPUT Name=owner>
</FORM>

A. ssn = window.SSN.owner.value;
B. ssn = document.SSN.owner.value;
C. ssn = document.SSN.input.value;
D. ssn = document.form.input.value;
E. ssn = window.owner.value;

You must include every object down in the DOM (Document Object Model) hierarchy.

8. After a JavaScript function runs the following two lines of code, what value does the variable 
   theDay contain?
(circle the letter before the one best answer)

var d=new Date();
theDay = d.getDay();

A. 1
B. 2
C. 26
D. 27
E. "Monday"

getDay() starts counting with Sunday as 0, Monday as 1, etc. 
The String representation in the variant is "1", not "Monday".

9. After a JavaScript function runs the following two lines of code, what value does the variable 
   x contain?
(circle the letter before the one best answer)

arr = new Array(5);
x = Math.random() / arr.length;

A. a floating point number between 0.0 and 0.2
B. a floating point number between 0.0 and 0.25
C. a floating point number between 0.0 and 1.0
D. a floating point number between 0.0 and 5.0
E. x would not be assigned due to one or more errors in the code

Math.random returns a value between 0.0 and 1.0. Array.length returns the number
of Objects in the array structure.

10. Why would someone use more than one pair of  tags in a Web page?
(circle the letter before the one best answer)

A. there is no reason to use more than one pair
B. to process the same algorithm in more than one scripting language
C. to call one JavaScript function from within another JavaScript function
D. to use a variable name like 'x' more than once
E. to span a switch statement across multiple functions

As mentioned in lecture, scope is the most important reasons to use more than one SCRIPT element.

11. When would someone use JavaScript to assign a new value to the window.location property   
    of a Web page?
(circle the letter before the one best answer)

A. To move a Web browser window to a new place on the screen
B. To redirect a Web page reader to a different page than the one they requested
C. To bookmark a page in a reader's list of favorites
D. To scroll the reader to a new location in the currently viewed Web page
E. The location property only applies to the document object, never the window object

12. Where on a Web page does the following JavaScript line belong? 
(circle the letter before the one best answer)

document.write(IBM_stock_price)

A. In the HEAD element of the Web page
B. Between the HEAD and BODY elements of a Web page
C. In the BODY element of a Web page
D. After the BODY element of a Web page
E. In an external JavaScript file

13. What is the value of variable x after the following block of JavaScript code is run?
(circle the letter before the one best answer)

x=0;
y=2;
while(y>0.5 || y<0.0) {
     x = x+1;
     y = y/x - y;
}

A. 0
B. 1
C. more than 1
D. undetermined since the code divides by zero
E. less than 1

The loop runs once since y is greater than 0.5. This adds one to the value of x.
x is now 1. y is now 0.0 which is neither > 0.5 nor < 0.0. So, the loop does not run again.

14. Which of the following lines of code in a JavaScript function written on a Web page could be 
    passing data to a function written in Java?
(circle the letter before the one best answer)

A. document.java.update(points);
B. document.script[0].update(points);
C. window.alert(updatePoints(100));
D. external function update(points);
E. document.applets.update.update(points)

You need to reference the document object, its applets property, the name of the specific
applet (update in this case) and the actual function to be run in the Java.

15. So, overall, the JavaScript language is basically intended to be
(circle the letter before the one best answer)

A. a compiled programming language written close to the machine architecture
B. an interpreted programming language written close to the machine architecture
C. a compiled programming language with a high-level abstraction from machine architecture
D. an interpreted programming language with a high-level abstraction from machine architecture
E. an intermediary language compiled to byte code and interpreted to the machine architecture