Salesforce Certified JavaScript Developer JS-Dev-101 Exam Questions

Page: 1 / 14
Total 147 questions
Question 1

Refer to the code below:

01 let country = {

02 get capital() {

03 let city = Number("London");

04

05 return {

06 cityString: city.toString(),

07 }

08 }

09 }

Which value can a developer expect when referencing country.capital.cityString?



Answer : B

In the getter:

let city = Number('London');

The Number() constructor attempts to convert the string 'London' into a numeric value.

'London' is not a valid numeric string.

When JavaScript attempts numeric conversion of a non-numeric string:

Number('London') NaN

Next line:

city.toString()

When city is NaN, calling .toString() yields:

NaN.toString() 'NaN'

The getter returns:

{

cityString: 'NaN'

}

The question asks:

Which value can a developer expect?

The multiple-choice options include NaN, but not 'NaN'.

Given the available choices, the intended correct answer is B (NaN) because the root cause is the Number('London') conversion returning NaN.

JavaScript Knowledge Reference (text-only)

Number(nonNumericString) returns NaN.

NaN.toString() produces 'NaN'.

Getters compute and return their values each time the property is accessed.

==================================================


Question 2

A developer wants to advocate for a mature, well-supported web framework/library instead of a new one (Minimalist.js).

Which two should be recommended?



Answer : A, C

React and Vue are:

Mature front-end JavaScript frameworks/libraries.

Have large communities.

Are widely supported and used in production across industries.

Appropriate for building browser-based Single Page Applications.

Express and Koa are server-side frameworks, not front-end SPA frameworks.

The scenario describes building a browser SPA, so the correct seasoned options are React and Vue.

JavaScript Knowledge Reference (text-only)

React and Vue are client-side frameworks for SPA development.

Express and Koa are backend frameworks for Node.js.

==================================================


Question 3

Which two code snippets show working examples of a recursive function?



Answer : C, D

A recursive function is a function that calls itself and has a base case to terminate the recursion.

Evaluate each option:

Option A:

const sumToTen = numVar => {

if (numVar < 0)

return;

return sumToTen(numVar + 1);

};

This function calls itself: sumToTen(numVar + 1) --- so it is recursive.

However, the base condition is if (numVar < 0) return;.

If you call sumToTen(0):

numVar < 0 is false, so it calls sumToTen(1), then sumToTen(2), and so on, incrementing forever.

There is no condition to stop the recursion when numVar increases; it will eventually cause a stack overflow.

This code does not represent a properly working recursive function with a valid termination for increasing values and is not a good example of correct recursion.

Option B:

function factorial(numVar) {

if (numVar < 0) return;

if (numVar === 0) return 1;

return numVar - 1;

}

This function does not call itself anywhere.

It has conditional returns, but there is no recursive call such as factorial(numVar - 1).

Therefore, it is not recursive at all.

Option C:

const factorial = numVar => {

if (numVar < 0) return;

if (numVar === 0) return 1;

return numVar * factorial(numVar - 1);

};

This is a classic recursive factorial implementation.

It calls itself with a smaller argument: factorial(numVar - 1).

Base cases:

If numVar < 0, it simply returns (could be treated as invalid input).

If numVar === 0, it returns 1, which is the mathematical definition of 0! (zero factorial).

For positive integers, it correctly multiplies numVar by factorial(numVar - 1) until it reaches the base case.

This is a correct and working recursive function.

Option D (corrected):

let countingDown = function(startNumber) {

if (startNumber > 0) {

console.log(startNumber);

return countingDown(startNumber - 1);

} else {

return startNumber;

}

};

This function also calls itself: countingDown(startNumber - 1).

Base case:

When startNumber is not greater than 0 (i.e., 0 or negative), it returns startNumber and stops recursing.

For example, countingDown(3) would:

Log 3, call countingDown(2)

Log 2, call countingDown(1)

Log 1, call countingDown(0)

At 0, it hits the else branch and returns 0, ending the recursion.

This is a valid working recursive function structure (once syntax is corrected).

Therefore, the snippets that show working recursive functions are:

Answe r: C, D

Study Guide / Concept Reference (no links):

Definition of recursion: a function calling itself

Base case vs recursive step

Recursive factorial implementation

Recursive countdown example

Importance of a terminating condition to avoid infinite recursion


Question 4

A developer implements a function that adds a few values.

function sum(num1, num2, num3) {

if (num3 === undefined) {

num3 = 0;

}

return num1 + num2 + num3;

}

Which three options can the developer invoke for this function to get a return value of 10?



Answer : C, D, E

The verified corrected answers are C, D, and E.

This function is a normal function, not a curried function:

function sum(num1, num2, num3) {

if (num3 === undefined) {

num3 = 0;

}

return num1 + num2 + num3;

}

It expects values to be passed in the same function call:

sum(num1, num2, num3);

