Tutorial 2: Working with Operators and Expressions

Tutorial 2: Working with Operators and Expressions

Tutorial 2: Working with Operators and Expressions

TRUE/FALSE

1.You can add a dynamic effect to a Web site using ontime processing.

ANS:FPTS:1REF:JVS 54

2.You can not insert values into a Web form’s fields.

ANS:FPTS:1REF:JVS 55

3.An event is an action that occurs within a Web browser or Web document.

ANS:TPTS:1REF:JVS 56

4.In the code <body onload = "myFunction()" ...>, myFunction() is executed when the page loads.

ANS:TPTS:1REF:JVS 57

5.Across the different web browsers and their versions, there is support for the same set of events.

ANS:FPTS:1REF:JVS 58

6.The time object is used in JavaScript to work with dates.

ANS:FPTS:1REF:JVS 59

7.You use the thisDate() function to get the current date and time.

ANS:FPTS:1REF:JVS 59

8.The getDate() method returns the specific month for a particular date.

ANS:FPTS:1REF:JVS 61

9.To get a two digit year, you use the getYear() method.

ANS:TPTS:1REF:JVS 61

10.JavaScript only allows the minutes and seconds to be retrieved from a date.

ANS:FPTS:1REF:JVS 61

11.JavaScript does not support daylight savings time.

ANS:TPTS:1REF:JVS 63

12.When writing your own function, you begin by using the word "method".

ANS:FPTS:1REF:JVS 63

13.Binary operators work on only one operand.

ANS:FPTS:1REF:JVS 67

14.You use the ++ operator to increment a variable in JavaScript.

ANS:TPTS:1REF:JVS 67

15.To avoid misunderstanding the actions of a decrement operator, read the action of the operator from right to left.

ANS:FPTS:1REF:JVS 68

16.If you insert a space between the two symbols of the assignment operator, JavaScript will report a syntax error.

ANS:TPTS:1REF:JVS 68

17.JavaScript measures time in terms of days.

ANS:FPTS:1REF:JVS 70

18.To use the round method, you type "math.round()".

ANS:FPTS:1REF:JVS 73

19.The Math.random() method returns a value between 0 and 1.

ANS:TPTS:1REF:JVS 74

20.The value.toFixed(n) method can be used to control the number of digits displayed by the browser.

ANS:TPTS:1REF:JVS 80

MODIFIED TRUE/FALSE

1.One way of inserting an event handler is to add it as an attribute of the element in the Web document. ______

ANS:TPTS:1REF:JVS 56

2.The onclick event handler handles when the mouse button has been pressed. ______

ANS:F, onmousedown

PTS:1REF:JVS 57

3.Another way to run a JavaScript command in response to an event is to treat it as a hypertext form. ______

ANS:F, link

PTS:1REF:JVS 58

4.To create a date object, you use new Time(). ______

ANS:F, new Date()

PTS:1REF:JVS 59

5.The time methods can be used to retrieve information from a date object. ______

ANS:F, date

PTS:1REF:JVS 60

6.The method to get the current hour from a date object is DateObject.getTimeHour(). ______

ANS:F

DateObject.getHours()

PTS:1REF:JVS 61

7.The getTime() method retrieves the time value as expressed in milliseconds. ______

ANS:TPTS:1REF:JVS 62

8.The operators shown in the figure above are binary operators. ______

ANS:F, unary

PTS:1REF:JVS 68

9.The -- operator in the figure above is used to decrement variables. ______

ANS:TPTS:1REF:JVS 68

10.To make a function available to other pages, you should place the function in an external file. ______

ANS:TPTS:1REF:JVS 64

11.The figure above shows an example of a JavaScript object. ______

ANS:F, function

PTS:1REF:JVS 69

12.The portion of JavaScript code pointed at in the figure above calculates the number of seconds in a day. ______

ANS:F, milliseconds

PTS:1REF:JVS 70

13.The & is a(n) comparison operator. ______

ANS:F, logical

PTS:1REF:JVS 84

14.To cancel a time-delayed command called timeOne, you would use timeOne.clearTimeout(). ______

ANS:F, clearTimeout(timeOne)

PTS:1REF:JVS 88

15.In the following JavaScript code:

setTimeout("addThree()",50);

setTimeout("showDate()",50);

the functions execute almost simultaneously. ______

ANS:TPTS:1REF:JVS 89

MULTIPLE CHOICE

1.In document.Contact.Name.value, the name of the form must be ____.

a. / Contact / c. / document
b. / Name / d. / value

ANS:APTS:1REF:JVS 55

