- Leo
- Tony
- Tiger
Refer to the code below:
flag();
function flag() {
console.log('flag');
}
const anotherFlag = () => {
console.log('another flag');
}
anotherFlag();
What is result of the code block?
Answer : D
Comprehensive and Detailed Explanation From Exact Extract JavaScript Knowledge:
Key points:
Function declarations are hoisted (their definitions are available before the line where they appear).
Function expressions assigned to const or let are not hoisted as callable functions, but here anotherFlag is only used after it is defined.
Step-by-step:
flag(); at the top:
flag is a function declaration defined later; due to hoisting, it is available.
It logs 'flag'.
The declaration:
function flag() {
console.log('flag');
}
is already in effect (hoisted before execution).
const anotherFlag = () => { console.log('another flag'); } defines anotherFlag as an arrow function.
anotherFlag(); is called after its definition; this is valid and logs 'another flag'.
No errors occur. The console output is:
flag
another flag
So option D is correct.
Concepts: function hoisting, difference between function declarations and function expressions, execution order.
________________________________________
HTML:
The current status of an Order: In Progress
Which JavaScript statement changes 'In Progress' to 'Completed'?
Answer : C
________________________________________
Comprehensive and Detailed Explanation From Exact Extract JavaScript Knowledge
To select an element by ID, JavaScript uses:
document.getElementById('status')
The ID must be passed without any selector prefixes (no dot, no hash).
Then, to update the text inside:
element.innerHTML = 'Completed';
Why the others are wrong:
A uses '.status' which would match a CSS class, not an ID.
B incorrectly prepends #, which is used in CSS selectors but not in getElementById.
D attempts to use .Value with capital V, which is incorrect for DOM text; .value only applies to form elements.
Thus, option C is correct.
________________________________________
JavaScript Knowledge Reference (text-only)
getElementById takes a raw ID string with no prefixes.
innerHTML updates an element's displayed HTML content.
==================================================
Given:
const str = 'Salesforce';
Which two statements result in 'Sales'?
Answer : A, C
Comprehensive and Detailed Explanation From Exact Extract JavaScript Knowledge:
str = 'Salesforce'
Index positions:
S(0) a(1) l(2) e(3) s(4) f(5) o(6) r(7) c(8) e(9)
substring(start, end)
Start index inclusive, end index exclusive.
str.substring(0, 5) characters at indices 0--4 'Sales'
str.substring(1, 5) indices 1--4 'ales'
substr(start, length)
Start index, then number of characters.
str.substr(0, 5) 5 characters starting at index 0 'Sales'
str.substr(1, 5) 5 characters from index 1 'alesf'
So the expressions that return 'Sales' are A and C.
________________________________________
Refer to the HTML below:
Which JavaScript statement results in changing "Leo" to "The Lion"?
Answer : D
Comprehensive and Detailed Explanation From Exact Extract JavaScript Knowledge:
We want to select the first <li> under #main and change its content.
document.querySelector(selector) returns the first element matching the CSS selector.
In the DOM, the <li> elements under <ul> are children in order:
1st child: 'Leo'
2nd child: 'Tony'
3rd child: 'Tiger'
li:nth-child(1) is the CSS pseudo-class that selects the first <li> inside its parent.
So:
document.querySelector('#main li:nth-child(1)').innerHTML = 'The Lion';
selects the first <li> ('Leo') inside #main and sets its innerHTML to 'The Lion'.
Why others are incorrect:
A . #main #Leo
There is no element with id 'Leo'. id is not set on the <li>, so the selector matches nothing.
B . li:second-child
There is no :second-child pseudo-class. The valid pseudo-class is :nth-child(2); and that would select 'Tony', not 'Leo'.
C . '#main li,Leo'
This selector means ''all #main li and all elements named <Leo>''.
There is no <Leo> tag, and querySelectorAll returns a NodeList, which does not have a single innerHTML property.
Therefore, D is the correct statement.
Study Guide Concepts:
document.querySelector vs querySelectorAll
CSS selectors: nth-child()
DOM element innerHTML property
________________________________________
A developer writes the code below to return a message to a user attempting to register a new
username. If the username is available, a variable named msg is declared and assigned a value on
line 03.
```javascript
function getAvailabilityMessage(item) {
if (getAvailability(item)) {
var msg = "Username available";
return msg;
}
}
Answer : C
Refer to the code below:
01 async function functionUnderTest(isOK) {
02 if (isOK) return 'OK';
03 throw new Error('not OK');
04 }
Which assertion accurately tests the above code?
Answer : A
Comprehensive and Detailed Explanation From Exact Extract JavaScript knowledge:
The function:
async function functionUnderTest(isOK) {
if (isOK) return 'OK';
throw new Error('not OK');
}
Behavior:
If isOK is true:
The function resolves (fulfills the promise) with value 'OK'.
If isOK is false:
The function throws, which in an async function becomes a rejected promise with Error('not OK').
We want an assertion that accurately tests the successful path for isOK === true.
Key points about console.assert:
Signature: console.assert(condition, message?)
If condition is falsy, it logs message as an assertion failure.
If condition is truthy, nothing is logged.
We also must understand await:
await functionUnderTest(true) will resolve to the string 'OK'.
Now evaluate options.
Option A:
console.assert(await functionUnderTest(true), 'OK');
await functionUnderTest(true) 'OK' (truthy).
console.assert('OK', 'OK');
Condition is 'OK' (truthy), so assertion passes.
The message 'OK' is only shown if the condition is falsy, which it is not.
This correctly verifies that the promise resolved (i.e., did not reject). Among the given options, this is the only one that both:
Uses await properly on the async function, and
Associates the message with the expected success 'OK'.
Option B:
console.assert(await (functionUnderTest(true), 'not OK'));
Inside the parentheses, the comma operator (a, b) evaluates a, discards it, and returns b.
So (functionUnderTest(true), 'not OK'):
Calls functionUnderTest(true) (returns a promise, but its result is discarded),
The expression evaluates to the string 'not OK'.
Then await 'not OK' just resolves to 'not OK' immediately (not a promise).
console.assert('not OK');
Condition is 'not OK' (truthy), so the assertion passes regardless of what functionUnderTest actually does.
This does not meaningfully test the function and is misleading.
Option C:
console.assert(functionUnderTest(true), 'OK');
functionUnderTest(true) returns a Promise, not the string 'OK' directly.
A Promise object is always truthy.
So console.assert(promise, 'OK') will pass, even if the promise later rejects.
Also, there is no await, so we are not actually waiting for the async result.
This is not a correct way to assert on an async function's resolved value.
Option D:
console.assert(await functionUnderTest(true), 'not OK');
await functionUnderTest(true) still resolves to 'OK' (truthy).
console.assert('OK', 'not OK'); passes.
However, the message 'not OK' is the opposite of what we expect for the success case and is only used when the condition fails.
This makes the assertion logically inconsistent with the function behavior (it would show 'not OK' if the assertion failed).
Therefore, the only assertion that:
Properly waits on the async function, and
Has expectation text that matches the successful behavior ('OK')
is:
Study Guide / Concept Reference (no links):
async and await behavior in JavaScript
Promises resolving vs rejecting in async functions
console.assert(condition, message) usage
Truthy and falsy values in JavaScript
Comma operator (a, b) semantics
________________________________________
A Node.js server library uses events and callbacks. The developer wants to log any issues the server has at boot time.
Which code logs an error with an event?
Answer : C
________________________________________
Comprehensive and Detailed Explanation From Exact Extract JavaScript Knowledge
Node.js event-based modules use the EventEmitter pattern.
The correct syntax for listening to events is:
emitter.on('eventName', callback)
The server library emits an 'error' event, which must be listened to using .on.
Option analysis:
A: .catch is for Promises, not EventEmitters.
B: .error is not an EventEmitter method.
C: Correct. Listens to the 'error' event.
D: try...catch only captures synchronous errors, not event-based asynchronous errors.
Therefore, the correct answer is option C.
________________________________________
JavaScript Knowledge Reference (text-only)
The EventEmitter API uses on(event, handler) to listen for events.
Errors emitted asynchronously cannot be caught with try...catch.
The 'error' event is standard for Node.js modules to signal operational errors.