Internet Programming Final Exam Study Guide

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

If totalMonths has a string value of "13", what does the code that follows display? var years = parseInt ( totalMonths / 12 ); var months = totalMonths % 12; if ( years == 0 ) { alert ( months + " months."); } else if ( months == 0 ) { alert ( years + " years"); } else { alert ( years + " years, and " + months + " months."); }

1 years, and 1 months

Which of the following statements about radio buttons is NOT true?

A user can select more than one radio button in a group

What does the following code do? var userName = $("user").firstChild.nodeValue;

It puts the text of an HTML element with "user"" as its id attribute into the userName variable.

What value is returned by the following expression? !isNaN("12.345")

true

After the code that follows is executed, what is the value of discountAmount? var discountAmount; var orderTotal = 200; if (orderTotal > 200) { discountAmount = orderTotal * .3; } else if (orderTotal > 100) { discountAmount = orderTotal * .2; } else { discountAmount = orderTotal * .1; }

40.0

The order of precedence for arithmetic expressions causes

multiplication operations to be performed before addition operations

What will be displayed in the user's browser after the following code executes if the user enters 87 at the prompt? "use strict" var getInfo = function() { grade = parseInt(prompt("What's your score on the test?")); var newGrade = getResult(grade); alert("Your grade, curved, is " + newGrade); }; var getResult = function(grade) { var newGrade = grade + 5; return (newGrade); };

nothing will display; grade is an undeclared variable

An identifier in JavaScript cannot contain ___________________ words.

reserved

To attach an event handler to an event (such as a user clicking a button), you

specify the object and the event that triggers the event handler

When you build an application with top-down coding and testing, you

start with a small portion of code and add one or two operations at a time

If you use a short-circuit operator to combine two expressions

the second expression is evaluated only if it can affect the result

If you do not want the user to be allowed to enter data into a textbox,

use the disabled property to gray out the textbox

What property would you use to get the text that has been entered into a text area?

value

Code Example 5-1 1. var getResult = function() { 2. var num1 = 54.95; 3. var num2 = .1; 4. var result = parseFloat(prompt("What is " + num1 + " * " + num2 + "?")); 5. if (result != (num1 * num2)) { 6. alert("Incorrect"); 7. } 8. else { 9. alert("Correct!"); 10. } 11. };

(Refer to Code Example 5-1) Why would "Incorrect" be displayed if the user entered 5.495 when prompted by this code?

Code Example 4-3 var tax = .07; var getCost = function(itemCost, numItems) { var subtotal = itemCost * numItems; var tax = 0.06; var total = subtotal + subtotal * tax; return (total); } var totalCost = getCost(25.00, 3); alert("Your cost is $" + totalCost.toFixed(2) + " including a tax of " + tax.toFixed(2)); (Refer to Code Example 4-3) What is the value of the global variable, tax?

.07

How many times will the while loop that follows be executed? var months = 5; var i = 1; while (i < months) { futureValue = futureValue * (1 + monthlyInterestRate); i = i+1; }

4

What does the following line of code do? $("user").innerHTML = "Hello, good friend!";

It sets the content of the element with "user" as its id attribute to "Hello, good friend!"

After the code that follows executes, the alert dialog box will display var userCost = 29.94999; alert("Your cost is $ " + parseFloat(userCost.toFixed(2)));

Your cost is $29.95

The condition for a while loop is tested

a do-while loop

The easiest type of error to fix is

a syntax error because web browsers and IDEs provide helpful error messages

To display string or numeric data in a dialog box, you use the JavaScript __________________ method.

alert

For the following code, an event handler named investmentChange is var investmentChange = function() { var years = parseInt( $("years").value ); alert("Years: " + years); }; window.onload = function() { $("investment").onchange = investmentChange; };

attached to the onchange event of a control with an id of "investment"

The easiest way to create a timer that will repeat its function at intervals but will begin immediately is to

call the function before you create the timer

Which of the following statements is false? A JavaScript identifier

can start with a number

What event occurs when the user selects a new item from a select list?

change

Which of the following statements cancels a timer that runs a function named newTimer?

clearTimeout(newTimer);

In the code that follows, prompt is a/an ____________________________. window.prompt("Enter your name");

method

