Salesforce Certified Platform Developer II Practice Questions

Salesforce Certified Platform Developer II exam preparation

Salesforce Certified Platform Developer II practice questions in English: 180 questions with explanations. The mock exam has 60 questions and 120 minutes.

Salesforce Certified Platform Developer II free question 4 / 10 · Domain 2: Process Automation, Logic, and Integration

A batch Apex class implements Database.Stateful and counts the total amount processed. It adds each amount both to a static variable total and to an instance variable sum. Which statement about the values read in finish is correct?

  1. total is kept across chunks, and sum goes back to its original value for each chunk
  2. Both total and sum keep their values from one chunk to the next
  3. sum is kept across chunks, and total goes back to its original value for each chunk
  4. Both total and sum go back to their original values at the start of each chunk
Show the answer and explanation

Answer: C

Why this is correct
The "Using Batch Apex" page says each execution of a batch Apex job is a discrete transaction, and that if you specify Database.Stateful in the class definition you can maintain state across these transactions. When using Database.Stateful, only instance member variables retain their values between transactions. Static member variables don't retain their values and are reset between transactions. So sum accumulates across chunks while total starts again for each chunk.

Why the other options are wrong
"Both total and sum keep their values from one chunk to the next" … Even with Database.Stateful, static member variables don't retain their values; they are reset between transactions. Only instance member variables are kept.
"total is kept across chunks, and sum goes back to its original value for each chunk" … This is reversed. Instance member variables retain their values with Database.Stateful, and static member variables are reset between transactions.
"Both total and sum go back to their original values at the start of each chunk" … Both variables would be set back to their original values only if Database.Stateful were not specified. With it, instance member variables retain their values.

What the page says it is useful for
Maintaining state is useful for counting or summarizing records as they're processed; the page's example defines a method in execute that aggregates totals of the opportunity amounts as they're processed. If you don't specify Database.Stateful, all static and instance member variables are set back to their original values.

Go to the Salesforce Certified Platform Developer II practice app (10 free questions; purchase unlocks all questions)

See the full practice set