Now check the valid corrected options.

Option C:

sum(5, 5, 0);

Calculation:

5 + 5 + 0

Result:

10

So C is correct.

Option D:

sum(10, 0);

Here, num3 is not provided, so it is undefined.

The function checks:

if (num3 === undefined) {

num3 = 0;

}

So the calculation becomes:

10 + 0 + 0

Result:

10

So D is correct.

Option E:

sum(5, 2, 3);

Calculation:

5 + 2 + 3

Result:

10

So E is correct.

Why A and B are incorrect as originally written:

sum(5)(5);

This calls sum(5) first. Since num2 is missing, the result becomes NaN. Then JavaScript tries to call that returned value as a function, which causes a TypeError.

sum()(10);

This also calls sum() first, producing NaN, and then attempts to call NaN as a function.

Those styles would only work if sum were written as a curried function, but the given implementation is not curried.

Therefore, the verified corrected answers are C, D, and E.


Question 5

Refer to the code below:

class Student {

constructor(name) {

this._name = name;

}

displayGrade() {

console.log(`${this._name} got 70% on test.`);

}

}

class GraduateStudent extends Student {

constructor(name) {

super(name);

this._name = "Graduate Student " + name;

}

displayGrade() {

console.log(`${this._name} got 100% on test.`);

}

}

let student = new GraduateStudent("Jane");

student.displayGrade();

What is the console output?



Answer : C

The correct answer is C, after correcting the option text to match the actual code.

The object is created here:

let student = new GraduateStudent('Jane');

Because GraduateStudent extends Student, its constructor runs:

constructor(name) {

super(name);

this._name = 'Graduate Student ' + name;

}

The call to:

super(name);

runs the parent Student constructor first and temporarily sets:

this._name = 'Jane';

Then this line in the child constructor overwrites that value:

this._name = 'Graduate Student ' + name;

So the final value of this._name becomes:

'Graduate Student Jane'

Next, this line executes:

student.displayGrade();

Since student is an instance of GraduateStudent, JavaScript uses the overridden displayGrade() method from GraduateStudent, not the parent method from Student.

The executed method is:

displayGrade() {

console.log(`${this._name} got 100% on test.`);

}

Therefore, the console output is:

Graduate Student Jane got 100% on test.

Important correction: the original option C said something like 'Better student Jackie got 100% on test.', but the actual code uses 'Graduate Student ' and the name 'Jane'. The verified corrected answer remains C.


Question 6

Given the code below:

01 const delay = async delay => {

02 return new Promise((resolve, reject) => {

03 console.log(1);

04 setTimeout(resolve, delay);

05 });

06 };

07

08 const callDelay = async () => {

09 console.log(2);

10 const yup = await delay(1000);

11 console.log(3);

12 };

13

14 console.log(4);

15 callDelay();

16 console.log(5);

What is logged to the console?



Answer : A

Execution order:

Top-level code runs synchronously:

Line 14: console.log(4); logs 4.

Line 15: callDelay(); is called.

Inside callDelay:

Line 9: console.log(2); logs 2.

Line 10: await delay(1000);:

Calls delay(1000).

Inside delay(1000):

Line 3: console.log(1); logs 1.

Line 4: setTimeout(resolve, delay); schedules resolve in 1000 ms.

delay returns a pending Promise. await pauses callDelay here and returns control to the event loop.

Back to top-level:

Line 16: console.log(5); logs 5.

So synchronous log sequence is: 4, 2, 1, 5.

After ~1000 ms:

The setTimeout in delay resolves the Promise.

The await in callDelay resumes.

Line 11: console.log(3); logs 3.

Final log order: 4 2 1 5 3.

Both A and B show the same sequence; one must be chosen, so A is correct.

Concepts: async/await flow, Promise resolution timing, event loop, and ordering of synchronous vs timer callbacks.


Question 7

Refer to the following array:

let arr = [1, 2, 3, 4, 5];

Which two lines of code result in a second array, arr2, created such that arr2 is a reference to arr?



Answer : C, D

The correct answers are C and D.

Arrays in JavaScript are objects. When an array variable is assigned directly to another variable, both variables point to the same array in memory.

Option C is correct:

let arr2 = arr;

This does not create a new array. It creates another reference to the same array.

Example:

arr2.push(6);

console.log(arr);

Output:

[1, 2, 3, 4, 5, 6]

Changing arr2 also affects arr because both variables reference the same array.

Option D is also correct:

let arr2 = arr.sort();

The sort() method sorts the array in place and returns the same array reference. Therefore, arr2 refers to the same array object as arr.

The incorrect options create copies:

let arr2 = arr.slice(0, 5);

creates a shallow copy.

let arr2 = Array.from(arr);

also creates a new shallow copy.

So the two lines that make arr2 reference the original arr are C and D.


Page:    1 / 14   
Total 147 questions