When the statement that follows is executed, JavaScript var rate = parseFloat(document.getElementById("rate").value);

executes the getElementById() method, executes the value method of the resulting object, and executes the parseFloat() method on that value

When you debug an application, you

fix the bugs that have been identified

The type of loop that provides for varying the value of an index (or counter) is a

for loop

After the code that follows is executed, the variable named userEntry will contain var userEntry = 461.95; userEntry = parseInt(userEntry);

461

Which of the following statements about function declarations and function expressions is NOT true? Question options: A) Function declarations can be coded after the statements that call them. B) The JavaScript interpreter hoists all function declarations to the top of the scope that calls them. C) Function expressions must be coded before any statements that call them. D) Function declarations cannot return a value.

D) Function declarations cannot return a value.

Code Example 7-3 The JavaScript:: 1. var $ = function(id) { 2. return document.getElementById(id); 3. }; 4. var timer; 5. var counter = 10; 6. var updateCounter = function() { 7. counter--; 8. $("counter").firstChild.nodeValue = counter; 9. if (counter <= 0) { 10. clearInterval(timer); 11. document.getElementById("counter").innerHTML = "Blastoff!"; 12. } 13. }; 14. window.onload = function() { 15. timer = setInterval ( updateCounter, 1000 ); 16. }; The HTML: <h3>Countdown: <span id="counter"> 10 </span> </h2> (Refer to Code Example 7-3) Which of the following statements about this code is true?

It creates a timer that counts down from 10 to 1 and displays "Blastoff!" in the span element when the counter variable reaches 0.

Given HTML that includes the element <p id = "ship">shipping cost goes here</p> what will the following code do if the user enters 100 at the prompt? var getInfo = function() { var cost = parseFloat(prompt("How much does the item cost?")); var ship = cost * .06; document.getElementById("ship").innerHTML="Shipping: $" + ship.toFixed(2); };

It will replace the text in the <p> element with "Shipping: $6.00".

Which of the following statements about the DOM HTML specification is NOT true?

Its properties provide new functionality.

Question options: A) You can display the developer's tools by pressing F12. B) The Sources panel will display a list of all runtime errors in the order they appear in your code. C) You can set breakpoints to examine the contents of variables at selected points in your code. D) The Watch section of the right pane allows you to enter a variable name or an expression that you want to watch.

You can display the developer's tools by pressing F12.

After the statements that follow are executed, var firstName = "Ray", lastName = "Harris"; var fullName = lastName; fullName += ", "; fullName += firstName;

fullName="Harris, Ray"

Which method of the Element interface returns true if the element has the specified attribute?

hasAttribute()

Which statements are equivalent to the if statements that follow? if (pay >= 500) { tax_rate = 0.3; } if (pay >= 300 && pay < 500) { tax_rate = 0.2; } if ((pay >= 100) && (pay < 300)) { tax_rate = 0.1; }

if (pay >= 500) { tax_rate = 0.3; } else if (pay >= 300) { tax_rate = 0.2; } else if (pay >= 100) { tax_rate = 0.1; }

When creating a timer, the delay or interval is specified

in milliseconds

In the code that follows, document is a/an document.getElementById("emailAddress");

object

The childNodes property of the Node interface

returns an array of Node objects that represent the child nodes of a node

Which of the following types of errors will cause an application to stop executing?

runtime error

Which of the following statements runs a function named newTimer once after 3 seconds?

setTimeout(newTimer, 3000);

When you test an application with Chrome's developer tools and a breakpoint is reached, you can click

the Step Into button to execute the current statement and move to the next statement

Which of the following will create an event handler that is attached to the mouseout event for a textbox with id = "username" so that the user's entry, formatted in all lowercase, will be displayed in an alert? Assume the following $ function has already been coded in the page: var $ = function(id) { return document.getElementById(id); };

var getUserName = function() { var name = $("username").value; alert("Your username is " + name.toLowerCase()); }; window.onload = function() { $("username").onmouseout = getUserName; };

What will futureValue contain after the for loop has been executed one time? var years = 10; var annualRate = 10; var futureValue = 1000; annualRate = annualRate / 100; for ( i = 1; i <= years; i++ ) { futureValue += futureValue * annualRate; }

1100

