Salesforce certification · Exam Salesforce-JavaScript-Developer

Salesforce-JavaScript-Developer Mock Exam Page 5

Salesforce JavaScript Developer - JS-Dev-101

Free Salesforce-JavaScript-Developer mock exam questions 41–42, each with the answer and an explanation.

20 questions · 33 minutes · free, no sign-up

Free Salesforce-JavaScript-Developer questions

41–42 of 42 free
Question 41 Choose 1
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?
Show answerHide answer

Correct answer: D Rejected Resolved4

Explanation

Explanation:
This question tests your understanding of promise chaining and error handling with catch(). The execute() function returns a Promise that immediately rejects (calls reject()). The initial promise chain starts with .then() handlers for the resolved case, but since the promise rejects, all .then() handlers before the first .catch() are skipped. The .catch() handler executes and logs 'Rejected'. After .catch(), the chain continues with .then() because a .catch() returns a new resolved promise by default (unless it throws an error). Therefore, 'Resolved4' is logged after 'Rejected'. The final output is 'Rejected' followed by 'Resolved4'.

Correct Option:

D. Rejected Resolved4 – Correct. Let's trace the execution:

execute() returns a Promise that rejects immediately.

The first .then() on line 07 is skipped because the promise is rejected.

The second .then() on line 08 is skipped.

The third .then() on line 09 is skipped.

The .catch() on line 10 catches the rejection and logs 'Rejected'.

The .catch() returns a resolved promise by default (no error thrown), so the chain continues.

The final .then() on line 11 executes and logs 'Resolved4'.

Final output: Rejected Resolved4.

Incorrect Options:

A. Resolved1 Resolved2 Resolved3 Rejected Resolved4 –
Incorrect. The initial promise rejects, so none of the .then() handlers before the .catch() execute. This option incorrectly assumes the .then() handlers run before the .catch(). They are skipped entirely.

B. Rejected –
Incorrect. This option ignores the final .then() after the .catch(). Once the .catch() handles the rejection, it returns a new resolved promise, allowing the subsequent .then() to execute. The chain does not stop after .catch() unless an error is thrown or the chain is intentionally terminated.

C. Resolved1 Resolved2 Resolved3 Resolved4 –
Incorrect. This option completely ignores the rejection. It would only be correct if the promise resolved instead of rejected. Since the promise rejects, the .catch() executes and the preceding .then() handlers are skipped.

Reference:

MDN Web Docs – Promise.prototype.catch() method

MDN Web Docs – Promise chaining and error handling

MDN Web Docs – Returning from catch and continuing chains

Salesforce Trailhead – JavaScript Essentials: Asynchronous Programming and Promises
Question 42 Choose 1
Correct implementation of try...catch for countsDeep():
Show answerHide answer

Correct answer: B

Explanation

Explanation:
This question tests your understanding of error handling with asynchronous operations in JavaScript. The countsDeep() function is presumably an asynchronous operation that may throw an error after some delay. When using setTimeout(), the function executes asynchronously after the timer expires. By that time, the surrounding try/catch block has already completed execution. Therefore, a try/catch block placed outside setTimeout() cannot catch errors thrown inside the delayed callback. To catch such errors, the try/catch must be placed inside the callback function, where the asynchronous code actually executes.

Correct Option:

B.

javascript
setTimeout(function() {
try {
countsDeep();
} catch (e) {
handleError(e);
}
}, 1000);

This is correct. The try/catch block is placed inside the setTimeout callback function. When countsDeep() executes after the 1000ms delay, any error thrown will be caught by the catch block, which then calls handleError(e). This correctly handles errors that occur during the asynchronous execution of countsDeep().

Incorrect Options:

A.

javascript
try {
countsDeep();
} handleError (e){
catch(e);
}

Incorrect. This uses invalid syntax. The correct syntax for try/catch is try { ... } catch (e) { ... }. The handleError and catch(e) syntax is not valid in JavaScript. Additionally, this places the try/catch outside any asynchronous wrapper, so if countsDeep() is asynchronous, the error may not be caught.

C.

javascript
try {
setTimeout(function() {
countsDeep();
}, 1000);
} catch (e) {
handleError(e);
}

Incorrect. The try/catch is placed outside the setTimeout. When setTimeout is called, it schedules the callback and returns immediately. The try/catch block completes before the callback executes. If countsDeep() throws an error after 1000ms, it will occur outside the try/catch scope, resulting in an uncaught error.

D. (Duplicate of C) –
Incorrect. Same reasoning as option C. The try/catch is outside the setTimeout and cannot catch errors thrown inside the asynchronous callback.

Reference:

MDN Web Docs – try...catch statement and asynchronous code

MDN Web Docs – setTimeout and error handling

MDN Web Docs – Call stack and event loop

Salesforce Trailhead – JavaScript Essentials: Error Handling and Asynchronous Programming

That's the last free question

You've seen the free 30% (42 of 147 questions). Full access unlocks every question and full-length Salesforce-JavaScript-Developer mock exams (65 questions, 105 minutes), a different mock on every attempt.

Unlock full mock exam
Full mock exam

Sit the Salesforce-JavaScript-Developer exam before exam day

Full-length mocks with the same number of questions and the same time limit as the real Salesforce-JavaScript-Developer exam. Every attempt draws a new set of questions from the full bank, so you can keep going until you pass comfortably.

Get full Salesforce-JavaScript-Developer mock exam
  • 65questions per mock
  • 105 mintime limit
  • 147questions in the bank
  • Newmock on every attempt