CIW JavaScript Specialist 5 — Questions and Answers
Question 1: What is the purpose of the JavaScript prototype chain?
- To define private class members
- To enable objects to inherit properties and methods from other objects (Correct answer)
- To enforce strict typing on object properties
- To prevent object mutation at runtime
Correct answer: To enable objects to inherit properties and methods from other objects
The prototype chain is JavaScript's inheritance mechanism, allowing objects to access properties and methods defined on their prototype objects.
Question 2: Which method of the Fetch API is used to parse a response body as JSON?
- response.text()
- response.json() (Correct answer)
- response.parse()
- JSON.parse(response)
Correct answer: response.json()
response.json() reads the Response body stream and returns a Promise that resolves with the result of parsing it as JSON.
Question 3: What does the JavaScript 'use strict' directive do?
- Enables ES6 module syntax automatically
- Enforces stricter parsing rules that catch common coding mistakes and prevent unsafe actions (Correct answer)
- Makes all variables immutable
- Disables the use of global variables
Correct answer: Enforces stricter parsing rules that catch common coding mistakes and prevent unsafe actions
'use strict' enables strict mode, which catches silent errors (like using undeclared variables) by throwing exceptions instead.
Question 4: In JavaScript, what is a callback function?
- A function that calls itself recursively
- A function passed as an argument to another function and invoked after an operation completes (Correct answer)
- A method that returns a value to the calling scope
- A function that reverses the call stack
Correct answer: A function passed as an argument to another function and invoked after an operation completes
A callback function is passed into another function as an argument and is executed at a later point, often after an asynchronous operation completes.
Question 5: Which CSS manipulation approach is preferred in modern JavaScript to add or remove a class on an element?
- element.className = 'newClass'
- element.setAttribute('class', 'newClass')
- element.classList.add() / element.classList.remove() (Correct answer)
- document.styleSheets[0].insertRule()
Correct answer: element.classList.add() / element.classList.remove()
element.classList provides add(), remove(), toggle(), and contains() methods for cleanly managing an element's class list without overwriting existing classes.
Question 6: What is the difference between null and undefined in JavaScript?
- They are identical in value and type
- null is explicitly assigned to indicate no value; undefined means a variable has been declared but not assigned (Correct answer)
- undefined is the result of deleting a property; null means the property was never created
- null is for numbers; undefined is for strings
Correct answer: null is explicitly assigned to indicate no value; undefined means a variable has been declared but not assigned
undefined is the default state of an uninitialized variable, while null is an intentional assignment representing the absence of any object value.
Question 7: Which JavaScript design pattern encapsulates related variables and functions to avoid polluting the global namespace?
- Observer pattern
- Factory pattern
- Module pattern (Correct answer)
- Singleton pattern
Correct answer: Module pattern
The Module pattern uses immediately invoked function expressions (IIFEs) or ES6 module syntax to create private scope and expose only a public API.
What is the purpose of the JavaScript prototype chain?