PD1 Apex Programming 3 — Questions and Answers
Question 1: Which interface must a class implement to be used as a batch Apex job?
- Schedulable
- Queueable
- Database.Batchable (Correct answer)
- Database.Stateful
Correct answer: Database.Batchable
The Database.Batchable interface with its start(), execute(), and finish() methods is required for batch Apex.
Question 2: What is the purpose of `Database.Stateful` in batch Apex?
- Allows the batch job to be scheduled
- Preserves instance variable values across execute() calls (Correct answer)
- Enables the job to call future methods
- Increases the batch size limit to 2,000
Correct answer: Preserves instance variable values across execute() calls
Implementing Database.Stateful retains instance variable state between execute() calls, which is reset otherwise.
Question 3: Which SOSL clause specifies the objects and fields to search within?
- FIND
- IN
- RETURNING (Correct answer)
- WITH
Correct answer: RETURNING
The RETURNING clause in SOSL specifies which sObjects and fields to include in the search results.
Question 4: What does the `@TestSetup` annotation do in an Apex test class?
- Marks a method to run after every test method
- Creates test data once and rolls it back after each test method (Correct answer)
- Prevents test methods from accessing org data
- Allows the test to call external web services
Correct answer: Creates test data once and rolls it back after each test method
@TestSetup creates shared test data once for the class; each test method gets a rollback to that state after it runs.
Question 5: In Apex, which statement correctly describes a static method?
- It can only be called on an instance of the class
- It can access instance variables directly
- It belongs to the class itself and can be called without instantiating the class (Correct answer)
- It must be declared inside an inner class
Correct answer: It belongs to the class itself and can be called without instantiating the class
Static methods belong to the class rather than any instance and are invoked using the class name.
Question 6: Which governor limit applies specifically to Queueable Apex chaining in a single transaction?
- You can enqueue up to 50 jobs
- You can chain up to 1 additional Queueable job (Correct answer)
- You can chain up to 5 additional Queueable jobs
- There is no limit on chaining Queueable jobs
Correct answer: You can chain up to 1 additional Queueable job
In a single transaction, a Queueable job can only chain (enqueue) one additional Queueable job.
Question 7: Which Apex data type is used to store a reference to a field on an sObject dynamically?
- String
- Schema.SObjectField (Correct answer)
- Schema.SObjectType
- Object
Correct answer: Schema.SObjectField
Schema.SObjectField is a token that represents a field reference and is used in dynamic Apex and field-level security checks.
Which interface must a class implement to be used as a batch Apex job?