At Universal Containers, every team has its own way of copying JavaScript objects. The code snippet shows an implementation from one team:
01 function Person() {
02 this.firstName = "John";
03 this.lastName = "Doe";
04 this.name = () => {
05 console.log('Hello ${this.firstName} ${this.lastName}');
06 }
07 }
08
09 const john = new Person();
10 const dan = JSON.parse(JSON.stringify(john)); // (intended deep copy)
11 dan.firstName = 'Dan';
12 dan.name();
(Original line 10 is logically intended to be JSON.parse(JSON.stringify(john)) to perform a JSON clone.)
What is the output of the code execution?
Answer : C
JSON.stringify(john) converts the john object into a JSON string.
When you JSON.parse that string back, you get a plain object:
Only data that can be represented in JSON is preserved (numbers, strings, booleans, arrays, plain objects).
Functions are not preserved and are dropped.
So dan is a plain object with properties firstName and lastName, but no name method.
Therefore, dan.name is undefined, and dan.name() throws:
TypeError: dan.name is not a function
The literal string interpolation inside console.log('Hello ${...}') is also wrong (single quotes), but the code never reaches that line.
________________________________________
A developer wrote the following code to test a sum3 function that takes in an array of numbers and returns the sum of the first three numbers in the array. The test passes:
01 let res = sum3([1, 2, 3]);
02 console.assert(res === 6);
03
04 res = sum3([1, 2, 3, 4]);
05 console.assert(res === 6);
A different developer made changes to the behavior of sum3 to instead sum all of the numbers present in the array.
Which two results occur when running the test on the updated sum3 function?
Answer : C, D
New behavior: sum3 now returns the sum of all elements.
Line 01: sum3([1, 2, 3]) 1 + 2 + 3 = 6
Assertion on line 02: res === 6 passes.
Line 04: sum3([1, 2, 3, 4]) 1 + 2 + 3 + 4 = 10
Assertion on line 05: res === 6 10 === 6 is false, so the assertion fails.
So:
Line 02 assertion passes D.
Line 05 assertion fails C.
________________________________________
Refer to the code below:
01 const objBook = {
02 title: 'JavaScript',
03 };
04 Object.preventExtensions(objBook);
05 const newObjBook = objBook;
06 newObjBook.author = 'Robert';
What are the values of objBook and newObjBook respectively?
Answer : A
________________________________________
Comprehensive and Detailed Explanation From Exact Extract JavaScript Knowledge
Object.preventExtensions(obj)
This built-in JavaScript method marks an object so that no new properties can be added to it.
Existing properties can still be read and updated, but adding new ones is disallowed.
const newObjBook = objBook;
Both variables reference the same object in memory. JavaScript objects are assigned by reference, not copied.
newObjBook.author = 'Robert';
Because the object has been marked as non-extensible, JavaScript will not allow new properties to be added.
The behavior depends on mode:
In non-strict mode: the assignment silently fails and does nothing.
In strict mode: this would throw a TypeError.
Since nothing indicates strict mode, this is non-strict behavior, making the assignment fail silently.
Therefore, the object remains:
{ title: 'JavaScript' }
Both objBook and newObjBook point to the same unchanged object.
This matches option A.
________________________________________
JavaScript knowledge references (text-only)
Object.preventExtensions() prevents adding new properties.
Assigning an object to another variable copies the reference, not the object.
Adding a property to a non-extensible object silently fails in non-strict mode.
==================================================
A developer has a fizzbuzz function that, when passed in a number, returns the following:
'fizz' if the number is divisible by 3.
'buzz' if the number is divisible by 5.
'fizzbuzz' if the number is divisible by both 3 and 5.
Empty string '' if the number is divisible by neither 3 nor 5.
Which two test cases properly test scenarios for the fizzbuzz function?
Answer : A, D
Comprehensive and Detailed Explanation From Exact Extract JavaScript knowledge:
First, recall the fizzbuzz rules:
If n divisible by 3 and not by 5 return 'fizz'.
If n divisible by 5 and not by 3 return 'buzz'.
If n divisible by both 3 and 5 return 'fizzbuzz'.
Otherwise return '' (empty string).
Evaluate each test:
Option A:
let res = fizzbuzz(true);
console.assert(res === '');
In JavaScript, when using arithmetic operations with true, it is coerced to the number 1. A typical implementation of fizzbuzz would treat the argument as a number and compute:
true % 3 1 % 3 1
true % 5 1 % 5 1
So true is effectively treated as 1, which is divisible by neither 3 nor 5. The function should return ''. The assertion res === '' will pass.
This test covers the scenario ''neither divisible by 3 nor 5''.
Option B:
let res = fizzbuzz(3);
console.assert(res === '');
3 is divisible by 3 but not by 5.
According to fizzbuzz rules, fizzbuzz(3) should return 'fizz'.
This test expects '', so it is incorrect; the assertion would fail in a correct implementation.
Option C:
let res = fizzbuzz(5);
console.assert(res === 'fizz');
5 is divisible by 5 but not by 3.
According to fizzbuzz rules, fizzbuzz(5) should return 'buzz'.
This test expects 'fizz', so it is incorrect.
Option D:
let res = fizzbuzz(15);
console.assert(res === 'fizzbuzz');
15 is divisible by both 3 and 5.
According to fizzbuzz rules, fizzbuzz(15) should return 'fizzbuzz'.
This assertion correctly matches the expected result, so it is a proper test case.
Thus, the two test cases that correctly test valid fizzbuzz behavior are:
Study Guide / Concept Reference (no links):
Modulo operator (%) for divisibility checks
Truthy/number coercion of boolean true in arithmetic (Number(true) === 1)
Designing unit tests with console.assert
Typical fizzbuzz logic structure and expected outputs
________________________________________
Original code:
01 let requestPromise = client.getRequest;
03 requestPromise().then((response) => {
04 handleResponse(response);
05 });
The developer wants to gracefully handle errors from a Promise-based GET request.
Which code modification is correct?
Answer : C
________________________________________
Comprehensive and Detailed Explanation From Exact Extract JavaScript Knowledge
Key JavaScript Promise rule:
try...catch does not catch asynchronous errors that occur in Promise chains.
Therefore, the correct way to handle errors in a Promise is:
promise.then(...).catch(...);
Analysis of options:
A and B:
Both wrap asynchronous code in try...catch, which does not catch Promise rejections.
try...catch only catches errors thrown synchronously inside the block.
C:
Correct. A .catch() on the Promise chain handles any error from the Promise returned by requestPromise().
D:
.finally() executes regardless of success or failure, but it does not receive the error object, so it cannot handle the error.
Thus the only valid solution is option C.
________________________________________
JavaScript Knowledge Reference (text-only)
Promise rejections must be handled with .catch().
try...catch only handles synchronous code.
.finally() does not receive a rejection reason.
==================================================
What are two unique features of fat-arrow functions compared to normal function definitions?
Answer : A, B
________________________________________
Comprehensive and Detailed Explanation From Exact Extract JavaScript Knowledge
Arrow functions have two defining characteristics:
Implicit return when written as a single expression:
const fn = () => 5; // returns 5 automatically
This is unique to arrow functions A is correct.
Lexical this binding:
Arrow functions do not create their own this.
Instead, they use the this value from the surrounding scope B is correct.
Incorrect options:
C is false: There is no special argument called parentThis.
D is the opposite of arrow function behavior:
Normal functions create their own this; arrow functions do not.
________________________________________
JavaScript Knowledge Reference (text-only)
Arrow functions have lexical this binding.
Arrow functions support implicit return for single expressions.
==================================================
Refer to the code below:
01 const server = require('server');
02
03 // Insert code here
A developer imports a library that creates a web server. The imported library uses events and callbacks to start the server.
Which code should be inserted at line 03 to set up an event and start the web server?
Answer : C
Comprehensive and Detailed Explanation From Exact Extract JavaScript Knowledge:
The question specifies:
The library ''uses events and callbacks to start the server''.
We want to ''set up an event and start the web server''.
Option C:
server((port) => { console.log('Listening on', port); });
This:
Calls server(...), which (by design in such libraries) typically starts the web server.
Passes a callback function that receives port when the server is ready.
Inside the callback, it logs 'Listening on
This matches the ''events and callbacks'' description: the callback is invoked when the server has started listening.
Why others are incorrect:
A . server.on('connect', ...)
Assumes server is an event emitter instance with a 'connect' event, which is not given. Also, this line alone would not start the server.
B . server.start();
Assumes a start method exists; the question says the library ''uses events and callbacks to start,'' implying invocation with a callback, not a plain start() call.
D . server();
Might start the server, but does not ''set up an event'' or callback to know when or where it is listening.
Therefore, C aligns with the described usage.
Concepts: callback-based APIs, starting servers with callbacks, event/callback style vs simple method calls.