Assuming that the href attributes of each <a> element contains the URL for an image, what does the code that follows do? var flowers = document.getElementsByTagName("a"); var i, flower, image; for ( i = 0; i < flowers.length; i++ ) { flower = flowers[i]; image = new Image(); image.src = flower.href; }

All of the above.

What is displayed in the alert dialog box after the following code is executed? var items = 3; for (var i = 1; i <= items; i++) { var result = 1; for (var j = i; j >= 1; j--) { result *= j; } alert("The factorial of " + i + " = " + result); }

The factorial of 1 = 1

The following code will move the focus to window.onload = function() { $("join_up").onclick = signUp; $("user_first_name").focus(); };

a textbox with id = "user_first_name"

Code Example 4-2 var add = function( x, y ) { return ( x + y ); } alert( add (5, 3) ); (Refer to Code Example 4-2) The function in this example

accepts 2 parameters and returns 1 value

Code Example 5-2 var getAvg = function () { var count = 1; var itemTotal = 0; var item = 0; while (count < 4) { item = parseInt(prompt("Enter item cost: ")); itemTotal += item; count++; } var averageCost = parseInt(itemTotal/count); alert("Total cost: $" + itemTotal + ", Average cost: $" + averageCost); }; (Refer to Code Example 5-2) How would you fix the error in the calculation of the average cost in this code?

by changing the value of averageCost to parseInt(itemTotal/(count - 1))

How would you rewrite the statement that follows that uses the DOM Core specification to use the DOM HTML specification? imageElement.getAttribute(src);

imageElement.src;

When the code that follows is executed, a message is displayed if the value the user enters var userEntry = (prompt("Enter cost:"); if (isNaN(userEntry) || userEntry > 500 ) { alert ("Message"); }

isn't a number or the value in userEntry is more than 500

Because window is the global object of JavaScript,

it is not necessary to code the object name before the method name

Code Example 7-2 JavaScript code for an Image Swap 1. var $ = function(id) { 2. return document.getElementById(id); 3. }; 4. window.onload = function() { 5. var listNode = $("image_list"); 6. var captionNode = $("caption"); 7. var imageNode = $("main_image"); 8. var imageLinks = listNode.getElementsByTagName("a"); 9. var i, image, linkNode, link; 10. for ( i = 0; i < imageLinks.length; i++ ) { 11. linkNode = imageLinks[i]; 12. image = new Image(); 13. image.src = linkNode.getAttribute("href"); 14. linkNode.onclick = function(evt) { 15. link = this; 16. imageNode.src = link.getAttribute("href"); 17. captionNode.firstChild.nodeValue = link.getAttribute("title"); 18. if (!evt) { evt = window.event; } 19. if (evt.preventDefault) { evt.preventDefault(); } 20. else { evt.returnFalse = false; } 21. }; 22. } 23. imageLinks[0].focus(); 24. }; (Refer to Code Example 7-2) Which lines of code attach an event handler for the click event of each link?

line 14 - 21

A coding error that produces the wrong results when the application is run is known as a

logic error

In the code that follows, getElementById is a/an document.getElementById("emailAddress");

method

Which of the following is a valid statement for declaring and initializing a variable named length to a starting value of 120?

var length = 120;

In the following statement, which expression is evaluated first? var truth = !A || B && (C || D)

!A

Assuming you have a radio button with an id of "contact_via", which of the following statements selects that radio button?

$("contact_via").checked = true;

Code Example 7-2 JavaScript code for an Image Swap 1. var $ = function(id) { 2. return document.getElementById(id); 3. }; 4. window.onload = function() { 5. var listNode = $("image_list"); 6. var captionNode = $("caption"); 7. var imageNode = $("main_image"); 8. var imageLinks = listNode.getElementsByTagName("a"); 9. var i, image, linkNode, link; 10. for ( i = 0; i < imageLinks.length; i++ ) { 11. linkNode = imageLinks[i]; 12. image = new Image(); 13. image.src = linkNode.getAttribute("href"); 14. linkNode.onclick = function(evt) { 15. link = this; 16. imageNode.src = link.getAttribute("href"); 17. captionNode.firstChild.nodeValue = link.getAttribute("title"); 18. if (!evt) { evt = window.event; } 19. if (evt.preventDefault) { evt.preventDefault(); } 20. else { evt.returnFalse = false; } 21. }; 22. } 23. imageLinks[0].focus(); 24. };

(Refer to Code Example 7-2) Which lines of code attach an event handler for the click event of each link?

What is the value of the totalsString variable after the following code is executed? var totals = [141.95, 212.95, 411, 10.95]; totals[2] = 312.95; var totalsString = ""; for (var i = 0; i < totals.length; i++) { totalsString += totals[i] + "|"; }

141.95|212.95|312.95|10.95|

Which of the following statements is/are true about validating HTML code for a web page?

A) Sometimes an HTML error will cause a JavaScript error; validating the HTML may help you pinpoint the problem. B) To validate HTML code, go to the W3C Markup Validation Service. C) The W3C Markup Validation Service can validate a page from a web server or by file upload. D) All of the above are true.

