Which statement accurately describes an aspect of promises?
Answer : A
Evaluate each option:
A . Arguments for .then() are optional.
This is correct.
.then() has the signature:
promise.then(onFulfilled?, onRejected?)
Both arguments (onFulfilled and onRejected) are optional.
If a callback is not supplied, JavaScript provides a default pass-through handler.
B . .then() cannot be added after a catch.
Incorrect.
Promises support chaining in any order:
promise
.catch(...)
.then(...);
After a catch, the chain continues normally.
C . .then() manipulates and returns the original promise.
Incorrect.
.then() always returns a new promise, not the original one.
This is fundamental to promise chaining behavior.
D . Returning values in .then() is not necessary.
Incorrect.
If you want the next .then() in the chain to receive a value, the current .then() must explicitly return it:
.then(value => {
return value * 2; // passes to next .then()
})
If nothing is returned, the next .then() receives undefined.
Why A is correct
It is the only statement that accurately describes built-in Promise behavior:
.then() accepts optional arguments.
JavaScript Knowledge Reference (text-only)
.then(onFulfilled?, onRejected?) accepts optional handlers.
Promise chaining creates new promises for each .then().
catch() can be followed by additional .then() calls.
Returning inside .then() passes values to the next step in the chain.
A developer publishes a new version of a package with bug fixes but no breaking changes. The old version number was 2.1.1.
What should the new package version number be based on semantic versioning?
Answer : A
Semantic versioning: MAJOR.MINOR.PATCH
MAJOR: incompatible API changes.
MINOR: add functionality in a backward compatible manner.
PATCH: backward compatible bug fixes.
Here:
Bug fixes only, no breaking changes increment PATCH.
From 2.1.1 to 2.1.2.
So the correct new version is 2.1.2.
Refer to the code below:
01 let o = {
02 get js() {
03 let city1 = String('St. Louis');
04 let city2 = String('New York');
05
06 return {
07 firstCity: city1.toLowerCase(),
08 secondCity: city2.toLowerCase(),
09 }
10 }
11 }
What value can a developer expect when referencing o.js.secondCity?
Answer : D
1. Getter Functions in JavaScript
In JavaScript, when an object uses the get keyword, it defines a getter method. Accessing a getter property executes the function and returns its value. Thus:
o.js
does not return the getter function; instead, it executes the function located at:
get js() { ... }
and returns the object inside the return block.
2. Behavior of String() and toLowerCase()
Inside the getter:
let city1 = String('St. Louis');
let city2 = String('New York');
String() creates a string value.
Then, the returned object is constructed as:
{
firstCity: city1.toLowerCase(),
secondCity: city2.toLowerCase(),
}
The method toLowerCase() is a standard JavaScript string method that returns a new string with all alphabetic characters converted to lowercase.
Therefore:
city2.toLowerCase()
returns:
'new york'
3. Referencing the Property
When the developer writes:
o.js.secondCity
the following happens:
The getter js runs and returns an object.
The returned object includes the property:
secondCity: 'new york'
Accessing .secondCity retrieves the lowercase string 'new york'.
Therefore, the correct value is 'new york'.
Why the Other Options Are Incorrect
A . undefined -- incorrect because the property secondCity clearly exists in the returned object.
B . An error -- incorrect because no invalid operations occur; all methods and properties are valid.
C . 'New York' -- incorrect because toLowerCase() transforms the string to lowercase.
JavaScript Knowledge Reference (Text-Based)
Getter methods using the get keyword return computed values when accessed.
JavaScript String values support the toLowerCase() method, which returns a lowercase version of the original string.
Accessing nested properties like o.js.secondCity triggers the getter, returning the constructed object.
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.
A developer creates a simple webpage with an input field. When a user enters text and clicks the button, the actual value must be displayed in the console:
HTML:
JavaScript:
01 const button = document.querySelector('button');
02 button.addEventListener('click', () => {
03 const input = document.querySelector('input');
04 console.log(input.getAttribute('value'));
05 });
When the user clicks the button, the output is always "Hello".
What needs to be done to make this code work as expected?
Answer : C
getAttribute('value')
This returns the initial HTML attribute, not the live, updated value.
Even if the user edits the text, the value attribute remains 'Hello' because HTML attributes do not update dynamically.
Input elements have a property value that always reflects the live current text inside the field.
So:
input.value
returns the user-entered value.
Therefore, line 04 must use the property, not the attribute:
console.log(input.value);
This ensures the updated input value is displayed.
JavaScript knowledge references (text-only)
HTML attributes are static and retrieved using getAttribute().
DOM element properties (like value) represent the current live state.
Input value changes update the .value property, not the attribute.
==================================================
A developer needs the function personalizeWebsiteContent to run when the webpage is fully loaded (HTML and all external resources).
Which implementation should be used?
Answer : B
JavaScript defines two main page-loading events:
DOMContentLoaded
Fires when the HTML document has been completely parsed.
External resources like images, stylesheets, iframe contents, etc. may not be loaded yet.
load event on window
Fires when:
HTML is fully parsed
All dependent resources (images, scripts, CSS, fonts, etc.) are fully loaded
The requirement states:
''when the webpage is fully loaded (HTML content and all related files)''
This aligns exactly with the window load event.
Implementation:
window.addEventListener('load', personalizeWebsiteContent);
This matches option B.
JavaScript knowledge references (text-only)
DOMContentLoaded fires after HTML parsing only.
load fires after all page resources finish loading.
The window object is the correct target for listening to the full load event.
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