Free Web Programming Question and Answers — Questions and Answers
Question 1: What is returned by the prompt method if a dialog box is displayed, text is entered, and the OK button is clicked?
- A string value (Correct answer)
- A Boolean value
- A null value
- None of the above
Correct answer: A string value
The `prompt()` method in JavaScript displays a dialog box that asks the user for input. If the user enters text and clicks 'OK,' the method returns the entered text as a string value. If the user clicks 'Cancel' or enters nothing, it returns `null` or an empty string, respectively.
Question 2: What occurs when a user chooses a new option from a select list?
- The change event (Correct answer)
- The click event
- The select event
- The dblclick event
Correct answer: The change event
In web programming, specifically with HTML `<select>` elements, the `change` event is fired when the value of the element is committed by the user. This occurs when a user selects a new option from the dropdown list, indicating a change in the selected value.
Question 3: You can obtain a pointer to the Text node by using the firstChild attribute to show data in a span element. What Text node property can you use to set the value shown in the span element, then?
- The value property
- The nodeValue property (Correct answer)
- The message property
- The text property
Correct answer: The nodeValue property
In the Document Object Model (DOM), a Text node represents the textual content within an HTML element. To access or modify the actual text content of a Text node, you use its `nodeValue` property. The `value` property is typically used for form elements like input fields.
Question 4: Which of the following commands, given a Date object named due date, sets the month to February?
- Due_date.setMonth("Feb");
- Due_date.setMonth(1); (Correct answer)
- Due_date.setMonth("February");
- Due_date.setMonth(2);
Correct answer: Due_date.setMonth(1);
In JavaScript, the `setMonth()` method of a Date object sets the month using a zero-based index. Therefore, January is 0, February is 1, March is 2, and so on. To set the month to February, you would pass the index `1` to the `setMonth()` method.
Question 5: What will the test variable be after the following line has been executed if the count variable has a value of 1? Test variable = (count == 1)? "errors": "error";
- 1
- Error
- Count
- Errors (Correct answer)
Correct answer: Errors
This is a ternary operator, which is a shorthand for an if-else statement. The condition `(count == 1)` is evaluated first. Since `count` has a value of `1`, the condition `1 == 1` is true, so the value before the colon, which is "errors", is assigned to the `test` variable.
Question 6: What Math object function can be utilized to get the biggest value returned from the data supplied to it?
- The Math.abs method
- The Math.max method (Correct answer)
- The Math.ceil method
- The Math.pow method
Correct answer: The Math.max method
The `Math.max()` method in JavaScript is a static method of the Math object that returns the largest of zero or more numbers. You can pass multiple numerical arguments to it, and it will identify and return the highest value among them, making it ideal for finding the maximum value.
Question 7: Which of the subsequent operators DOES NOT do type coercion?
- >==
- !=
- >
- === (Correct answer)
Correct answer: ===
The `===` operator in JavaScript is the strict equality operator. Unlike the `==` operator, it checks for both value and type equality without performing any type coercion. This means that `1 === '1'` would evaluate to `false` because their types are different, even though their values are numerically equivalent.
What is returned by the prompt method if a dialog box is displayed, text is entered, and the OK button is clicked?