2.A(n) ____ is a statement that tells browsers what code to run in response to the specified event.

a. / event script / c. / unary operator
b. / event handler / d. / element attribute

ANS:BPTS:1REF:JVS 56

3.The ____ event handler runs when the browser has completed unloading the document.

a. / onload / c. / onerror
b. / onremove / d. / onunload

ANS:DPTS:1REF:JVS 57

4.The ____ event handler tests whether or not a user has pressed and released a button on the keyboard.

a. / onquit / c. / onkeydown
b. / onclick / d. / onkeypress

ANS:DPTS:1REF:JVS 57

5.If a user clicks an item on a page, which event(s) may occur?

a. / ondblclick / c. / a only
b. / onclick / d. / either a or b

ANS:DPTS:1REF:JVS 57

6.The ____ event handler runs when a user has entered a text input field.

a. / onload / c. / onfocus
b. / onchange / d. / onblur

ANS:CPTS:1REF:JVS 57

7.To add code that runs when content of an input field has been modified, you use the ____ event handler.

a. / onload / c. / onfocus
b. / onchange / d. / onblur

ANS:BPTS:1REF:JVS 57

8.To have the function verify() execute when a person leaves the Name input field, you must use the ____ event handler.

a. / onload / c. / onfocus
b. / onchange / d. / onblur

ANS:DPTS:1REF:JVS 57

9.To give buttons an action on a form, you can use the ____ event.

a. / onblur / c. / onkeypress
b. / onclick / d. / onchange

ANS:BPTS:1REF:JVS 56

10.Which of the following is not a Window event?

a. / onresize / c. / onscroll
b. / onselect / d. / onmove

ANS:BPTS:1REF:JVS 57

11.Which of the following is not a mouse event?

a. / onmousedown / c. / onmouseclick
b. / onmouseup / d. / onmouseout

ANS:CPTS:1REF:JVS 57

12.Which of the following is not a keyboard event?

a. / onkeyout / c. / onkeypress
b. / onkeydown / d. / none of the above

ANS:APTS:1REF:JVS 57

13.Older web browsers used ____ links to run JavaScript commands.

a. / paragraph / c. / hypertext
b. / image / d. / source

ANS:CPTS:1REF:JVS 58

14.A function includes its ____, which are values used by the function.

a. / terms / c. / ids
b. / parameters / d. / names

ANS:BPTS:1REF:JVS 63

15.____ is the general syntax for using anchor tags to run JavaScript.

a. / <a alt = "javascript:script">content</a> / c. / <a href = "javascript:script">content</a>
b. / <a href = "script">content</a> / d. / <a src = "javascript:script">content</a>

ANS:CPTS:1REF:JVS 58

16.To create a date object using the current date and store it in myDate, you enter ____.

a. / myDate = new Date();
b. / myDate = new Time();
c. / myDate = new Time(2011, 1, 24, 14, 35, 5);
d. / myDate = new Date(2011, 1, 24, 14, 35, 5);

ANS:APTS:1REF:JVS 59

17.January is indicated by the number ____ in JavaScript.

a. / 0 / c. / 4
b. / 1 / d. / 10

ANS:APTS:1REF:JVS 59

18.Dates are numeric values measured in ____.

a. / milliseconds / c. / microseconds
b. / nanoseconds / d. / picoseconds

ANS:APTS:1REF:JVS 60

19.The ____ method always returns the year in four digit format.

a. / getYear(); / c. / getDate();
b. / getFullDate(); / d. / getFullYear();

ANS:DPTS:1REF:JVS 61

20.The starting date for time calculations is ____.

a. / January 1, 1970 / c. / January 1, 1900
b. / January 1, 1990 / d. / January 1, 1960

ANS:APTS:1REF:JVS 60

21.After the following code, what value does x contain?

x = new Date("October 31, 2011 11:22:05");

x = x.getMinutes();

a. / 31 / c. / 11
b. / 22 / d. / 5

ANS:BPTS:1REF:JVS 61

22.When the someDate variable stores the date object for "June 15, 2011 14:35:28" (which is a Wednesday), the ____ method would return a value of 3.

a. / someDate.getDate(); / c. / someDate.getTime();
b. / someDate.getMonth(); / d. / someDate.getDay();

ANS:DPTS:1REF:JVS 62

23.To change the someDate variable’s year to 2012, you enter ____.

a. / someDate.setYear(12); / c. / someDate.setFullYear(2012);
b. / someDate.setFullYear(12); / d. / someDate.setYear(2012);

ANS:CPTS:1REF:JVS 63

24.Which of the following includes support for daylight savings time?

