Salesforce Certified Platform Developer Practice Questions

Salesforce Certified Platform Developer exam preparation

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

Salesforce Certified Platform Developer free question 8 / 10 · Domain 3: User Interface

In a Visualforce custom component, apex:attribute with assignTo passes a value to selectedValue. The component controller is as follows, and a value is always passed for the attribute. public class CustCmpCtrl { public CustCmpCtrl() { if (selectedValue != null) { EditMode = true; } } private Boolean EditMode = false; public String selectedValue { get; set; } } Which statement about EditMode matches the Visualforce Developer Guide?

  1. The assignTo value is passed in as a constructor argument, so EditMode becomes true with the value the constructor received
  2. The setter is called before the constructor, so selectedValue already has a value there and EditMode becomes true
  3. The constructor is called before the setter, so selectedValue is still null there and EditMode does not become true
  4. EditMode is private and cannot be changed by the constructor, so it stays false whether or not selectedValue is null
Show the answer and explanation

Answer: C

Why this is correct
"Best Practices for Controllers and Controller Extensions" has a rule titled Controller Constructors Evaluate Before Setter Methods. It tells you not to depend on a setter method being evaluated before a constructor, and gives this example. Because the constructor is called before the setter, selectedValue will always be null when the constructor is called, and EditMode does not become true. Initialization that depends on the attribute value belongs in code that runs after the value is set.

Why the other options are wrong
"The assignTo value is passed in as a constructor argument, so EditMode becomes true with the value the constructor received" … assignTo puts the attribute value into the controller property through the setter. It is not passed as a constructor argument, and the constructor runs before that happens.
"EditMode is private and cannot be changed by the constructor, so it stays false whether or not selectedValue is null" … A private variable can still be changed by the constructor of the same class. The reason EditMode stays false is that the constructor runs before the setter, so selectedValue is null at that point.
"The setter is called before the constructor, so selectedValue already has a value there and EditMode becomes true" … The guide says the opposite: the constructor is called before the setter, so selectedValue is null in the constructor. It tells you not to depend on the setter running first.

More from the same rule
The value of a component attribute reaches the controller property through the setter because of assignTo. Initialization that depends on the attribute value should not be placed in the constructor.

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

See the full practice set