A developer wrote the following code:
01 let x = object.value;
02
03 try {
04 handleObjectValue(x);
05 } catch(error) {
06 handleError(error);
07 }
The developer has a getNextValue function to execute after handleObjectValue(), but does not want to execute getNextValue() if an error occurs. How can the developer change the code to ensure this behavior?
Answer : B
Requirement:
getNextValue() should run only if handleObjectValue(x) does not throw an error.
If an error occurs, handleError(error) should run, and getNextValue() should be skipped.
Option B:
try {
handleObjectValue(x);
getNextValue();
} catch (error) {
handleError(error);
}
Behavior:
If handleObjectValue(x) succeeds (no error):
Execution continues to getNextValue();.
If handleObjectValue(x) throws:
Control jumps directly to catch, getNextValue() is skipped.
handleError(error); is called.
This matches the requirement perfectly.
Why others are incorrect:
A: } then { is invalid JavaScript syntax.
C:
try {
handleObjectValue(x);
} catch (error) {
handleError(error);
}
getNextValue();
getNextValue() is called after the try...catch regardless of whether an error occurred. Not acceptable.
D: finally always runs:
try {
handleObjectValue(x);
} catch (error) {
handleError(error);
} finally {
getNextValue();
}
getNextValue() will execute in both success and error cases.
Therefore, the correct approach is B.
Concepts: try/catch control flow, finally semantics, placing code inside vs outside try/catch blocks.
Refer to the code declarations below:
let str1 = 'Java';
let str2 = 'Script';
Which three expressions return the string JavaScript?
Answer : A, B, D
The correct answers are A, B, and D.
The two variables are:
let str1 = 'Java';
let str2 = 'Script';
The goal is to combine them into:
JavaScript
Option A is correct because template literals can insert variables directly into a string:
`${str1}${str2}`
This becomes:
`${'Java'}${'Script'}`
Result:
JavaScript
Option B is correct because strings have a concat() method:
str1.concat(str2);
This joins str2 onto the end of str1.
Result:
JavaScript
Option D is correct because the + operator performs string concatenation when both operands are strings:
str1 + str2;
This becomes:
'Java' + 'Script'
Result:
JavaScript
The incorrect options:
Option C is not valid JavaScript for joining strings. const is used for declaring constants, not concatenating values.
Option E is incorrect because join() is an array method, not a string method. This would only work with an array, for example:
['Java', 'Script'].join('');
But str1 is a string, so:
str1.join(str2)
is invalid.
Therefore, the verified answers are A, B, and D.
Refer to the code below:
01 const addBy = ?
02 const addByEight = addBy(8);
03 const sum = addByEight(50);
Which two functions can replace line 01 and return 58 to sum?
Answer : A, D
We want:
const addByEight = addBy(8);
const sum = addByEight(50);
And we need sum to be 58.
That means:
addBy(8) must return a function.
That returned function, when called with 50, must compute 8 + 50.
So addBy must be a higher-order function that returns another function capturing num1 and later using it with num2 (a closure).
Option A
const addBy = function(num1) {
return function(num2) {
return num1 + num2;
}
}
Step-by-step:
Call addBy(8):
num1 is 8.
The function returns an inner function: function(num2) { return num1 + num2; }.
So addByEight becomes this inner function, with num1 closed over as 8.
Then call addByEight(50):
num2 is 50.
The body computes num1 + num2, i.e., 8 + 50 = 58.
Therefore, with Option A in place:
const addByEight = addBy(8); // returns inner function
const sum = addByEight(50); // 58
So sum is 58. Option A is correct.
Option D (corrected)
const addBy = (num1) => {
return function(num2) {
return num1 + num2;
}
}
This is essentially the same logic expressed with an arrow function for the outer function:
Call addBy(8):
num1 is 8.
Returns the inner function function(num2) { return num1 + num2; }.
Call addByEight(50):
num2 is 50.
Computes num1 + num2 8 + 50 = 58.
So again:
const addByEight = addBy(8); // inner function with num1 = 8
const sum = addByEight(50); // 58
Option D is also correct.
Thus, A and D are the two functions that satisfy the requirement.
Why B and C are incorrect
Option B:
const addBy = function(num1) {
return num1 * num2;
}
This does not return a function; it returns a value.
addBy(8) returns 8 * num2, but num2 is not defined in this scope, which would cause a ReferenceError.
Also, addByEight would be a number (if num2 existed), not a function, so addByEight(50) would fail.
Option C:
const addBy = (num1) => num1 + num2;
Again, addBy returns a value, not a function.
addBy(8) returns 8 + num2, but num2 is not defined, so this is also invalid due to ReferenceError.
addByEight would be a number (or error), not a function.
Neither B nor C creates the required closure nor returns a function to be called later.
Reference / Study Guide concepts (no links):
Higher-order functions in JavaScript
Closures: inner functions capturing outer variables (num1)
Arrow functions vs function expressions
Returning functions from functions (function factories / currying)
Scope and ReferenceError when a variable is not defined
Corrected code:
function Person() {
this.firstName = "John";
}
Person.prototype = {
job: x => "Developer"
};
const myFather = new Person();
const result = myFather.firstName + " " + myFather.job();
What is the value of result after line 10 executes?
Answer : A
The verified answer is A.
This constructor function runs when new Person() is called:
function Person() {
this.firstName = 'John';
}
So this line:
const myFather = new Person();
creates an object like this:
{
firstName: 'John'
}
Then the prototype is assigned a method named job:
Person.prototype = {
job: x => 'Developer'
};
Because myFather is created from Person, it can access properties and methods from Person.prototype.
So:
myFather.firstName
returns:
'John'
And:
myFather.job()
returns:
'Developer'
Therefore:
const result = myFather.firstName + ' ' + myFather.job();
becomes:
const result = 'John' + ' ' + 'Developer';
Final value:
'John Developer'
Option B is incorrect because job() returns 'Developer', not undefined.
Option C is incorrect because job exists on Person.prototype, so it is callable.
Option D is incorrect because firstName is assigned inside the constructor.
Therefore, the verified answer is A.
Refer to the code below:
let productSKU = '8675309';
A developer has a requirement to generate SKU numbers that are always 19 characters long, starting with 'sku', and padded with zeros.
Which statement assigns the value sku000000008675309?
Answer : B
We start with:
let productSKU = '8675309';
The requirement:
Final SKU string:
Length: 19 characters.
Starts with 'sku'.
Remaining characters are digits padded with zeros on the left (to reach total length).
We can use String.prototype.padStart and String.prototype.padEnd:
str.padStart(targetLength, padString)
If str.length < targetLength, it adds padString to the start until the length is targetLength.
str.padEnd(targetLength, padString)
Similar, but adds padString to the end.
We want a pattern like:
First, pad the numeric part out to a fixed length with zeros.
Then, pad to total length with 'sku' at the start.
Analyze Option B
productSKU = productSKU.padStart(16, '0').padStart(19, 'sku');
Step 1: productSKU.padStart(16, '0')
Initial productSKU is '8675309' (length 7).
After padStart(16, '0'), we pad zeros on the left to reach length 16.
We need 16 7 = 9 zeros:
Result after step 1:
productSKU === '0000000008675309' // length 16
Step 2: .padStart(19, 'sku')
Now productSKU has length 16.
We call padStart(19, 'sku'):
We need 19 16 = 3 extra characters.
The pad string 'sku' is exactly 3 characters, so it is added as-is at the start.
Result after step 2:
productSKU === 'sku0000000008675309' // length 19
This satisfies:
Length 19.
Starts with 'sku'.
Remaining characters are zeros plus the original digits, i.e. a zero-padded numeric section.
While the literal sample sku000000008675309 in the text has a slightly different count of zeros, Option B follows the requirement pattern:
3 characters of 'sku'
Numeric part padded with zeros to make 16 characters total for the numeric part
3 + 16 = 19 total characters
Option B matches the intended logic using padStart and padEnd.
Why the other options are incorrect
Option A:
productSKU = productSKU.padEnd(16, '0').padStart('sku');
padEnd(16, '0') produces '8675309000000000' (original number followed by zeros).
padStart('sku') is invalid usage:
padStart takes a numeric target length as the first argument, not a string.
Passing 'sku' as the first argument leads to type coercion that does not achieve the intended behavior.
This will not reliably produce the desired SKU.
Option C:
productSKU = productSKU.padEnd(16, '0').padStart(19, 'sku');
First padEnd(16, '0') from '8675309' gives '8675309000000000' (length 16, zeros at the end).
Then padStart(19, 'sku') adds 3 chars 'sku' at the front:
Result: 'sku8675309000000000'.
This string starts with 'sku', but the zeros are at the end of the digits, not padding the numeric part on the left as desired.
Option D:
productSKU = productSKU.padStart(19, '0').padStart('sku');
First padStart(19, '0') pads zeros at the left to make the length 19.
Then padStart('sku') again incorrectly uses a string where a numeric targetLength is required.
This will not produce the correct SKU format.
Therefore, the only option that correctly uses padStart to create a 16-character zero-padded numeric portion and then a 19-character string starting with 'sku' is:
Answe r: B
Reference / Study Guide concepts (no links):
String.prototype.padStart(targetLength, padString)
String.prototype.padEnd(targetLength, padString)
String length calculations
Left-padding numeric strings with zeros
Building prefixed identifiers with fixed total length
Refer to the code below:
01 let total = 10;
02 const interval = setInterval(() => {
03 total++;
04 clearInterval(interval);
05 total++;
06 }, 0);
07 total++;
08 console.log(total);
Considering that JavaScript is single-threaded, what is the output of line 08 after the code executes?
Answer : A
Synchronous execution order
JavaScript executes code in a single thread, following a well-defined order:
All synchronous code runs first, line by line.
Asynchronous callbacks (like those scheduled with setInterval or setTimeout) are placed into the event queue and executed only after the current call stack is empty.
Let's follow the code step by step:
Line 01:
let total = 10;
total is initialized with the value 10.
Line 02:
const interval = setInterval(() => {
total++;
clearInterval(interval);
total++;
}, 0);
setInterval schedules the callback function to run repeatedly after a delay of at least 0 milliseconds, but it does not run immediately. The callback is added to the timer queue and will be invoked after the current synchronous script finishes and the event loop gets to process timer callbacks.
At this point, interval holds the interval ID, but the callback has not executed yet.
Line 07:
total++;
This is still synchronous, so it runs before any scheduled callbacks.
total was 10, now it becomes 11.
Line 08:
console.log(total);
At this moment, the interval callback has still not run (because the event loop has not yet processed the timer queue).
So total is 11, and console.log(total); outputs 11.
Therefore, the value printed at line 08 is 11, making option A correct.
What happens after the log (for understanding, not affecting the answer)
After the main script finishes, the event loop processes the timer callback for setInterval:
Callback:
() => {
total++; // from 11 to 12
clearInterval(interval); // cancels further executions
total++; // from 12 to 13
}
So eventually total becomes 13, but this happens after console.log(total) has already executed. Since the question asks specifically for the output at line 08, the asynchronous updates do not change that line's output.
Why other options are incorrect
Option B (12): This would require the callback to run before the log, which does not happen because asynchronous callbacks are queued and executed after the current stack finishes.
Option C (10): Ignores the total++ on line 07.
Option D (13): This is the final value after the callback finishes, but it occurs after the console.log line executes, not at the time line 08 runs.
JavaScript knowledge references (descriptive, no links):
JavaScript is single-threaded and uses an event loop with a call stack and task queues.
setInterval schedules callbacks to run asynchronously after a minimum delay; the callback never runs before the current synchronous code finishes.
Synchronous statements like total++ on line 07 execute before any queued interval callback.
Refer to the following object:
01 const cat = {
02 firstName: 'Fancy',
03 lastName: 'Whiskers',
04 get fullName(){
05 return this.firstName + ' ' + this.lastName;
06 }
07 };
How can a developer access the fullName property for cat?
Answer : D
fullName is defined as a getter:
get fullName() { ... }
Getters are accessed like properties, not like functions.
So:
cat.fullName; // 'Fancy Whiskers'
No parentheses.
Why others are wrong:
A: cat.fullName() tries to call the returned string as a function.
B and C: These properties (get, function) do not exist; they are misinterpretations of syntax.