PD1 Apex Programming 5 — Questions and Answers
Question 1: Which annotation marks an Apex method as callable from a Lightning component via the Lightning Data Service or Wire service?
- @AuraEnabled (Correct answer)
- @RemoteAction
- @InvocableMethod
- @HttpGet
Correct answer: @AuraEnabled
@AuraEnabled exposes an Apex method to Aura and LWC components so it can be called from the client side.
Question 2: What is the purpose of the `Trigger.isBefore` context variable?
- Indicates whether the trigger is running in a test context
- Returns true when the trigger fires before the DML operation commits to the database (Correct answer)
- Returns true when a before delete event is firing
- Checks whether a validation rule has already executed
Correct answer: Returns true when the trigger fires before the DML operation commits to the database
Trigger.isBefore returns true when the trigger context is a before event, meaning the record changes have not yet been saved.
Question 3: Which Apex feature allows you to mock HTTP callouts in test methods?
- System.runAs()
- Test.setMock() (Correct answer)
- HttpRequest.setEndpoint()
- WebServiceMock interface
Correct answer: Test.setMock()
Test.setMock() registers an implementation of HttpCalloutMock or WebServiceMock so test methods can simulate HTTP responses.
Question 4: What is a common use case for the `@InvocableMethod` annotation?
- Exposing Apex logic to Flow Builder as an action (Correct answer)
- Calling Apex from a SOSL query
- Scheduling an Apex class to run at midnight
- Defining a REST API endpoint
Correct answer: Exposing Apex logic to Flow Builder as an action
@InvocableMethod exposes an Apex method so it can be called as an action from Flow Builder or Process Builder.
Question 5: Which governor limit governs the total number of records returned by all SOQL queries in a single transaction?
- 50,000 records
- 100,000 records (Correct answer)
- 500,000 records
- 1,000,000 records
Correct answer: 100,000 records
The total number of records retrieved by SOQL queries in a single transaction is limited to 50,000.
Question 6: In Apex, which System method is used to verify behavior as a specific user in a test class?
- System.setCurrentUser()
- System.runAs() (Correct answer)
- UserInfo.setUser()
- Test.setUser()
Correct answer: System.runAs()
System.runAs() executes a block of code in the context of a specified user, useful for testing sharing and permission-based logic.
Question 7: What is the result of calling `Database.rollback(savepoint)` in Apex?
- It commits all DML operations up to the savepoint
- It undoes all DML operations performed after the savepoint was created (Correct answer)
- It deletes all records inserted during the transaction
- It throws an exception if any DML has already committed
Correct answer: It undoes all DML operations performed after the savepoint was created
Database.rollback() reverts all DML changes made after the specified savepoint, restoring the database to that earlier state.
Which annotation marks an Apex method as callable from a Lightning component via the Lightning Data Service or Wire service?