function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Parikhit SarkarParikhit Sarkar 

Which code block will run successfully in an execute anonymous window

 A developer has the controller class below

Public with sharing class myFooController {
                public integer prop {get; private set;}
}

Which code block will run successfully in an execute anonymous window?

a. myFooController m = new myFooController();
System.assert(m.prop != null);
b. myFooController m = new myFooController();
System.assert(m.prop == 1);
c. myFooController m = new myFooController();
System.assert(m.prop == null);
d. myFooController m = new myFooController();
System.assert(m.prop == 0);

Can someone please help me solve this question? I found out that the correct answer is option (C) but I don't understand the reason behind it.
Best Answer chosen by Parikhit Sarkar
Parikhit SarkarParikhit Sarkar
Thanks for your responses but I have figured out myself that the correct answer will be (C).

The reason is simply because the prop variable is never defined in the constructor of the controller and hence its value is null. Therefore, executing below code:
myFooController m = new myFooController();
System.assert(m.prop == null);


in the anonymous window will always evaluate to true and thus it will execute.

All Answers

Stephan braunnStephan braunn
Hi you can check details: Which code block will run successfully in an execute anonymous window (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_anonymous_block.htm)


Best Regards: Students Help (https://www.essaycorp.co.uk/nursing-essay-help/)
Andrew GAndrew G

From my understanding ,
the Get method will pass data from the controller to the page.  Since the Get does not return anything, it will simply return a variable of type Integer but with no value set, hence it will be null

now, lets got out on a limb with my understanding of getters/setters
if we did

Public with sharing class myFooController {
      public integer prop ;
      public integer getprop() {return 1;}
      public integer setprop() 
      { 
      //do nothing
      }
}
In the above controller, option b from amongst the answers would be correct.

myFooController m = new myFooController();
System.assert(m.prop == 1);


HTH
Andrew

Parikhit SarkarParikhit Sarkar
Thanks for your responses but I have figured out myself that the correct answer will be (C).

The reason is simply because the prop variable is never defined in the constructor of the controller and hence its value is null. Therefore, executing below code:
myFooController m = new myFooController();
System.assert(m.prop == null);


in the anonymous window will always evaluate to true and thus it will execute.
This was selected as the best answer