PD1 Lightning Components 2 — Questions and Answers
Question 1: Which lifecycle hook in a Lightning Web Component is called every time the component is inserted into the DOM?
- constructor()
- connectedCallback() (Correct answer)
- renderedCallback()
- disconnectedCallback()
Correct answer: connectedCallback()
connectedCallback() fires each time the component is inserted into the DOM, making it ideal for setup logic that depends on the component being in the page.
Question 2: In Aura components, which attribute type allows a component to accept a JavaScript function as a parameter from a parent component?
- String
- Object
- Action (Correct answer)
- Function
Correct answer: Action
The Action attribute type in Aura allows a component to receive a controller action reference, enabling parent-to-child method passing.
Question 3: What is the correct way to call an Apex method imperatively in a Lightning Web Component?
- import apexMethod from '@salesforce/apex'; apexMethod().then(...) (Correct answer)
- Apex.call('ClassName.method', params)
- @wire(apexMethod) wiredResult;
- apex.invoke('ClassName', 'method', params)
Correct answer: import apexMethod from '@salesforce/apex'; apexMethod().then(...)
Imperative Apex calls in LWC use the imported method as a regular JavaScript function that returns a Promise.
Question 4: Which decorator in LWC makes a property reactive so that changes trigger a re-render?
- @api
- @wire
- @track (Correct answer)
- @observable
Correct answer: @track
@track makes an object or array's nested properties reactive in LWC; primitive @api properties are already reactive.
Question 5: In a Lightning Web Component, where must you define a public property that a parent component can set?
- Using @track in the JS file
- Using @api in the JS file (Correct answer)
- In the component's .html template
- In the component's meta XML file
Correct answer: Using @api in the JS file
@api exposes a property or method as part of the component's public API, allowing parent components to pass values in.
Question 6: Which file extension is used for the HTML template of a Lightning Web Component?
- .html (Correct answer)
- .lwc
- .template
- .hbs
Correct answer: .html
LWC HTML templates use the standard .html extension and must contain a single <template> root element.
Question 7: What does the `c` namespace represent in a Lightning component reference like `<c-my-component>`?
- A Salesforce core component
- A custom (org-defined) component (Correct answer)
- A community component
- A component from a managed package
Correct answer: A custom (org-defined) component
The `c` namespace is the default namespace for custom components built within a Salesforce org.
Which lifecycle hook in a Lightning Web Component is called every time the component is inserted into the DOM?