Refer to the code below:
01 let first = 'Who';
02 let second = 'What';
03 try {
04 try {
05 throw new Error('Sad trombone');
06 } catch (err) {
07 first = 'Why';
08 throw err;
09 } finally {
10 second = 'When';
11 }
12 } catch (err) {
13 second = 'Where';
14 }
What are the values for first and second once the code executes?
Initial values:
first = 'Who'
second = 'What'
Execution:
Inner try/catch/finally:
Line 05: throw new Error('Sad trombone');
Control goes to the inner catch.
Inner catch (lines 06--08):
catch (err) {
first = 'Why';
throw err;
}
first is set to 'Why'.
The error is rethrown.
Inner finally (lines 09--11):
finally {
second = 'When';
}
finally runs whether or not there was an error.
second becomes 'When'.
After inner finally, the rethrown error continues to propagate to the outer catch.
Outer catch (lines 12--14):
} catch (err) {
second = 'Where';
}
Because the inner try rethrew, the outer catch runs.
It sets second = 'Where'.
Final values:
first was changed to 'Why' in the inner catch and never changed again.
second became 'When' in the inner finally, and then 'Where' in the outer catch.
So:
first is 'Why'
second is 'Where'
Option B is correct.
Concepts: nested try/catch/finally, rethrowing errors, order of execution for catch vs finally, variable mutation through error propagation.
01 function changeValue(obj) {
02 obj.value = obj.value / 2;
03 }
04 const objA = {value: 10};
05 const objB = objA;
06
07 changeValue(objB);
08 const result = objA.value;
What is the value of result after the code executes?
objA is { value: 10 }.
objB = objA; both variables reference the same object.
changeValue(objB);:
obj.value = obj.value / 2; // modifies the shared object
So:
objA.value becomes 10 / 2 = 5.
Thus result is 5.
Why does second have access to variable a?
The answer is B because JavaScript uses lexical scope.
When a function is created inside another function, the inner function can access variables from the outer function where it was defined.
For example:
function first() {
let a = 10;
function second() {
console.log(a);
}
second();
}
Here, a belongs to the outer function first(). The inner function second() can access it because JavaScript looks for variables through the scope chain.
The lookup works like this:
1. Look inside second()
2. If not found, look inside first()
3. If not found, look in the global scope
Since a is found in the outer function's scope, second() can use it.
This is also the basis of a closure. A closure allows an inner function to remember and access variables from its outer lexical environment.
Why the other options are incorrect:
A is not the best answer because a is not declared inside second() itself.
C is incorrect because the prototype chain is used for object property lookup, not local variable scope lookup.
D is incorrect because hoisting explains how declarations are moved during compilation, but it does not explain why an inner function can access an outer function's variable.
Therefore, the verified answer is B.
Refer to the code below:
01
02
| Click me! |
07
08 function printMessage(event) {
09 console.log('Row log');
10 event.stopPropagation();
11 }
12
13 let elem = document.getElementById('row1');
14 elem.addEventListener('click', printMessage, false);
15
16
Which code change should be done for the console to log the following when "Click me!" is clicked?
Row log
Table log
Current behavior:
Clicking <td> triggers the click event on row1, then bubbles up to <table>.
printMessage runs, logs 'Row log', then event.stopPropagation() stops the event from bubbling to the table.
So 'Table log' never appears.
To allow the table's inline onclick to run after the row handler:
Remove the propagation stop:
function printMessage(event) {
console.log('Row log');
// event.stopPropagation(); // remove this
}
Now the event bubbles:
printMessage logs 'Row log'.
The table's onclick runs, logging 'Table log'.
Option A only changes capture/bubble phase but still stops propagation. C does nothing meaningful (stopPropagation takes no arguments). B removes the row handler entirely.
Refer to the code:
01 function execute() {
02 return new Promise((resolve, reject) => reject());
03 }
04 let promise = execute();
05
06 promise
07 .then(() => console.log('Resolved1'))
08 .then(() => console.log('Resolved2'))
09 .then(() => console.log('Resolved3'))
10 .catch(() => console.log('Rejected'))
11 .then(() => console.log('Resolved4'));
What is the result when the Promise in the execute function is rejected?
execute() returns a Promise that immediately calls reject().
So promise starts in a rejected state.
When a Promise is rejected and you chain .then() calls without rejection handlers, all those .then() callbacks are skipped until a .catch() is encountered:
promise
.then(...) // skipped
.then(...) // skipped
.then(...) // skipped
.catch(...) // executed
.then(...); // executed after catch
Execution:
.then(() => console.log('Resolved1')) is skipped.
.then(() => console.log('Resolved2')) is skipped.
.then(() => console.log('Resolved3')) is skipped.
.catch(() => console.log('Rejected')) runs and logs Rejected.
The .catch() returns a resolved Promise (no explicit return, so undefined), so the next .then() runs:
.then(() => console.log('Resolved4')) logs Resolved4.
Final output:
Rejected
Resolved4
This matches option D.
Sandra Morgan
13 days agoStephen Roberts
1 month agoDorothy Jackson
1 month agoHeather Moore
2 months agoAdam Flores
3 months agoAmanda Cooper
3 months agoAmy Thomas
4 months agoDavid Jackson
5 months agoKaren Jones
5 months agoCharles Carter
5 months agoHeather Bell
6 months agoBrenda Clark
6 months agoDeborah Mitchell
5 months agoJeffrey Bailey
6 months agoRobert Anderson
6 months agoGerald Ramirez
5 months agoMalcom
6 months agoZoila
7 months agoMicaela
7 months agoKendra
7 months agoPhyliss
7 months agoLeandro
8 months agoHelga
8 months agoBrent
8 months ago