a. / computer’s operating system / c. / Date methods
b. / JavaScript / d. / all of the above

ANS:APTS:1REF:JVS 63

25.Consider the following code:

function showBirthday(birthday) {

thisDay = birthday.getDate();

thisMonth = birthday.getMonth()+1;

thisYear = birthday.getFullYear();

return thisMonth + "/" + thisDay + "/" + thisYear;

}

What type of parameter must birthday be?

a. / time object / c. / check object
b. / date object / d. / none of the above

ANS:BPTS:1REF:JVS 63

26.The ____ method extracts the year value from the date variable.

a. / extractYear() / c. / Year()
b. / getFullYear() / d. / thisYear()

ANS:BPTS:1REF:JVS 63

27.To make JavaScript code available to other web pages, the code should be placed in ____.

a. / the head of the document / c. / an external file
b. / an internal script tag / d. / a CSS file

ANS:CPTS:1REF:JVS 64

28.To tell the browser to include code from the myCode.js file, you should enter ____.

a. / <script type="text/javascript" link="myCode.js">
b. / <script type="text/javascript" file="myCode.js">
c. / <script type="text/javascript" href="myCode.js">
d. / <script type="text/javascript" src="myCode.js">

ANS:DPTS:1REF:JVS 64

29.In a <script> tag linking an external JavaScript file, the MIME type should be specified as ____.

a. / file/javascript / c. / mime/javascript
b. / text/javascript / d. / src/javascript

ANS:BPTS:1REF:JVS 64

30.To perform a calculation, a statement must include a(n) ____.

a. / operator / c. / script file
b. / link / d. / none of the above

ANS:APTS:1REF:JVS 66

31.A(n) ____ operator can be used to perform simple mathematical calculations.

a. / conditional / c. / ordinal
b. / logical / d. / arithmetic

ANS:DPTS:1REF:JVS 66

32.The ____ operator increases a value by 1 (unary operator).

a. / + / c. / ++
b. / * / d. / ~

ANS:CPTS:1REF:JVS 67

33.____ operators work on two elements in an expression.

a. / Binary / c. / Unary
b. / Tertiary / d. / Increment

ANS:APTS:1REF:JVS 67

34.____ operators work on only one variable.

a. / Binary / c. / Solo
b. / Unary / d. / Increment

ANS:BPTS:1REF:JVS 67

35.The most common assignment operator is the ____.

a. / += / c. / =
b. / -= / d. / ==

ANS:CPTS:1REF:JVS 68

36.The expression to return the remainder of x divided by y would be ____.

a. / x / y / c. / y / x
b. / y % x / d. / x % y

ANS:DPTS:1REF:JVS 67

37.In JavaScript, the expression ____ creates the same result as the expression x = x + y;.

a. / y += x / c. / y = x ++ y
b. / x = x++ / d. / x += y

ANS:DPTS:1REF:JVS 68

38.What does the following generic function called foo return?

function foo()

{

var y = 3;

var x = 4;

y = --x;

x = y++ % x;

return y;

}

a. / 4 / c. / 0
b. / 3 / d. / 5

ANS:APTS:1REF:JVS 68

39.Which of the following calculates the difference in terms of days?

a. / days = (newYear - currentDate)/(1000*60*60);
b. / days = (newYear - currentDate)/(1000*60*24);
c. / days = (newYear - currentDate);
d. / days = (newYear - currentDate)/(1000*60*60*24);

ANS:DPTS:1REF:JVS 70

40.The ____ can be used for performing advanced calculations and mathematical operations.

a. / Date object / c. / Library object
b. / Math object / d. / All of the above

ANS:BPTS:1REF:JVS 73

41.To change x's value to the nearest integer, you would use the following code: ____

a. / Math.round(x); / c. / math.round(x);
b. / Math.exp(x); / d. / math.ceil(x);

ANS:APTS:1REF:JVS 73

42.When using a random number, the function you would least likely use to get an integer value would be ____.

a. / Math.round() / c. / Math.ceil()
b. / Math.floor() / d. / none of the above

ANS:APTS:1REF:JVS 74

43.The ____ constant has the approximate value of 3.1416.

a. / Math.X / c. / Math.PI
b. / Math.E / d. / All of the above

ANS:CPTS:1REF:JVS 75

44.To display x with only 2 digits, you can apply the ____ method.

a. / numDigits / c. / fixDigits
b. / toFixed / d. / toCurrency

ANS:BPTS:1REF:JVS 80

45.If x has the value 5.1356, x.toFixed(3) returns what value?

a. / 5.13 / c. / $5.13
b. / $5.135 / d. / 5.136

