Questions: Answers in green 1. Match the technology with its description: (put a letter in front of each term) ____ HTML A. An all-purpose programming language on the magnitude of C++ ____ JavaScript B. An interpreted language used as programming glue ____ Java C. Technology you use to process the other three technologies ____ Web browsr D. The language that provides structure to Web documents If you had to write the name of each technology into the following diagram, which ones would you put where if you were looking at their dependence on each other (you can put more than one technology at each level)? Probably the best answer is: _____________________________________________ | ___________WEB BROWSER__________ | | | _______HTML____________ | | | | | | | | | | | JAVASCRIPT - JAVA | | | | | |_____________________| | | | |_______________________________| | |____________________________________________| But quite a few other configurations are right too. A couple of students made the hierarchy: JAVA WEB BROWSER HTML JAVASCRIPT which is really great as you certainly can have an all-Java Web browser! Quite a few students inverted the diagram. To be dependent means you have to rely on another technology to get things done. So, the Web browser is least dependent (most independent) as it can do something completely different than any HTML, Java, or JavaScript processing. HTML is without question always dependent on some other technology to do something since it is just a markup language. But, if you are advanced thinkers, you might realize that all kinds of applications can run HTML. It need not be a Web browser though Web browsers are the most popular applications used to process HTML. I can certainly see arguments for saying JavaScript is dependent on Java or Java is dependent on JavaScript (since both can interface with each other and dependence depends on what processing you want to make happen). In fact both Java and JavaScript can be used outside of any Web browser/HTML environment. Bottom line is I now have a better sense of how you think. No one lost any points for this part of question 1. But, I did give one point extra credit to those who did a great job of it. I was pleased with your collective effort. 2. A variable type that flexibly stores data in multiple representations is called a: (circle the letter before of the best answer) A. synonym B. variant -- everyone got this question right C. conglomerate D. type 4 variable 3. JavaScript provides the variable type you answered in question 2. List one benefit and one disadvantage of allowing such a type in a programming language. Benefit: Disadvantage: - Flexibility in interpretation - Uses up more computer memory - Faster processing (no casting) - Confusing to read as programmers - Allows lazy programming - Allows lazy programming - No type checking 4. Up to how many functions can be written validly inside a single HMTL SCRIPT element: (circle the letter before of the best answer) A. None B. None or one C. Exactly one D. There is no upper limit -- the SCRIPT element includes the open tag, JavaScript, and the close tag 5. Why might you want to wait for a Java applet to finish initializing before allowing JavaScript functions to run? Because the JavaScript may only make sense when an initialized value is returned from a called Java function 6. Given the following JavaScript script segment: var x=0, y=0; function doIT() { var x=21; x=doITtoo(); x=x-3; return x; } function doITtoo() { y=y+3; return y; } a. What value will doIT() return if there is no other code available to run? doIT() will return 0, the value of x at the end of the function. Following x: - starts at 0 - becomes 21 upon first line of doIt() - becomes 3 when doITtoo() returns 3 - becomes 0 when 3 is subtracted in doIT() b. True or False (circle either True or False based on the following statement): doIT() can possibly return a different value without changing any of the code shown here. (in other words, other code running within the script environment, although not shown here, can change what doIT() returns) TRUE - if any other code changes the global value of variable y, doITtoo() will return a different value than 3 and x-3 will not be 0. 7. Given the following JavaScript script segment: var x=0, y=1; if(x==0 && y==0) { x=1; } What is the final value of x if the code is interpreted sequentially? 0 - nothing changes it What is the final value of y if the code is interpreted sequentially? 1 - nothing changes it For x to become 1, both x and y would already have to be 0 (&& means AND within a condition) 8. Which one of the following statements is definitely true based on the following valid JavaScript line: document.forms[0].display.value = clock; A. There are multiple forms on the document where the line appears. B. There are no forms on the document where the line appears. C. The first form on the document has a control called display. D. Some input control named forms[0] is displaying a clock. I should not have used the word 'control' since I only used that word informally in class. But, the question comes directly from the JavaScript tutorial (I copied and pasted the line) where there is in fact only one form in the example (hence answer A is incorrect). B is possible but not definite. C must be definite for it to be valid. I guess D was clever on my part since I actually got one taker. I was not expecting that. By the way, I got paid $2000 one winter break to write 2000 multiple choice questions for a company in Tampa, Florida (on 3D Studio Max, HTML, VRML, and DHTML). I learned a bit about the process based on review comments I received. $1 a question seems like a good payoff but man o man was that one tedious piece of work. 9. Which of the following JavaScript operators would be most helpful in writing code to force a choice between seven options: (circle the letter before the best answer) A. % as in x %7 B. / as in x / 7 C. + as in x + 7 D. ++ as in x++7 I expected most of you to get this one via process of elimination. Kudos to students that wrote down exactly what the % operator does. I use it very often in writing code when I want to choose between X options (for simulating allele selection in recombinant sex genetics for example). Some of you knew what % does but couldn't see how that would help force a choice. It helps force a choice by returning known whole integer values. No matter what x is, we get back 0, 1, 2, 3, 4, 5, or 6 when we mod (% is called a modulus operator) by 7. When we divide by 7 we can get back any value, but at 1/7th the value of x. If we add 7 we get back any value, but at 7 more than x. If we ++ (increment) x we get back any value, but at one more than x. Most students got this right (even without knowing what % is -- which is fine since you know it now, eh?). 10. How many times will the following JavaScript function run when called? var timer; function start() { timer = setTimeout("start()",10); } A. 0 as it is invalid B. 1 C. Up to 10 times or until the function is no longer active in computer memory. D. 1 or more including possibly more than 10 times Ah... this is the question least answered correctly. I copied and pasted it directly from the JavaScript tutorial but it isn't consistent with the two class examples I gave you. So, I could have made it clearer for you. I should have written timer = window.setTimeout("start()",10); and then I suspect more of you would have gotten it correct (though more did get it correct than just dumb luck). I gave you back two points if you chose A or B. But, C is definitely incorrect. The 10 refers to time in milliseconds, not anything to do with the number of times the function should run. 11. Which Math library function (method) helps make sure a variable Array index won't cause an exception? (i.e. if you saw the line: lines[Math.____(x)] which Math library function would you expect to see fill in the blank?) (circle the letter before the best answer:) A. random B. max C. min D. round -- the only answer guaranteed to return a whole number and Array indices must be whole numbers 12. How can you properly open a second Web browser window with a single JavaScript statement? A. window.open("http://www.thedomain.com/") - That's the ticket B. document.openWin("http://www.thedomain.com/") C. openWindow("http://www.thedomain.com/") D. document.open("http://www.thedomain.com/") (by the way, I didn't mention in class the fact that you can use single-quotes and double-quotes interchangeably. So, I could have shown single quotes ( ' ) instead of the double quotes I used above. This interchangeability lets you include either quote mark inside of the other quote marks. Many languages don't have that feature and instead use an escape character to mean 'use this character but don't let it effect the syntax of the code it is within'. In Java the escape character is the back-slash ( \ ) and so you would write \" to include a double-quote inside of a string.) 13. What does the following JavaScript function do? function refresh() { location.reload(); } A. It reloads the current Web page, thereby setting variables back to their original values. B. It reloads the current Web page while keeping variables at their current values. C. It reloads the location within the Web page that called the refresh() function. D. It reloads a JavaScript function named location into computer memory. B is incorrect because the Web browser does not store variables from page to page unless you use some kind of additional technology (cookies comes to mind). C is a good naive answer as you might expect it to be the case. But, if it were a location within the page, you would have to reference the document with it (i.e. document.location). This question comes directly from the JavaScript tutorial. 14. Where on a Web page does the following JavaScript line belong? document.write(weather_report); A. In the HEAD element B. Between the HEAD and BODY elements C. In the BODY element -- You always write in the body. If you write in the head, it isn't shown D. After the BODY element 15. Using the built in Web browser JavaScript functions, which system information can be verified within the script A. Screen Resolution B. Browser version C. Operating System D. Screen Color Depth All of the above can be interrogated reliably from within JavaScript. 16. Which of the following statements are true: A. Scripts in the body section will execute while the page loads. B. Scripts in the head section will be executed when called. C. Scripts between the head and body section will execute while the page loads. A and B are verbatim from the JavaScript tutorial. C is incorrect as those scripts must be called as if they were in the HEAD. 17. Which HTML attribute calls a function that will run at load time? (circle the letter before the best answer:) A. LOADER B. STARTUP C. ONLOAD -- everyone got that correct D. STARTER 18. The correct way to make external JavaScripts available in a Web page is (circle the letter before the best answer:) A. <script src="xxx.js"> where the actual script is in an external script file called "xxx.js". B. document.import("xxx.js") where the actual script is in an external script file called "xxx.js". C. document.script.import("xxx.js') where the actual script is in an external script file called "xxx.js". D. window.import("xxx.js") where the actual script is in an external script file called "xxx.js". 19. What is a correct result of interpreting all the following JavaScript lines? var x = "The axis at 23.5" var y = 0; findTheX(x); function findTheX(the_text) { y=the_text.indexOf("x"); } (circle the letter before the best answer:) A. y is -1; B. y is 0; C. y is 5; -- I admit to doing a bad job of counting. I should have offered 4 and 5 as answers, not 5 and 6 D. y is 6; 20. Which of the following JavaScript lines would return the word "axis" from the phrase "The axis is 23.5"? (circle the letter before the best answer:) A. substring("axis"); B. substring(4,8); -- The benefit of asking questions before the exam, eh? C. substring(8).substring(4); D. substring(word(1)); 21. What is the most likely effect of the following JavaScript line? document.theForm.what.value="The cat"; A. "The cat" function runs and returns a value. B. A form control named 'what' now shows the phrase "The cat". -- Just like in our class examples C. A document form is replaced with a new form called "The cat". D. The phrase "The cat" returns a value of 0 since it is not numeric. 22. Which of the following function calls is not calling a JavaScript function? A. window.alert(calculateRent()); B. document.script[0].calcuateRent(); C. document.applets.calculateRent(); D. window.open("http://www.rentcalc.com"); Again, I could have made this a better question. C is trying to pass the function off to Java since it explicitly uses reference to the applets object. It is questionable as to whether it would run or not since it doesn't specify which Java applet to pass it to. Some of you are right in thinking the window referenced functions are run by the browser, but they are JavaScript functions passed to the browser (and considered part of the JavaScript language -- while calculateRent() is not part of the JavaScript language -- so it is the 'best' answer). A is definitely wrong since calculateRent() will be assumed to be a JavaScript function. D is the answer I think has some merit. B explicitly points to which SCRIPT element the JavaScript calculateRent() function should run from (so, you could actually have more than one calculateRent() function in different SCRIPT elements). 23. JavaScript is a programming language: (circle ALL letters before true answers:) A. with syntax similar to Java. B. that is a subset of the Java programming language. C. is always interpreted one line at a time. D. is always interpreted one statement at a time. E. where lines and statements are equivalent. B is incorrect since some features of JavaScript are not in Java. C is incorrect since you can put more than one statement on a line (that's the only reason for the semi-colon). E is incorrect for the same reason (one line can have n number of statements). 24. How many looping statements are there in JavaScript? (circle the correct number:) 1. The 'for' loop 2. The 'for' loop, and the 'while' loop 3. The 'for' loop, the 'while' loop, and the 'do...while' loop. -- straight from the JavaScript tutorial 4. The 'for' loop, the 'while' loop, the 'do...while' loop, and the loop...until loop. 25. Your turn. Provide a question and answer that shows insight into the material you have studied on JavaScript. You will be graded on the sophistication of the question but you must also provide the correct answer: Thank you for all your questions. Some showed great insight. A couple just reworded ideas in the exam already. I list here some ideas from your classmates that might actually teach you something: str.substring(5,2) actually works the same as str.substring(2,5) in my browser. Given a for loop, how can you re-write it as a while loop (this is ALWAYS possible to do). How about vice versa? Q: Why is it good to preload images in the HTML HEAD SCRIPTS? A: Images are then stored in memory to be used dynamically when called upon. T or F: If you SRC to an external .js file, the external file must include the SCRIPT open and close tags. A: FALSE Q: Why is JavaScript Open Source? A: JavaScript code is usually not copyrighted. It's primarily used for Web applications. One of the Web's purposes is to share information, so it doesn't make sense for JavaScript to be patented for specific use. Q: Why would one use an attribute of TYPE=HIDDEN in an INPUT element? A: To store data in that control without it being seen on the page. JavaScript can change it and, if part of a form, can return the updated value to the form processing routine. A: Give examples of both ways to initialize an Array in JavaScript: Q: name = new Array(2); -or- name = new Array("A","B"); name[0] = "A"; | name[1] = "B"; | Q: Give an example of a user event and how JavaScript processes it A: Form processing where a form control is checked for valid input through the ONSUBMIT=validate() attribute in the element. ONSUBMIT is active when the user clicks on the Submit button. Q: What does the syntax <!-- --> mean to JavaScript? A: Ignore it as it is only intended for older browsers that don't interpret JavaScript.