Functions, Methods and Events in JavaScript (Quiz)
Consider the following JavaScript code: function helloWorld(){ myVar = "Hello World"; } helloWorld(); alert(myVar); What will be the result of this code?
A dialog box that displays: Hello World
Consider the following JavaScript code: var subTotal = 1; var tax = 0.03; alert("The total is " + subTotal * (1 + tax)); What is the result of this code?
A dialog box that displays: The total is 1.03
Consider the following JavaScript code: function helloWorld(){ var myVar = "Hello World"; } alert(myVar); What will be the result of this code?
A dialog box that displays: undefined
Which of the following best describes a function?
A named, organized block of code
In JavaScript, what is an argument?
A value passed into a function from outside the function
In JavaScript, what is casting?
A way of changing a variable from one data type to another
Which of the following is true about functions in JavaScript?
Calling a function from within itself will result in an error.
Which of the following is true about calling statements in JavaScript?
Calling statements require parentheses, even if the function does not require arguments to be passed.
Consider the following code: <script type="text/JavaScript"> <!-- var age; function showAge() { getAge(); alert(age); } function getAge() { age = prompt("Please enter your age:","18"); } // --> </script> <body onload="showAge();"> In what order will the functions excecute?
First getAge, then showAge
In JavaScript, what type of methods are also called functions?
Methods that return a value
Where is the best place in an XHTML document to place a function?
The head of the document
Consider the following JavaScript expression: parseInt("1.5y"); What is the result of this expression?
The result is: 1
Consider the following code: function counter(x) { x++ } var myVar = 0; counter(myVar); What is the value of myVar after execution?
The value is: 0
Consider the following JavaScript code: function concatenate(txt1, txt2) { txt1 += " "; var myText; return txt1 + txt2; } var myText = concatenate("Hello","World"); What is the value of myText after execution?
The value is: Hello World
Why should you use indentation in your JavaScript code?
To make the code easier to read
In JavaScript, what is the term for a built-in method that does not belong to any particular object, such as the isNaN() method?
Top-level function
How does a global variable differ from local variable?
You can access a global variable value from any function or <script> block you define on the X/HTML page.
Which of the following is an example of a user-defined JavaScript function?
addSpace()
Which event occurs when input focus is removed from a form element (e.g., when a user clicks the mouse button outside of a particular field)?
blur
Which code snippet demonstrates the correct syntax for a user-defined JavaScript function?
function my_function() { alert("test function"); }
Which event occurs when a Web page is launched in the browser?
load
Which event handler is used by the submit object?
onclick
Which of the following event handlers is used by the link object?
onmouseout
Which of the following is an event handler processed by the form object?
onsubmit
Consider the following code (line numbers added for reference): 01: 1 function makeBold(text) { 02: var boldText = text.bold() 03: //insert code here 04: } What code statement must be inserted in Line 3 to return the text in bold to the calling statement?
return boldText;
Consider the following JavaScript function: function myFunction(x) { //your code here } Which code statement should be added to the function to return to the calling statement the value of x multiplied by 2?
return x * 2
Which JavaScript event occurs when the user highlights the text in a form field?
select
Which of the following JavaScript objects uses the select event?
text
Which of the following JavaScript objects uses the onerror event handler?
window