SJDI

Free SJDI practice questions

10 questions from our SJDI practice tests, each with the correct answer and a written explanation. Read them with the answers shown, or switch to quiz mode and answer first.

Back to the SJDI practice exams

Question 1 of 10, Variables, Types, and Collections

A developer attaches metadata to DOM elements that are frequently removed from the page. The metadata must not keep removed elements alive in memory, and the code never needs to iterate over all entries. Which approach meets these requirements?

  1. AStore the metadata in a WeakMap keyed by the element object Correct answer
  2. BStore the metadata in a Map keyed by the element object
  3. CStore the metadata in a Set of { element, data } wrapper objects
  4. DStore the metadata on a global object keyed by each element's id

Answer and explanation: WeakMap holds its keys weakly, so once an element is removed and no other references exist, both the key and its value can be garbage collected; WeakMaps are not iterable, which matches the requirement. A Map or a Set holds strong references, keeping removed elements in memory until entries are deleted manually. A global object keyed by id strings also retains the data indefinitely and breaks for elements without an id.

Question 2 of 10, Variables, Types, and Collections

A Lightning web component stores a list of selected record Ids and must reject duplicates automatically while preserving insertion order. Lookups such as "is this Id already selected?" must be fast. Which data structure should the developer use?

  1. AA WeakSet, adding Ids with add() so unused entries are garbage collected
  2. BA Set, adding Ids with add() and checking membership with has() Correct answer
  3. CA plain object keyed by Id, iterating with for...in to read the selections
  4. DAn Array, checking each new Id with indexOf() before calling push()

Answer and explanation: A Set stores unique values, keeps insertion order when iterated, and offers has() for constant-time membership checks. An Array with indexOf() works but checks are linear and duplicates are only prevented manually. A WeakSet accepts only objects, not string Ids, and cannot be iterated. A plain object does not guarantee insertion order for integer-like keys and is less direct than a Set for this purpose.

Question 3 of 10, Objects, Functions, and Classes

A developer invokes a function with an explicit this value: function greet(greeting, punctuation) { return ${greeting}, ${this.name}${punctuation}; } const person = { name: 'Rae' }; console.log(greet.call(person, 'Hi', '!'), greet.apply(person, ['Hey', '?'])); What is logged?

  1. AHi, undefined! Hey, undefined?
  2. BHi, Rae! Hey,?, Raeundefined
  3. CHi, Rae! Hey, Rae? Correct answer
  4. DHi, Rae! followed by a thrown TypeError

Answer and explanation: call() takes this followed by individual arguments, and apply() takes this followed by an array of arguments that is spread into the parameters; both set this to person, so name resolves to 'Rae'. undefined for the name would appear only if the function were called without a receiver. Treating the array as a single greeting argument describes call(), not apply(), and apply accepts an array, so no TypeError is thrown.

Question 4 of 10, Objects, Functions, and Classes

A developer is comparing method styles in a Node.js CommonJS script: const counter = { count: 10, regular() { return this.count; }, arrow: () => typeof this }; console.log(counter.regular(), counter.arrow()); What is logged?

  1. A10 object Correct answer
  2. B10 number
  3. C10 undefined
  4. Dundefined object

Answer and explanation: A method defined with shorthand syntax receives the calling object as this, so regular() returns 10. Arrow functions do not bind their own this; they capture it from the enclosing scope, which at the top of a CommonJS module is module.exports, an object. The arrow function therefore cannot see counter, and typeof this is 'object' rather than 'number'; it is not undefined because module scope this is an object.

Question 5 of 10, Browser and Events

A script appends 1,000 rows to a table one at a time with table.appendChild(row) inside a loop, and between insertions it reads table.offsetHeight. The page becomes janky. Which change improves performance the MOST?

  1. AWrap each appendChild call in setTimeout so that every row is inserted in a separate task
  2. BBuild the rows in a DocumentFragment, append it once, and read offsetHeight only afterwards Correct answer
  3. CRead offsetHeight before every insertion instead of after it so that layout stays cached
  4. DReplace appendChild with innerHTML += for each row so that the browser parses all of them faster

Answer and explanation: Interleaving DOM writes with layout reads forces a synchronous reflow on every iteration (layout thrashing). Building rows in a DocumentFragment and inserting them in one operation, then reading layout once, avoids repeated reflows. innerHTML += re-parses the whole table each time, separate timers spread the same expensive work across many tasks, and reading before each write still forces a layout after every modification.

