PD1 Lightning Components 4 — Questions and Answers
Question 1: Which LWC feature allows a component to receive data from a Lightning Data Service wire adapter without writing Apex?
- @api with getRecord
- @wire with getRecord from 'lightning/uiRecordApi' (Correct answer)
- @track with RecordForm
- force:recordData in the template
Correct answer: @wire with getRecord from 'lightning/uiRecordApi'
The getRecord wire adapter from lightning/uiRecordApi retrieves record data using Lightning Data Service, bypassing the need for custom Apex.
Question 2: What is the correct LWC syntax to iterate over a list of items in a template?
- <template for:item={items}>
- <template for:each={items} for:item='item'> (Correct answer)
- <aura:iteration items={items} var='item'>
- <template repeat={items} as='item'>
Correct answer: <template for:each={items} for:item='item'>
LWC requires both for:each to specify the array and for:item to name the iteration variable in the template directive.
Question 3: In LWC, which key directive is REQUIRED when using for:each iteration to help the framework efficiently update the DOM?
- key={item.Id} (Correct answer)
- data-id={item.Id}
- id={item.Id}
- index:key={index}
Correct answer: key={item.Id}
The key directive with a unique value (like key={item.Id}) is required on iterated elements so LWC can efficiently reconcile the virtual DOM.
Question 4: Which approach correctly prevents an LWC component from accessing the parent component's DOM elements?
- LWC automatically enforces shadow DOM isolation between components (Correct answer)
- You must manually set shadowRoot.mode = 'closed'
- Use the @isolate decorator on component properties
- Set isolated: true in the component's meta XML file
Correct answer: LWC automatically enforces shadow DOM isolation between components
LWC uses native shadow DOM (or synthetic shadow DOM on older browsers) by default, automatically encapsulating each component's DOM from others.
Question 5: In the Aura framework, what is the purpose of `<aura:handler name='init'>` with action='{!c.doInit}'?
- It handles click events on the component
- It calls the doInit controller method when the component initializes (Correct answer)
- It registers an application event handler
- It subscribes the component to the platform event 'init'
Correct answer: It calls the doInit controller method when the component initializes
The init event handler in Aura triggers the specified controller action during the component initialization phase, similar to LWC's connectedCallback.
Question 6: Which LWC module provides the NavigationMixin used for programmatic navigation?
- lightning/navigation (Correct answer)
- force/navigation
- c/navigation
- lightning/pageReferenceUtils
Correct answer: lightning/navigation
NavigationMixin is imported from the 'lightning/navigation' module and mixed into a component class to enable programmatic page navigation.
Question 7: When a developer marks an LWC method with @api, what capability does this give the parent component?
- The parent can read the child's private properties
- The parent can call the method directly on the child component (Correct answer)
- The method is automatically wired to a Salesforce data source
- The method becomes accessible via Aura application events
Correct answer: The parent can call the method directly on the child component
@api on a method exposes it as part of the component's public interface, allowing parent components to call it imperatively via a template reference.
Which LWC feature allows a component to receive data from a Lightning Data Service wire adapter without writing Apex?