What will happen first as soon as the page that contains the following code loads? window.onload = function() { alert("Welcome to the Calculate Cost page!"); $("ship").onclick = shipCost(); };

An alert dialog box will display with the message "Welcome to the Calculate Cost page!".

Code Example 7-2 JavaScript code for an Image Swap 1. var $ = function(id) { 2. return document.getElementById(id); 3. }; 4. window.onload = function() { 5. var listNode = $("image_list"); 6. var captionNode = $("caption"); 7. var imageNode = $("main_image"); 8. var imageLinks = listNode.getElementsByTagName("a"); 9. var i, image, linkNode, link; 10. for ( i = 0; i < imageLinks.length; i++ ) { 11. linkNode = imageLinks[i]; 12. image = new Image(); 13. image.src = linkNode.getAttribute("href"); 14. linkNode.onclick = function(evt) { 15. link = this; 16. imageNode.src = link.getAttribute("href"); 17. captionNode.firstChild.nodeValue = link.getAttribute("title"); 18. if (!evt) { evt = window.event; } 19. if (evt.preventDefault) { evt.preventDefault(); } 20. else { evt.returnFalse = false; } 21. }; 22. } 23. imageLinks[0].focus(); 24. }; (Refer to Code Example 7-2) What would happen if lines 18 through 20 were omitted?

Clicking on a link would open a new browser window or tab and display the image specified by the href attribute of the link.

What does the following code do? $("email").firstChild.nodeValue = "Entry is invalid.";

Sets the text for the element with "email" as its id attribute.

When you view the source code for a page that's displayed in a browser, you see

The initial HTML for the page, along with the JavaScript if it's stored in the HTML file.

var name = prompt("Enter the name to print on your tee-shirt"); while (name.length > 12) { name = prompt("Too long. Enter a name with fewer than 12 characters."); } alert("The name to be printed is " + name.toUpperCase());

The user will be prompted to enter a different name

Code example 7-1 1. var eventHandler = function(evt) { 2. if (!evt) { evt = window.event; } 3. if (evt.preventDefault) { 4. evt.preventDefault(); 5. } 6. else { 7. evt.returnValue = false; 8. } 9. };

They cancel the default action of the event for all modern browsers.

What method would you use to remove the focus from a control?

blur()

If you want to store a true or false value in a variable, you use the __________________ data type.

boolean

Code Example 5-2 var getAvg = function () { var count = 1; var itemTotal = 0; var item = 0; while (count < 4) { item = parseInt(prompt("Enter item cost: ")); itemTotal += item; count++; } var averageCost = parseInt(itemTotal/count); alert("Total cost: $" + itemTotal + ", Average cost: $" + averageCost); }; (Refer to Code Example 5-2) Suppose you tested the getAvg function by entering the values 5, 10, and 15 when prompted and the alert() method displayed "Total cost: $30, Average cost: $7". What type of error is this?

logic error

When you test an application with Chrome's developer tools and a breakpoint is reached, you can view the current data by

moving the mouse pointer over the name of a variable in the Sources panel

The equal to (==) and not equal to (!=) operators should

not be used to compare floating-point numbers


Kaugnay na mga set ng pag-aaral

Small Business Strategies - Chapter 7

View Set

Linear Pairs and Vertical Angles

View Set

Mike Meyer's Guide to Network+ Chapter 2

View Set