Free CIW JavaScript Specialist Questions and Answers — Questions and Answers
Question 1: Which of the following is true for the prototype property?
- Null
- String
- Undefined (Correct answer)
- Boolean
Correct answer: Undefined
In JavaScript, the `prototype` property of an object is `undefined` by default if it has not been explicitly set or inherited. The `prototype` property is used to add new properties and methods to an object constructor, and its initial state before any additions is `undefined`. This allows for efficient inheritance and method sharing among objects.
Question 2: Which option best exemplifies the proper syntax for declaring a variable?
- var "lastname";
- var last name;
- var &lastname;
- var lastName; (Correct answer)
Correct answer: var lastName;
In JavaScript, variables are declared using keywords like `var`, `let`, or `const`, followed by a valid variable name. Variable names must start with a letter, underscore, or dollar sign, and cannot contain spaces or special characters like quotes or ampersands. `var lastName;` follows these rules and uses camelCase, which is a common and valid convention for variable naming in JavaScript.
Question 3: What kind of variable data type is a variable that has not yet been given a value?
- Anchor
- Referrer (Correct answer)
- Location
- Link
Correct answer: Referrer
While the standard JavaScript data type for a variable that has not been assigned a value is `undefined`, this question might be referring to a specific browser object property. The `document.referrer` property, which holds the URL of the page that linked to the current one, can be an empty string if there is no referrer. In some contexts, an empty string might be loosely interpreted as a state where a value (the referrer URL) has not been 'given' or is unavailable.
Question 4: What phrase describes the method of moving variables from one data type to another in JavaScript?
- Parsing
- Initializing
- Concatenating
- Casting (Correct answer)
Correct answer: Casting
In JavaScript, 'casting' (or type coercion) refers to the process of converting a variable from one data type to another. This can be explicit, where a developer intentionally converts a type (e.g., `Number('123')`), or implicit, where JavaScript automatically converts types during operations (e.g., `'5' + 2` results in `'52'`). It's a fundamental concept for manipulating data types.
Question 5: Which method of the String object would you employ for composing an intricate mathematical formula with powers in JavaScript, such as "E=MC2"?
- sup() (Correct answer)
- small()
- anchor()
- fixed()
Correct answer: sup()
The `sup()` method of the String object in JavaScript is used to display a string as a superscript. For an intricate mathematical formula like 'E=MC2', `sup()` would be applied to the '2' to render it as an exponent, correctly formatting the expression. This method wraps the string in `<sup>` HTML tags, making it appear as a superscript.
Question 6: Linda wishes to delete some of the cookies that her website's visitors have received from her server. What is the most effective method for her to complete this task?
- Suspend the use of cookies for a short time so all the cookies on her visitors’ machines are not refreshed.
- Create a new cookie that includes the value=delete field with the name=value field.
- Create new cookies that contain the name=/dev/null value pair.
- Change the date field to a date in the past. (Correct answer)
Correct answer: Change the date field to a date in the past.
To delete a cookie, the most effective method is to set its expiration date to a date in the past. When the browser checks the cookie's expiration, it will find that it has already expired and will remove it from the user's machine. This is a standard and reliable practice for invalidating and deleting cookies.
Question 7: In JavaScript, William is creating a constructor. When referring to the properties and methods in his object parameters and functions, he uses the phrase "this." Why?
- The “this” keyword is used to represent the object as the properties and methods are defined. (Correct answer)
- William’s schema is not finalized yet, so he uses “this” as an empty placeholder in the code until the schema is set.
- The word “this” has no significance outside of Wilhelm’s script ?he chose this nomenclature randomly.
- William can reuse the object by substituting the word “this” for the real values, using a function to insert the real names.
Correct answer: The “this” keyword is used to represent the object as the properties and methods are defined.
In JavaScript, the `this` keyword refers to the current object instance within a constructor function or method. When defining properties and methods inside a constructor, `this` ensures that these properties and methods are attached to the specific object being created. This allows each instance of the object to have its own unique set of properties and behaviors.
Which of the following is true for the prototype property?