ANS:DPTS:1REF:JVS 80

46.The ____ method returns a text string with n significant digits to the left or right of the decimal point.

a. / toPrecision / c. / toExponential
b. / toFixed / d. / toDigits

ANS:APTS:1REF:JVS 81

47.Given the following code:

var result = 10/"A";

document.write(result);

What value will be displayed?

a. / N/A / c. / NaN
b. / NonN / d. / infinity

ANS:CPTS:1REF:JVS 79

48.Which function makes sure that a number is not greater than the largest numeric value supported by JavaScript?

a. / isNaN(value) / c. / isZero(value)
b. / isNull(value) / d. / isFinite(value)

ANS:DPTS:1REF:JVS 79

49.When displaying the value 5.78912, JavaScript shows ____ digits by default.

a. / 0 / c. / 4
b. / 2 / d. / 6

ANS:DPTS:1REF:JVS 80

50.If x has the value of 5, then to convert it to a text string, you could use ____.

a. / myString = x; / c. / myString = parseText(x);
b. / myString = x + ""; / d. / none of the above

ANS:BPTS:1REF:JVS 80

51.____ converts a text string, myString, to a number with decimal places.

a. / parseText / c. / parseDecimal
b. / parseInt / d. / parseFloat

ANS:DPTS:1REF:JVS 81

52.Which of the following returns NaN?

a. / parseInt("The number of barrels was 12");
b. / parseInt("135.6");
c. / parseInt("3 books were read");
d. / none of the above

ANS:APTS:1REF:JVS 81

53.The general format of the conditional operator is ____.

a. / (condition) : falseValue ? trueValue / c. / (condition) ? falseValue : trueValue
b. / (condition) : trueValue : falseValue / d. / (condition) ? trueValue : falseValue

ANS:DPTS:1REF:JVS 83

54.The ____ comparison operator returns true if variables are equal.

a. / != / c. / >=
b. / == / d. / <=

ANS:BPTS:1REF:JVS 84

55.The ____ comparison operator returns true if variables are not equal.

a. / ~= / c. / !=
b. / == / d. / ++

ANS:CPTS:1REF:JVS 84

56.The ____ logical operator returns true when both expressions are true.

a. / || / c. / ==
b. / | / d.

ANS:DPTS:1REF:JVS 84

57.The ____ logical operator returns true when either expression is true.

a. / || / c. / ==
b. / | / d.

ANS:APTS:1REF:JVS 84

58.The ____ logical operator returns true if the expression is false and false if the expression is true.

a. / || / c.
b. / ! / d. / ==

ANS:BPTS:1REF:JVS 84

59.Which of the following assigns y a value of 20 if x is greater than 1 and less than 3 and 15 otherwise?

a. / y = (x > 1) & (x < 3) : 20 ? 15; / c. / y = (x > 1) & (x < 3) ? 20 : 15;
b. / y = (x > 1) & (x < 3) ? 15 : 20; / d. / y = (x > 1) || (x < 3) ? 20 : 15;

ANS:CPTS:1REF:JVS 84

60.Using the figure above, if thisHour is 21, what values will be stored in ampm and thisHour after the last line of code?

a. / p.m., 9 / c. / p.m. 10
b. / a.m., 9 / d. / a.m., 8

ANS:APTS:1REF:JVS 85

61.A(n) ____ command runs after a specific amount of time.

a. / time-delayed / c. / delayed
b. / interval-run / d. / time-interval

ANS:APTS:1REF:JVS 88

62.The general format of the command to specify that a task occur once, after some time, is ____.

a. / setTime("command", interval); / c. / setTimeout("command", delay);
b. / setDelay("command", interval) / d. / setInterval("command", delay);

ANS:CPTS:1REF:JVS 88

63.To indicate that the displayList() method should run once, 15 milliseconds after the page loads, you would use ____.

a. / onload = "setTimeout("displayList()", 15);"
b. / onload = "setTimeout('displayList()', 15);"
c. / onload = "setInterval("displayList()", 15);"
d. / onload = "setInterval('displayList()', 15);"

ANS:APTS:1REF:JVS 88

64.To give an identification to a command set to run once after a delay, the general syntax is ____.

a. / timeID = setInterval("command",delay);
b. / timeID = setDelay("command",delay);
c. / timeID = setIDTimeout("command",delay);
d. / timeID = setTimeout("command",delay);

ANS:DPTS:1REF:JVS 88

65.Which of the following clears only the command called myDelay that has been set to run once after 30 seconds?

