Sample Exam 3 1) Write the details of a function that prompts the user for a color and uses what they typed to set the link color within the existing web page. function changeLinkColor() { var color=prompt(``enter a color please'',''''); document.link=color; } 2) We are given the following function... function squares(size) { var sqr; for (var i=1; i<= size; i++) { sqr = i * i; alert(sqr); } } Write a program trace explaining what each line of the program does and give sample result define a function called "squares" that takes a single parameter called "size" this function has a variable called "sqr" defined. we loop "size" many times using a loop counter variable "i" Inside the loop, we compute the square of "i" and store the result in "sqr" Pop-up an alert containing the square of "i" and do that until i=size a sample result would be the following numbers each in its own alert box: 1 3) Write a function that prompts the user to input a number, and then gives them 5 chances to guess the square of the number. <script language="JavaScript"> var theNumber = prompt("pick a number",""); var num = parseInt(theNumber); var theSquare; var theirNumber; var guess; theSquare= num * num; for (var i=1; i <= 5; i++) { theirNumber= prompt("what is " + num + " squared","0"); guess= parseInt(theirNumber); if (guess == theSquare) { alert("Correct"); break; } else { alert("Attempt " + i + " is wrong"); } } </script> 4) Write a function that takes 3 parameters, price, tax rate, balance, and prints yes in an alert box if someone has adequate balance to purchase an item at the specified price with the given tax rate. Otherwise, print out no in an alert box. <script language="JavaScript"> function affortIt(price, taxrate, balance) { var total = price + price *taxrate; if (total <= balance) { alert("yes"); } else { alert("no"); } } </script> 2 5) You are given the following form... <form name="exam"> <input type="text" name="first"> <input type="text" name="last"> <input type="button" value="click me" onClick="display()"> </form> Modify this form in the following fashion a. Add a function connected to the button to display all the data associated with the components in the form. This function is to be called whenever the user clicks the button. <script language="JavaScript"> function display() { alert(document.exam.first.value + " document.exam.last.value); } </script> " + b. modify the form so that when the user clicks in each of the fields, their contents are hi-lighted. <form name="exam"> <input type="text" name="first" onFocus="this.select()" onChange="display()"> <input type="text" name="last" onFocus="this.select()" onChange="display()"> <input type="button" value="click me" onClick="display()"> </form> 6) Write a function that generates a web page containing a table with 3 columns and 500 rows. Label each location in the table with row and column location. function genTable() { document.open(); document.write("<html><head><title>the title</title></head><body><table>"); for (var i=1; i <= 500; i++) { document.write("<tr> <td> row" + i + "col 1 </td>"); document.write("<td> row" + i + "col 2 </td>"); document.write("<td> row" + i + "col 3 </td></tr>"); } document.write("</table></body></html>"); document.close(); } 3 7) Write the details of the following function that prompts the user for a color using the following format: <body ID="bdy"> <p> Pick a background color for this page. </p> <form name="form1"> <b> Color </b> <input type="text" name="body"><br> <input type="button" value="Change color onclick="changeBgColor()"> <br> </form> </body> and then uses that color to set the background color of the web page. function changeBgColor() { } function changeBgColor(){ var bodycolor = document.form1.body.value; document.getElementById("bdy").style.backgroundColor = bodycolor; } 8) We are given the following function... function fib(size) { var output = new Array(size); output[0]= 0; output[1]= 1; for (var i=2; i< size; i++) { output[i]= output[i-1] + output[i-2]; } return output; } Write a program trace that explains what each line of the program does and give a sample result that is returned. define a function called "fib" that takes a single parameter called "size" this function has a array object called "output" defined. we loop "size" many times using a loop counter variable "i" Inside the loop, we compute the sum of output[i-1] and output[i-2] and store the result in output[i] we return the array output 4 9) Write a function that keeps prompting the user for input until the letter "p" is typed by the user. function iqTest() { var letterP = prompt("Please enter the letter p"," "); while (letterP!=="p") { letterP = prompt("No dummy! Enter the letter p",""); } alert("Thank you!") } 10) Write a function that takes 4 variables, a, b, c, d, and prints true in an alert box if input a > input b and input c > input d. Else, print false in an alert box <script language="JavaScript"> function question9(a,b,c,d) { if (a>b&&c>d) { alert("true") } else { alert("false") } } </script> 11) Write a function that generates a complete web page containing a list with 50 items. The items should be item1 through item50 <script> function genList() { document.open(); document.write("<html><head></head><body>"); document.write("<ul>"); for (var i=1; i <= 50; i++) { document.write("<li> item" + i + "</li>"); } document.write("</ul></body></html>"); document.close(); } </script> 5 12) Write a function tip(bill) that will calculate and return the amount that should be left as a tip for a restaurant bill when passed the parameter bill. The tip should be 20% of the total before a tip is added. <html> <head> <title>Question 9</title> <script language="JavaScript"> function tip(bill) { var bill, t; t = .2*bill; return t; } </script> </head> <body> <script> var a = prompt("how much is the bill?",""); var af = parseFloat(a); t = tip(af); alert("Please leave a tip of $" +t) </script> </body> </html> 13) Write a function that prompts the user for a color and uses what they typed to set the background color of the web page. <html> <head><title>Changing Background Color</title> <script language="JavaScript"> function changeBgColor(){ var bodycolor = document.form1.body.value; document.getElementById("bdy").style.backgroundColor = bodycolor; } </script> </head> <body ID="bdy"> <p> Pick a background color for this page. </p> <form name="form1"> <b> Color </b> <input type="text" name="body"><br> <input type="button" value="Change color" onclick="changeBgColor()"> <br> </form> </body> </html> 6 14) Write a function that takes 4 variables, a, b, c, d, and prints true in an alert box if input a ≥ input b and input c ≤ input d. Else, print false in an alert box. <html> <head> <title>Question I-3</title> <script language="JavaScript"> function question9() { var a = prompt("Give me an \"a\"",""); var af = parseFloat(a); var b = prompt("Give me an \"b\"",""); var bf = parseFloat(b); var c = prompt("Give me an \"c\"",""); var cf = parseFloat(c); var d = prompt("Give me an \"d\"",""); var df = parseFloat(d); if (af>=bf&&cf<=df) { alert("true") } else { alert("false") } } </script> </head> <body> <form> <input type="button" value="determine conditional" onclick="question9()"> </form> </body> </html> 15) You are given the following function … <script type="text/javascript"> function fact(n) { var n, retval=1; for(i=n;i>0;i--){ retval=retval*i; } return retval; } </script> 7 a) Write a program trace that explains what each line of the program does and give a sample of the result that is returned. Input “n” via some mechanism, e.g., a prompt box Pass the value of n to function fact(n) Begin with i = n, retval =1 and loop decrementing i by 1 Compute retval = retval x I, initially 1 x n, then n x n-1 … When I =0 return retval, which is n! For example if n=6 16) Write a function that loops through heading tags (h1-h6) and generates the following web page (Note: “this is heading n” should be formatted using an <hn> tag as in <h1>This is heading 1</h1>). <html> <body> <script type="text/javascript"> for (i = 1; i <= 6; i++) { document.write("<h" + i + ">This is heading " + i); document.write("</h" + i + ">"); } </script> </body> </html> 8 17) Assume you have created an array of pets: <script type="text/javascript"> var mypets = new Array(); mypets[0] = "dog"; mypets[1] = "cat"; mypets[2] = "goldfish"; </script> a) Add a for loop to print out the array (add code below): [5 pts.] <script type="text/javascript"> var mypets = new Array(); mypets[0] = "dog"; mypets[1] = "cat"; mypets[2] = "goldfish"; for (i=0;i<mypets.length;i++) { document.write(mypets[i] + "<br />"); } </script> b) Modify the JavaScript to create a second array, mynewpets, containing turtle and gerbil. Modify mypets to append mynewpets, so that the for loop will now print out the modified 5-element array mypets using the same for loop as above. Add code below: [10 pts.] <script type="text/javascript"> var mypets = new Array(); mypets[0] = "dog"; mypets[1] = "cat"; mypets[2] = "goldfish"; var mynewpets = new Array(); <html> <body> <script type="text/javascript"> var mypets = new Array(); mypets[0] = "dog"; mypets[1] = "cat"; mypets[2] = "goldfish"; var mynewpets = new Array(); mynewpets[0] = "turtle"; mynewpets[1] = "gerbil"; mypets = mypets.concat(mynewpets) for (i=0;i<mypets.length;i++) { document.write(mypets[i] + "<br />"); } </script> </body> </html> 9 1) Write a function share(bill,sizeGroup) that takes two parameters, bill and sizeGroup, calculates and returns the amount (share) that each person should pay when the bill is divided equally among sizeGroup diners using an alert box. A tip of 20% of the total should be computed and added to the bill before computing each person’s share. Write ONLY the function (see next question) [5 pts.] function share(bill,sizeGroup) { var share= (bill*1.2)/sizeGroup; share=share.toFixed(2); alert("Each person owes $" + share) } 2) Suppose one calls the function share(bill,sizeGroup) in 1) above using the code below. a) Add the necessary statements to insure that the inputs, bill and sizeGroup, are converted to the correct type and precision, e.g., bill is floating point with two decimal points (12.65 and not 12.6534) and sizeGroup is an integer before passing to share(bill,sizeGroup). [4 pts] <script> var bill =prompt("How much is the total Bill"," "); bill=parseFloat(bill); bill=bill.toFixed(2); var sizeGroup=prompt("How many people are dining?"," "); sizeGroup=parseInt(sizeGroup); share(bill,sizeGroup); </script> b) Could precision/type be handled in the function? Yes or no. [1 pt.] yes 3) Modify the script below, so that “I love CMPSCI120” is printed. Hint: use one of the string object methods [5 pts.] <html> <head> </head> <body> <script type="text/javascript"> var str="I hate CMPSCI120"; document.write(str.replace("hate","love") </script> </body> </html> 10 ); 4) Write a function question4() that asks for 4 inputs (a, b, c, d) using prompt boxes, converts them to floating point, and prints true in an alert box if (input a x input b) < (input c x input d). Else, print false in an alert box. [5 pts.] <html> <head> <title>Question JI-4</title> <script language="JavaScript"> function question9() { var a = prompt("Give me an \"a\"",""); var af = parseFloat(a); var b = prompt("Give me an \"b\"",""); var bf = parseFloat(b); var c = prompt("Give me an \"c\"",""); var cf = parseFloat(c); var d = prompt("Give me an \"d\"",""); var df = parseFloat(d); if ((af*bf)<(cf*df)) { alert("true") } else { alert("false") } } </script> </head> <body><form> <input type="button" value="determine conditional" onclick="question9()"> </form> </body> 11 5) You are given the following function … <script type="text/javascript"> function primeNum() { var p =prompt("enter a postive integer less than 100", " "); p=parseInt(p); while (p>=100) {var p =prompt("I said less than 100!!", " ");} var isPrime=1; for (var i=2; i<p; i++) { var modI=p%i; if (modI == 0) { isPrime=0;break; } } if (isPrime) {alert(p + " is prime!")} else {alert(p + " is not prime!")} } </script> Explain via a step by step program trace what the function primeNum() does. 12 6) Write a function TablePrint(r,c) that takes two parameters, r and c, and prints a table with r rows and c columns where each table entry is “[r,c]”. Here is an example output for r= 5, c=6: Write ONLY the function, not the full script with inputs. function genTable(r,c) { document.write("<table>"); for (var i=1; i <= r; i++) { document.write("<tr> <td> [row " + i + ", col " +1 + "]</td>"); var j =2; for (var j=2; j <= c-1; j++) { document.write("<td> [row " + i + ", col " + j + "]</td>"); } document.write("<td> [row " + i + ", col " + c + "]</td></tr>"); } document.write("</table>"); } 13 Assume you have created an array of UMass sports: var umassSports = new Array(); umassSports[0] = "Football"; umassSports[1] = "Hockey"; umassSports[2] = "Basketball"; umassSports[3] = "Swimming"; umassSports[4] = "Track"; umassSports[5] = "Skiing"; a) Add code below to print out the array as shown: [4 pts.] <script language="JavaScript"> var umassSports = new Array(); umassSports[0] = "Football"; umassSports[1] = "Hockey"; umassSports[2] = "Basketball"; umassSports[3] = "Swimming"; umassSports[4] = "Track"; umassSports[5] = "Skiing"; document.write("<b>UMass Sports Include: "+ umassSports + "<br>"); </script> b) Modify the JavaScript to create a new array, aftercutsSports, that removes Football and Skiing and prints out the results as shown below. Hint: use the appropriate method for the Array object. [6 pts.] <script language="JavaScript"> var umassSports = new Array(); umassSports[0] = "Football"; umassSports[1] = "Hockey"; umassSports[2] = "Basketball"; umassSports[3] = "Swimming"; umassSports[4] = "Track"; umassSports[5] = "Skiing"; document.write("<b>UMass Sports Include: "+ umassSports + "<br>"); var aftercutsSports=umassSports.slice(1, 5); document.write("<b>UMass Sports After the budget cuts Include: "+ aftercutsSports + "<br>"); </script> 14 4) Complete the following function, so that the function greetKitty()inputs from the text box (you need to reference the text box to extract the value) and outputs an alert box that says Hello name as shown below. [10 pts.] <html> <head> <script type="text/javascript"> function greetKitty() { var name = document.getElementById("myText").value; alert("Hello " + name); } </script> </head> <body> <p> what is your Cat's name?</p> <form> <input size="25" type="text" id="myText" value="Garfield"> <input type="button" value="Input" onclick="greetKitty()"> </form> </body> </html> 15 W3Schools JAVASCRIPT & (limited) PHP Quizzes 1) Inside which HTML element do we put the JavaScript? a) <js> b) <javascript> c) <scripting> d) <script> 2) What is the correct JavaScript syntax to write "Hello World"? a) document.write("Hello World") b) response.write("Hello World") c) "Hello World" d) ("Hello World") 3) Where is the correct place to insert a JavaScript? a) Both the <head> section and the <body> section are correct b) The <body> section c) The <head> section 4) What is the correct syntax for referring to an external script called "xxx.js"? a) <script name="xxx.js"> b) <script href="xxx.js"> c) <script src="xxx.js"> 5) An external JavaScript must contain the <script> tag False True 6) How do you write "Hello World" in an alert box? a) alertBox="Hello World" b) alert("Hello World") c) alertBox("Hello World") d) msgBox("Hello World") 7) How do you create a function? a) function:myFunction() b) function=myFunction() c) function myFunction() 8) How do you call a function named "myFunction"? a) myFunction() b) call function myFunction c) call myFunction() 16 9) How do you write a conditional statement for executing some statements only if "i" is equal to 5? a) if i==5 then b) if (i==5) c) if i=5 then d) if i=5 10) How do you write a conditional statement for executing some statements only if "i" is NOT equal to 5? a) if (i <> 5) b) if <>5 c) if =! 5 then d) if (i != 5) 11) How many different kind of loops are there in JavaScript? a) Four. The "for" loop, the "while" loop, the "do...while" loop, and the "loop...until" loop b) Two. The "for" loop and the "while" loop c) One. The "for" loop 12) How does a "while" loop start? a) while i=1 to 10 b) while (i<=10;i++) c) while (i<=10) 13) How does a "for" loop start? a) for i = 1 to 5 b) for (i = 0; i <= 5) c) for (i = 0; i <= 5; i++) d) for (i <= 5; i++) 14) How can you add a comment in a JavaScript? a) 'This is a comment b) <!--This is a comment--> c) //This is a comment 15) What is the correct JavaScript syntax to insert a comment that has more a) //This comment has more than one line// b) <!--This comment has more than one line--> c) /*This comment has more than one line*/ 17 16) What is the correct way to write a JavaScript array? a) var txt = new Array(1:"tim",2:"kim",3:"jim") b) var txt = new Array("tim","kim","jim") c) var txt = new Array="tim","kim","jim" d) var txt = new Array:1=("tim")2=("kim")3=("jim") 17) How do you round the number 7.25, to the nearest whole number? a) round(7.25) b) Math.round(7.25) c) rnd(7.25) d) Math.rnd(7.25) 18) How do you find the largest number of 2 and 4? a) Math.max(2,4) b) ceil(2,4) c) top(2,4) d) Math.ceil(2,4) 19) What is the correct JavaScript syntax for opening a new window called "window2" ? a) new.window("http://www.w3schools.com","window2") b) window.open("http://www.w3schools.com","window2") c) open.new("http://www.w3schools.com","window2") d) new("http://www.w3schools.com","window2") 20) How do you put a message in the browser's status bar? a) window.status("put your message here") b) statusbar = "put your message here" c) window.status = "put your message here" d) status("put your message here") 21) How do you find the client's browser name? a) client.navName b) browser.name c) navigator.appName 22) What does PHP stand for? a) PHP: Hypertext Preprocessor b) Private Home Page c) Personal Hypertext Processor d) Personal Home Page 18 23) PHP server scripts are surrounded by delimiters, which? a) <script>...</script> b) <?php…?> c) <&>...</&> d) <?php>...</?> 24) How do you write "Hello World" in PHP a) "Hello World"; b) echo "Hello World"; c) Document.Write("Hello World"); 25) PHP allows you to send emails directly from a script a) True b) False 26) PHP can be run on Microsoft Windows IIS(Internet Information Server): a) False b) True 27) How do you create a function? [4 pts.] a) function:myFunction() b) function=myFunction() c) function myFunction() 28) How does a "for" loop start? [4 pts.] a) for i = 1 to 5 b) for (i = 0; i <= 5) c) for (i = 0; i <= 5; i++) d) for (i <= 5; i++) 29) How do you write a conditional statement for executing some statements only if "i" is NOT equal to 5? [4 pts.] a) if (i <> 5) b) if <>5 c) if =! 5 then d) if (i != 5) 30) PHP server scripts are surrounded by delimiters, which? [4 pts.] a) <script>...</script> b) <?php…?> c) <&>...</&> d) <?php>...</?> 19 31) How do you put a message in the browser's status bar? [4 pts.] a) window.status("put your message here") b) statusbar = "put your message here" c) window.status = "put your message here" d) status("put your message here") 32) For the following JavaScript code [4 pts.] <html> <head> <title>Question MC-3</title> </head> <body> <script language="JavaScript"> var grade=prompt("What grade do you expect in CMPSCI120?", "grade"); </script> </body> </html> which one of the following events occurs? a) b) c) 33) How can you add a one-line comment in a JavaScript? [4 pts.] a) 'This is a comment b) <!--This is a comment--> c) //This is a comment 20 34) How do you find the largest number of 2 and 4? [4 pts.] a) Math.max(2,4) b) ceil(2,4) c) top(2,4) d) Math.ceil(2,4) 35) What is the correct way to write a JavaScript array? [4 pts.] a) var txt = new Array(1:"tim",2:"kim",3:"jim") b) var txt = new Array("tim","kim","jim") c) var txt = new Array="tim","kim","jim" d) var txt = new Array:1=("tim")2=("kim")3=("jim") 36) How do you call a function named "myFunction"? [4 pts.] a) myFunction() b) call function myFunction c) call myFunction() True/False Questions: Circle One 1) You can get a macro virus by opening an RTF file. T or F T 2) A “virus” is program or code that replicates by infects another program, boot sector, partition sector, or document that supports macros, by inserting itself or attaching itself to that medium T or F T 3) A “Trojan Horse” can enter your computer via an email attachment T or F T 4) Facebook and similar social networks can guarantee complete privacy if you take proper precautions. T or F F 5) “Interruption” is a security attack on the “integrity” of computer-based systems and networks. T or F F 6) The Caesar and Atbash ciphers are simple encryption schemes that are highly secure. T or F F 7) A “One-Time Pad” is an example of a public-key encryption strategy. T or F F 8) The keys to the security of polyalphabetic ciphers are the cyclic pattern and the “”n” monoalphabetic ciphers. T or F T 9) A “virus” is program or code that replicates by infects another program, boot sector, partition sector, or document that supports macros, by inserting itself or attaching itself to that medium. T 10) ”Modification” is a security attack on Availability. F 11) If you take proper precautions, you can assure your complete privacy on MySpace, Facebook and similar social. F 21 Multiple Choice Questions (4 pts. each). Circle the letter of the best answer. 1) A Word macro virus: a) can be passed from a PC to a Mac. b) is a relatively unusual computer virus. c) cannot be stopped by disabling Word macros in your preference settings. d) is only activated when Word files are attached to e-mail messages. 2) Which of the following is not a primary security goal: a) Availability b) Scalability c) Confidentiality d) Integrity 3) ”Modification” is a security attack on: a) Availability b) Scalability c) Confidentiality d) Integrity 4) A “One-Time Pad” provides “perfect security” if: (1) the key is never reused; (2) the key is based on Dr. Znivago; (3) the key is truly random; and (4) the key is kept secret a) All but (1) b) All but (2) c) All but (3) d) All but (4) 5) Which of the following is a not a requirement for insuring that a “One-Time Pad” provides “perfect security”: a) The key is destroyed after use b) The key is truly random c) The key is a multi-alphabetic cypher d) The key is known only by sender and receiver 6) Which of the following is one of three primary security goals: a) Robustness b) Scalability c) Confidentiality d) Reliability Short Answer Questions – Answer each question in 2-4 sentences. 1. How do you know your computer is infected with a virus? Reported by a virus scanner Unusual or atypical behavior (slowness, crashes, freezes, … ) But there is no way to insure you do or don’t have a virus 22 1. What should do you to protect your computer from virus attacks? List three different things you can do. Use a antivirus scanner Update the virus definitions Contact your IT office Remove your computer from the internet Keep bootable media out of your drive on startup Use a firewall 2. What is a denial-of-service attack (DoS attack)? an attempt to make a computer resource unavailable to its intended users. A common method of attack involves saturating the target (victim) machine with external communications requests 3. How often should you update your virus definitions? Frequently. Most antivirus software will let you opt for automatic updates although this can put a significant demand on your processor. 4. Why are digital certificates important in e-commerce? Digital certificates are used by browsers to validate the identity of a website 5. What is the difference between strong and weak encryption? Strong encryption means that it cannot be broken using conventional or special systems, methods or algorithms in time less than the time the encrypted information must remain secure Weak encryption is when the plaintext can be determined/extracted/decrypted in time less than the time the encrypted information must remain secure Medium-Long answer questions. For Exam 2, these will include CSS and/or html or layout questions. 1. Explain how public key encryption works. Explain both the steps and the details. 23 Each person has two keys, a public key K+ and a private key K-. To send a message, Alice can encrypt a message with Bob’s public key, which can only be decrypted using Bob’s private key. [This is not required] Similarly, Alice can encrypt a message with her private key, which can be encrypted by anyone using her public key … thus identifying Alice as the sender (digital signature). 24 1. Which of the following is correct? a. jQuery is a JSON Library b. jQuery is a JavaScript Library 2. jQuery uses CSS selectors and XPath expressions to select elements? False or True T 3. Which sign does jQuery use as a shortcut for jQuery? a. the ? Sign b. the % sign c. the $ sign 4. With jQuery, look at the following selector: $("div"). What does it select? a. The first div element b. All div elements 5. Is jQuery a library for client scripting or server scripting? a. Server scripting b. Client scripting 6. The jQuery html() method works for both HTML and XML documents – False or True T 7. What is the correct jQuery code to set the background color of all p elements to red? a. $("p").style("background-color","red"); b. $("p").layout("background-color","red"); c. $("p").manipulate("background-color","red"); d. $("p").css("background-color","red"); 8. With jQuery, look at the following selector: $("div.intro"). What does it select? a. The first div element with class="intro" b. All div elements with id="intro" c. All div elements with class="intro" d. The first div element with id="intro" 9. Which jQuery method is used to hide selected elements? a. hidden() b. display(none) c. hide() d. visible(false) 25 10. Which jQuery method is used to set one or more style properties for selected elements? a. style() b. css() c. html() 11. What is the correct jQuery code for making all div elements 100 pixels high? a. $("div").height(100) b. $("div").yPos(100) c. $("div").height="100" 12. Which statement is true? a. To use jQuery, you must buy the jQuery library at www.jquery.com b. To use jQuery, you can refer to a hosted jQuery library at Google c. To use jQuery, you do not have to do anything. Most browsers (Internet Explorer, Chrome, Firefox and Opera) have the jQuery library built in the browser 13. What scripting language is jQuery written in? a. JavaScript b. C# c. VBScript d. C++ 14. Which jQuery function is used to prevent code from running, before the document is finished loading? a. document.ready() b. body.onload() c. document.load() 26
© Copyright 2024