1.
If the prompt method displays a dialog box, and the user enters some text and clicks on the OK button, what does the prompt method return?
Correct Answer
D. A string value
Explanation
The prompt method returns a string value. When the user enters text and clicks on the OK button in the dialog box, the prompt method captures that text and returns it as a string value.
2.
For the following code, if the user clicks the Cancel button in the first dialog box that’s displayed, a second dialog box is displayed that says...
var firstName = prompt("Please enter your first name:");
if ( firstName == null ) {
alert("You must enter your first name.");
} else if (firstName == "") {
alert("First name is required.");
} else {
alert("Thank you.");
}
Correct Answer
B. You must enter your first name.
Explanation
The correct answer is "You must enter your first name." because if the user clicks the Cancel button in the first dialog box, the value of the variable firstName will be null. The first if statement checks if the value of firstName is null, and if it is, it displays the alert message "You must enter your first name." This is the correct answer because it accurately describes the action taken when the Cancel button is clicked.
3.
What property of the Radio object is used to determine if a radio button is selected?
Correct Answer
B. The checked property
Explanation
The correct answer is the checked property. This property is used to determine if a radio button is selected. It returns a boolean value, true if the radio button is selected and false if it is not.
4.
For a Textarea object, what property do you use to get the text that has been entered into the text area?
Correct Answer
A. The value property
Explanation
The value property is used to get the text that has been entered into the textarea. This property allows you to access and retrieve the content of the textarea element.
5.
To remove the focus from a control, what method do you use?
Correct Answer
B. The blur method
Explanation
The blur method is used to remove the focus from a control. When a control has focus, it means that it is currently selected or active, and the blur method allows you to shift the focus away from that control. This can be useful in situations where you want to redirect the user's attention to another part of the page or remove the focus from a specific control after a certain event or action has occurred.
6.
What event occurs when the user selects a new item from a select list?
Correct Answer
C. The change event
Explanation
When the user selects a new item from a select list, the event that occurs is the change event. This event is triggered when the value of the select list is changed, indicating that the user has made a selection. It allows for the execution of specific actions or functions in response to the user's selection.
7.
For the following code, an event handler named investment_change is
var investment_change = function () {
var years = parseInt( $("years").value );
alert("Years: " + years);
}
window.onload = function () {
$("investment").onchange = investment_change;
}
Correct Answer
D. Attached to the onchange event of a control with an id of “investment”.
Explanation
The event handler named investment_change is attached to the onchange event of a control with an id of "investment". This means that the code will be executed whenever the value of the control with the id "investment" is changed.
8.
To display data in a span element, you can use the firstChild property to get a pointer to the Text node. Then, what property of the Text node can you use to set the value that’s displayed in the span element?
Correct Answer
D. The nodeValue property
Explanation
The correct property to set the value that's displayed in the span element is the nodeValue property.
9.
Assuming you have a radio button with an id of “contact_via”, which of the following statements selects that radio button?
Correct Answer
B. $("contact_via").checked = true;
Explanation
The correct statement that selects the radio button with an id of "contact_via" is $("contact_via").checked = true;. This statement uses the checked property to set the radio button as selected.
10.
How can you clear a check from a Checkbox object?
Correct Answer
B. Set its checked property to false
Explanation
To clear a check from a Checkbox object, you need to set its checked property to false. This will uncheck the checkbox and remove the check mark from it.
11.
The _______ property of the Number object returns the largest positive value that
JavaScript can represent.
Correct Answer
MAX_VALUE
Explanation
The MAX_VALUE property of the Number object in JavaScript returns the largest positive value that can be represented by the language. It is a constant value that can be used to compare or set the maximum limit for numerical operations in JavaScript.
12.
In JavaScript, _________ represents a value that isn’t a valid number.
Correct Answer
Number.NaN
Explanation
In JavaScript, Number.NaN represents a value that isn't a valid number. It is a special value that indicates the result of an operation that cannot produce a normal numeric result. It is often used to check if a value is not a number using the isNaN() function.
13.
The ____________ method of the Math object can be used to generate and return a decimal number that’s greater than or equal to 0 but less than 1.
Correct Answer
random
Explanation
The "random" method of the Math object generates and returns a decimal number between 0 (inclusive) and 1 (exclusive). This means that the number can be greater than or equal to 0 but less than 1.
14.
The _________ property of a string returns the number of characters in the string.
Correct Answer
length
Explanation
The "length" property of a string is used to determine the number of characters present in the string. It returns the total count of characters, including spaces and punctuation marks. This property is commonly used in programming to manipulate and analyze strings based on their length.
15.
The _____________ method of a Date object returns the month in local time with a value of 0 for January, 1 for February, and so on.
Correct Answer
getMonth()
Explanation
The getMonth() method of a Date object returns the month in local time with a value of 0 for January, 1 for February, and so on. This method is used to retrieve the month component of a date object. It is useful when you need to perform operations or comparisons based on the month value.
16.
If a numerical operation returns a number greater than the largest possible JavaScript value, it returns
Correct Answer
C. Infinity
Explanation
If a numerical operation in JavaScript returns a number that is greater than the largest possible JavaScript value, it will return "Infinity". This occurs when a calculation exceeds the maximum value that can be represented in JavaScript, which is approximately 1.7976931348623157e+308. "Infinity" represents an infinite value and is used to indicate that the result of the operation is beyond the numeric limits of JavaScript.
17.
What method of the Number object returns a string with the number rounded to the specified number of decimal places?
Correct Answer
D. The toFixed method
Explanation
The toFixed method of the Number object returns a string with the number rounded to the specified number of decimal places. This method is used to format a number with a fixed number of decimal places, regardless of whether there are trailing zeros. It allows for precise control over the number of digits displayed after the decimal point.
18.
If the count variable has a value of 1, what is the value of the test variable after the following statement is executed?
var test = (count == 1) ? "errors" : "error";
Correct Answer
C. Errors
Explanation
The value of the test variable will be "errors" if the count variable has a value of 1. This is because the ternary operator is used in the statement, which checks if the condition (count == 1) is true. If it is true, the value "errors" is assigned to the test variable. If the condition is false, the value "error" is assigned to the test variable. Since the count variable has a value of 1, the condition is true and "errors" is assigned to the test variable.
19.
What method of the Math object can be used to return the largest value from the values that are passed to it?
Correct Answer
A. The Math.max method
Explanation
The Math.max method is used to return the largest value from the values that are passed to it. This method takes multiple arguments and returns the highest value among them. It is commonly used in mathematical calculations or when comparing multiple values to find the maximum value.
20.
What text does the following code display in a dialog box?
alert("The file is in \"C:\\My Documents\"");
Correct Answer
D. The file is in "C:\\My Documents\"
Explanation
The code displays the text "The file is in "C:\My Documents\" in a dialog box.
21.
The equality operators convert data from one type to another before performing a comparison, but the ______________ operators do not convert data before performing a comparison.
Correct Answer
identity
Explanation
The identity operators in programming, such as "===" in JavaScript, do not convert data types before performing a comparison. They strictly check for both value and type equality. This means that the operands must be of the same type and have the same value for the comparison to be true. In contrast, the equality operators like "==" in JavaScript, will perform type conversion if the operands are of different types before making the comparison.
22.
The ___________ operator returns a true value if the expression on its left is greater than or equal to the expression on its right.
Correct Answer
>=
greater than or equal to
Explanation
The >= operator is used to compare two expressions and returns true if the expression on its left is greater than or equal to the expression on its right. It is commonly used in conditional statements to check if a value is greater than or equal to another value.
23.
The ___________ operator returns a true value when the expression on both sides of the operator are true.
Correct Answer
&&
AND
Explanation
The "&&" operator is also known as the "AND" operator. It returns a true value only when both sides of the operator are true. In other words, if the expression on the left side and the expression on the right side of the "&&" operator are both true, then the overall result will be true. If either one or both of the expressions are false, then the overall result will be false.
24.
The ________________ operator has the highest precedence of the logical operators.
Correct Answer
!
NOT
Explanation
The "!" operator, also known as the "NOT" operator, has the highest precedence among all the logical operators. This means that when evaluating an expression, the "!" operator is applied first before any other logical operators.
25.
The ________________ loop tests its conditional expression at the end of the loop.
Correct Answer
do-while
Explanation
The do-while loop is a type of loop that tests its conditional expression at the end of the loop. This means that the code within the loop will always execute at least once, regardless of whether the condition is initially true or false. After the code has executed, the condition is checked and if it is true, the loop will continue to execute. If the condition is false, the loop will terminate and the program will move on to the next line of code.
26.
The ________________ loop is typically used when the loop requires a counting variable.
Correct Answer
for
Explanation
The "for" loop is typically used when the loop requires a counting variable. It allows for the initialization of a variable, a condition to be checked before each iteration, and an increment or decrement of the variable after each iteration. This makes it ideal for situations where a loop needs to iterate a specific number of times or when a loop needs to iterate over a sequence of elements using an index.
27.
Which of the following operators does NOT perform type coercion?
Correct Answer
C. ===
Explanation
The operator "===" does not perform type coercion. It is a strict equality operator that checks for both value and type equality. It returns true if the operands are of the same type and have the same value, and false otherwise. Unlike the "==" operator, which performs type coercion and converts the operands to a common type before comparison, the "===" operator does not perform any type coercion and requires both operands to be of the same type for a true result.
28.
Which of the following comparisons will result in a true value?
Correct Answer
D. "" == 0
Explanation
The comparison "" == 0 will result in a true value. This is because the double equals operator (==) in JavaScript performs type coercion, which means it converts the operands to a common type before comparison. In this case, an empty string "" is coerced to a numerical value of 0, and since both operands now have the same type (number), the comparison evaluates to true.
29.
What is displayed when the following code executes?
var rate = 0.1;
if ( isNaN(rate) ) {
alert("Rate is not a number.");
}
else if (rate < 0) {
alert("Rate cannot be less than zero.");
}
else if (rate > 0.2) {
alert("Rate cannot be greater than 20%.");
}
else {
alert("Rate is valid.");
}
Correct Answer
D. Rate is valid.
Explanation
The code first checks if the value of the variable "rate" is not a number using the isNaN() function. Since the value of "rate" is a number (0.1), this condition is false. Then it checks if the value of "rate" is less than 0, which is also false. Next, it checks if the value of "rate" is greater than 0.2, which is also false. Since none of the previous conditions are true, the code executes the else block and displays the message "Rate is valid."
30.
What is displayed when the following code executes?
var age = 19, score = 750;
if ( age >= 21 && score >= 700 ) {
alert ("Loan approved");
} else if ( age >= 21 && score >= 650 ) {
alert ("Cosigner needed.");
} else if ( age >= 18 && score >= 680 ) {
alert ("Two cosigners needed.");
} else {
alert ("Loan denied.");
}
Correct Answer
C. Two cosigners needed.
Explanation
The code first checks if the age is greater than or equal to 21 and the score is greater than or equal to 700. Since the age is 19 and the score is 750, this condition is not met. Then it checks if the age is greater than or equal to 21 and the score is greater than or equal to 650. Again, this condition is not met. Next, it checks if the age is greater than or equal to 18 and the score is greater than or equal to 680. This condition is also not met. Finally, since none of the conditions are met, the code executes the else statement and displays "Loan denied." Therefore, the correct answer is "Loan denied."
31.
To display data in a span element, you can use the firstChild property to get a pointer to the Text node. Then, what property of the Text node can you use to set the value that's displayed in the span element.
Correct Answer
D. The nodeValue property
Explanation
The correct answer is the nodeValue property. The nodeValue property is used to set the value that is displayed in the span element.
32.
Assuming you have a radio button with an id of "contact_via", which of the following statements selects that radio button?
Correct Answer
B. $("contact_via").checked = true;
Explanation
The correct statement that selects the radio button with the id "contact_via" is $("contact_via").checked = true;. This statement sets the "checked" property of the radio button to true, which selects the radio button.
33.
How can you clear a check from a Checkbox object?
Correct Answer
B. Set its checked property to false
Explanation
To clear a check from a Checkbox object, you need to set its checked property to false. This will uncheck the checkbox and remove the checkmark from it.
34.
What method of the String object searches the string for an occurence of the specified search string?
Correct Answer
C. The indexOf method
Explanation
The indexOf method is used to search for the first occurrence of a specified search string within a given string. It returns the index of the first occurrence of the search string, or -1 if the search string is not found.
35.
Which of the following statements is NOT a valid way to create a date object named birthday?
Correct Answer
A. Var birthday = "12/2/1978";
Explanation
The statement "var birthday = "12/2/1978";" is not a valid way to create a date object named birthday because it assigns a string value to the variable instead of a date object. The other options correctly create a date object using different methods: assigning the current date and time using "new Date()", parsing a string date using "new Date("12/2/1978")", and specifying the year, month, and day as arguments using "new Date(1978, 11, 2)".
36.
What method of the Date object gets the day of the month in local time?
Correct Answer
B. The getDate method
Explanation
The correct answer is the getDate method. This method returns the day of the month in local time. It is used to retrieve the numerical value of the day of the month from a Date object.
37.
What text does the following code display in the dialog box?
var investment = "$100";
if (isNaN(investment) || investment <= 0) {
alert("Investment is not valid");
} else {
alert("Investment: " + investment.toFixed(2));
}
Correct Answer
A. Investment is not valid.
Explanation
The code will display "Investment is not valid" in the dialog box. This is because the variable "investment" is assigned the value "$100", which is a string. The code checks if the investment is not a number (isNaN) or if it is less than or equal to 0. Since "$100" is not a number, the condition is true and the alert message "Investment is not valid" is displayed.
38.
What is the value of salesTax after the following code executes?
var salesTax = 53.937;
salesTax = parseFloat(salesTax.toFixed(2));
Correct Answer
C. 53.94
Explanation
The code first uses the toFixed() method to round the value of salesTax to 2 decimal places, resulting in 53.94. Then, the parseFloat() function is used to convert the rounded value back to a floating-point number. Therefore, the value of salesTax after the code executes is 53.94.
39.
Which of the following statements performs a case-insensitive comparison of strings named text1 and text2?
Correct Answer
D. Text1.toLowerCase() == text2.toLowerCase()
Explanation
The correct answer is "text1.toLowerCase() == text2.toLowerCase()". This statement performs a case-insensitive comparison of the strings named text1 and text2 by converting both strings to lowercase using the toLowerCase() method and then comparing them using the equality operator (==). This ensures that the comparison is not affected by differences in letter case.
40.
Given a Date object named due_date, which of the following statements sets the month to February?
Correct Answer
A. Due_date.setMonth(1);
Explanation
The correct answer is due_date.setMonth(1). In JavaScript, the setMonth() method is zero-based, which means that January is represented by 0, February by 1, and so on. Therefore, calling setMonth(1) will set the month of the due_date object to February.