exam questions

Exam CRT-450 All Questions

View all questions & answers for the CRT-450 exam

Exam CRT-450 topic 1 question 139 discussion

Actual exam question from Salesforce's CRT-450
Question #: 139
Topic #: 1
[All CRT-450 Questions]

A developer has an integer variable called maxAttempts. The developer needs to ensure that once maxAttempts is initialized, it preserves its value for the length of the Apex transaction; while being able to share the variable's state between trigger executions.
How should the developer declare maxAttempts to meet these requirements?

  • A. Declare maxAttempts as a private static variable on a helper class.
  • B. Declare maxAttempts as a variable on a helper class.
  • C. Declare maxAttempts as a member variable on the trigger definition.
  • D. Declare maxAttempts as a constant using the static and final keywords.
Show Suggested Answer Hide Answer
Suggested Answer: D 🗳️

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
Brinuz
10 months, 1 week ago
Selected Answer: D
The answer is D
upvoted 3 times
...
wjj
1 year ago
Selected Answer: A
To meet the given requirements, the developer should declare maxAttempts as a private static variable on a helper class. This approach ensures that the value of maxAttempts is preserved for the length of the Apex transaction, and it also allows the variable's state to be shared between trigger executions. Option (C) is not a good choice, as member variables on trigger definitions are not static and can be reset between trigger executions, which would violate the requirement of preserving the value of maxAttempts for the length of the transaction. Option (D) is not suitable, as a constant variable cannot be modified after initialization, and the requirement is to preserve the value of maxAttempts for the entire transaction. Option (B) is not a good choice either, as declaring maxAttempts on a helper class as a regular variable would not make it static, and its value would not be preserved for the length of the Apex transaction. Therefore, option (A) is the best choice for declaring maxAttempts to meet the given requirements
upvoted 2 times
briccoli11
11 months, 1 week ago
Thank you for your thorough explanation. I'm still a little confused why option D (Declare maxAttempts as a constant using the static and final keywords.) is not the answer, as the question says that "once maxAttempts is initialized, it [should] preserve its value for the length of the transaction." https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_constants.htm ^ This source says that a variable with a final keyword can be assigned "with a static initializer method if the constant is defined in a class" Can you clarify why D would still be incorrect based off of this?
upvoted 1 times
...
...
wjj
1 year ago
To meet the given requirements, the developer should declare maxAttempts as a private static variable on a helper class. This approach ensures that the value of maxAttempts is preserved for the length of the Apex transaction, and it also allows the variable's state to be shared between trigger executions. Option (A) is not a good choice, as member variables on trigger definitions are not static and can be reset between trigger executions, which would violate the requirement of preserving the value of maxAttempts for the length of the transaction. Option (C) is not suitable, as a constant variable cannot be modified after initialization, and the requirement is to preserve the value of maxAttempts for the entire transaction. Option (D) is not a good choice either, as declaring maxAttempts on a helper class as a regular variable would not make it static, and its value would not be preserved for the length of the Apex transaction. Therefore, option (B) is the best choice for declaring maxAttempts to meet the given requirements.
upvoted 1 times
...
semio
1 year, 3 months ago
Selected Answer: D
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_constants.htm
upvoted 4 times
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...