a. / clearInterval(myDelay); / c. / clearTimeout(myDelay);
b. / clearInterval(); / d. / clearTimeout();

ANS:CPTS:1REF:JVS 88

66.A(n) ____ command instructs a browser to run the same command repeatedly.

a. / timed-interval / c. / time-delayed
b. / interval-run / d. / delayed

ANS:APTS:1REF:JVS 88

67.Which of the following clears only the command called myDelay that has been set to run every 25 seconds?

a. / clearTimeout(myDelay); / c. / clearInterval(myDelay);
b. / clearInterval(); / d. / clearTimeout();

ANS:CPTS:1REF:JVS 89

68.To give an identification to a command set to run repeatedly after a certain amount of time, the general syntax is ____.

a. / timeID = setTimeout("command",interval);
b. / timeID = setDelay("command",interval);
c. / timeID = setIDTimeout("command",interval);
d. / timeID = setInterval("command",interval);

ANS:DPTS:1REF:JVS 88

69.Given the following code, which function starts running at 30 milliseconds?

setTimeout("superClock()",30);

setTimeout("updateMenu()",30);

setTimeout("performCalc()",30);

a. / superClock() / c. / performCalc()
b. / updateMenu() / d. / all of the above

ANS:DPTS:1REF:JVS 89

70.To clear all commands set to repeat, you use ____.

a. / clearTimeout() / c. / clearDelay()
b. / clearRepeat() / d. / clearInterval()

ANS:DPTS:1REF:JVS 88

Case-Based Critical Thinking Questions

Case 2-1

Legend Films is planning to create a page that will allow users to email their video requests for the studio to add to their online video offerings.

71.In order to add the date to the form, the Web designer could use the ____ object.

a. / Math / c. / Arithmetic
b. / Comparison / d. / Date

ANS:DPTS:1REF:JVS 59TOP:Critical Thinking

72.The Web designer would like to create code that could be used in each movie new release page to count down to the release. Where should he place the code to make it easy to reuse?

a. / in a script tag / c. / in an external file
b. / in the head of the document / d. / none of the above

ANS:CPTS:1REF:JVS 64TOP:Critical Thinking

73.External files containing JavaScript commonly use the extension ____.

a. / asp / c. / js
b. / script / d. / php

ANS:CPTS:1REF:JVS 65TOP:Critical Thinking

Case-Based Critical Thinking Questions

Case 2-2

Carol Smith wants to create a page that would have several dynamic effects. For instance, she wants a clock that shows how long the user has been viewing a page as well as to have the page show a welcome message and a goodbye message. She also has a list of links that should run her functions to update parts of the page.

74.Carol wants to have the clock update every 10 seconds. Which method would be best for her to use to cause her script to run according to her schedule?

a. / setInterval / c. / setTimeout
b. / setDelay / d. / none of the above

ANS:APTS:1REF:JVS 88TOP:Critical Thinking

75.Carol will most likely use which event to display her welcome message?

a. / onenter / c. / onunload
b. / onexit / d. / onload

ANS:DPTS:1REF:JVS 57TOP:Critical Thinking

76.The goodbye message will most likely involve using the ____ event.

a. / onenter / c. / onunload
b. / onexit / d. / onload

ANS:CPTS:1REF:JVS 57TOP:Critical Thinking

77.Since Carol is using a list of links to activate JavaScript commands, she will need to use a(n) ____ even though it is no longer considered good design.

a. / hypertext link / c. / onclick event
b. / external file / d. / onload event

ANS:APTS:1REF:JVS 58TOP:Critical Thinking

Case-Based Critical Thinking Questions

Case 2-3

Betty's Custom Floral Arrangements shop has an online order form. She has added some JavaScript functions to calculate how much a person has to pay before they submit the page, and now wants to add her functions to her form buttons.

78.Which event is best used for her buttons?

a. / ondblclick / c. / onclick
b. / onkeypress / d. / onmouseout

ANS:CPTS:1REF:JVS 57TOP:Critical Thinking

79.Betty wants to display the total from her calcTotal() function in her form named results in a text box called finalAmount. What will the command look like?

a. / results.finalAmount.document.value = calcTotal();
b. / document.finalAmount.results = calcTotal();
c. / document.finalAmount.results.value = calcTotal();
d. / document.results.finalAmount.value = calcTotal();

ANS:DPTS:1REF:JVS 55TOP:Critical Thinking

80.Betty gets complaints that customers are seeing NaN in certain places. What function can help her stop that from happening?

a. / isFiN / c. / isNaN
b. / isNumber / d. / isFinite

ANS:CPTS:1REF:JVS 79TOP:Critical Thinking