Question 6 of 10, Browser and Events

A list contains hundreds of <li> elements that are added and removed dynamically. The developer needs to handle clicks on any item with minimal memory use and without re-registering listeners when items change. Which approach should be used?

  1. ASet the onclick property of every <li> inside a MutationObserver callback
  2. BAdd a click listener to each <li> when it is created and remove it when it is deleted
  3. CAdd a click listener to each <li> with { capture: true } so new items inherit it
  4. DAdd one click listener to the parent <ul> and use event.target.closest('li') to find the item Correct answer

Answer and explanation: Event delegation relies on click events bubbling from each item to the parent, so a single listener on the <ul> handles current and future items, and closest('li') identifies which item was clicked. Per-item listeners, whether added directly or through a MutationObserver, need constant bookkeeping and use more memory. The capture option only changes the phase in which a listener runs; it does not make listeners apply to newly created elements.

Question 7 of 10, Debugging and Error Handling. Choose 2.

A developer must step through a Lightning web component's client-side JavaScript in a Salesforce org with readable framework code. Which two actions help? (Choose two.)

  1. AEnable debug mode for the user in Setup so that framework code is not minified Correct answer
  2. BUse the Sources panel in browser DevTools to set breakpoints in the component's JavaScript Correct answer
  3. CDeploy the component with the API version set to debug
  4. DAdd System.debug() statements to the component's JavaScript file
  5. EOpen the Developer Console log for the component's client-side code

Answer and explanation: Debug mode makes Lightning serve unminified framework code with more readable output, and browser DevTools breakpoints let the developer step through the component's JavaScript. System.debug is an Apex method and does not exist in JavaScript, and Developer Console debug logs capture server-side Apex execution rather than browser code. API version numbers do not provide a debug setting.

Question 8 of 10, Asynchronous Programming. Choose 2.

A dashboard must check a job status every five seconds until it completes, without blocking the UI. Which two approaches are valid? (Choose two.)

  1. ArequestIdleCallback(poll, 5000) to repeat the poll every five seconds
  2. BA recursive setTimeout that schedules the next poll after each one finishes Correct answer
  3. Cconst id = setInterval(poll, 5000); clearInterval(id) when done Correct answer
  4. DPromise.all([poll(), 5000]) to repeat the poll after five seconds
  5. Ewhile (true) { poll(); } with a 5-second sleep inside the loop

Answer and explanation: setInterval repeats the callback every five seconds until clearInterval is called, and a recursive setTimeout schedules each poll after the previous one, which avoids overlapping requests. JavaScript has no blocking sleep, and an infinite loop would freeze the page. requestIdleCallback runs once when the browser is idle, its option is a timeout rather than a repeat interval, and Promise.all does not create any delay or repetition.

Question 9 of 10, Server Side JavaScript

A team clones a Node.js project in a CI pipeline and needs a clean install that uses exactly the versions recorded in package-lock.json, failing if the lock file and package.json disagree. Which command should the pipeline run?

  1. Anpm audit fix
  2. Bnpm update
  3. Cnpm ci Correct answer
  4. Dnpm install

Answer and explanation: npm ci removes any existing node_modules, installs exactly what package-lock.json specifies, and errors out if the lock file is out of sync with package.json, which makes builds reproducible. npm install may update the lock file, npm update deliberately moves to newer allowed versions, and npm audit fix changes versions to patch vulnerabilities.

Question 10 of 10, Testing

A Jest test for a Lightning web component changes a public property on the element and then immediately queries the shadow DOM for the updated text, but the assertion fails. What should the developer do?

  1. ASet the property with setAttribute() instead of assigning it in JavaScript
  2. BWait for a resolved promise so the re-render finishes, then assert Correct answer
  3. CQuery document.body.innerHTML instead of the component's shadowRoot property
  4. DCall element.render() on the component before running the assertion again

Answer and explanation: LWC re-renders asynchronously after a property change, so the test must wait a microtask, typically by returning a resolved promise and asserting in then(). Components do not expose a render() method to call directly. The text lives inside the component's shadow root, so querying document.body.innerHTML does not reach it, and setAttribute does not avoid the asynchronous re-render.