1.
How do you create a function named coolFunction?
Correct Answer
D. Function coolFunction () {}
Explanation
The correct answer is "function coolFunction () {}". This is the correct way to create a function named coolFunction in JavaScript. The function keyword is used to declare a function, followed by the function name (coolFunction in this case), parentheses for any parameters, and curly braces to enclose the function body.
2.
To display text ‘’message’ along with the value contained in variable msg, an alert box should be called via:
Correct Answer
B. Alert ('message' + msg);
Explanation
To display the text 'message' along with the value contained in the variable msg, the correct way is to call the alert box using the syntax alert ('message' + msg). This will concatenate the string 'message' with the value of the variable msg and display it in an alert box. The other options, such as call alert, alert, and alertBox, are incorrect syntax and will not display the desired result.
3.
In a JS statement, what is the difference between a++ vs ++a where a is a variable:
Correct Answer
C. Both increments the value of a by 1 but a++ does it after assignment and ++a does it before assignment
Explanation
The difference between a++ and ++a is that a++ increments the value of a by 1 after the assignment, while ++a increments the value of a by 1 before the assignment.
4.
Which of these is not a comparison operator?
Correct Answer
C. =
Explanation
The equal sign (=) is not a comparison operator, but an assignment operator. It is used to assign a value to a variable, rather than comparing two values. Comparison operators are used to compare values and include operators such as greater than (>), less than (=), less than or equal to (
5.
Which command skips the rest of a case statement?
Correct Answer
D. Break
Explanation
The correct answer is "break". In a case statement, the break command is used to exit the switch statement and skip the remaining cases. It is often used to prevent fall-through, where execution continues to the next case even after a match is found. By using break, the program will jump to the end of the switch statement, allowing it to continue executing the code after the switch block.
6.
How can you reference part of a string starting from 4th character and containing a total of 5 characters contained in var str? (e.g. from a string “hello world!”, it should return “lo wo”).
Correct Answer
C. Str.substr(3,5);
Explanation
The correct answer is `str.substr(3,5)` because the `substr()` method in JavaScript is used to extract a portion of a string, starting from the specified index and containing the specified number of characters. In this case, `str.substr(3,5)` will start from the 4th character (index 3) and return a string with 5 characters.
7.
Which event do you use to run something after the page has finished loading?
Correct Answer
B. Onload
Explanation
The correct answer is "onload". The "onload" event is used to run something after the page has finished loading. This event is commonly used to trigger functions or scripts that require the page's content to be fully loaded before executing. It ensures that all elements, including images and external resources, have been loaded and are ready to be manipulated or interacted with.
8.
How do you define a "for" loop which starts from 0, goes till 5 and increments by 1 after each iteration?
Correct Answer
D. For (i = 0; i
Explanation
The correct answer is "for (i = 0; i < 6; i++)". This is the correct syntax for defining a "for" loop that starts from 0, goes till 5, and increments by 1 after each iteration. The loop will continue as long as the value of "i" is less than 6, and after each iteration, the value of "i" will be incremented by 1.
9.
What is the correct way to write a JavaScript array?
Correct Answer
B. Var txt = new Array("tim","kim","jim");
Explanation
The correct way to write a JavaScript array is by using the syntax var txt = new Array("tim","kim","jim"). This creates an array named "txt" with three elements: "tim", "kim", and "jim".
10.
What will the statement mentioned below will do?
result = (testCond == 1) ? “pass” : “fail”;
Correct Answer
C. If testCond is equal to 1 then result will contain string pass otherwise it will be set to fail.
Explanation
The statement mentioned will assign the value "pass" to the variable "result" if the condition "testCond == 1" is true. Otherwise, it will assign the value "fail" to the variable "result".