Popular Apex Interview Questions and Answers

What is Apex?

Apex is a strongly typed Object Oriented Programming Language(OOPL) that runs on Force.com. Apex is used to add programmatic business logic to the application. Apex enables a developer to add business logic to most system events.

How does Apex work?

Apex is compiled, stored and run entirely on force.com platform. All Apex runs entirely on-demand on the force.com. When a developer writes and saves apex code, the platform app server first compiles the code into an abstract set of instructions that can be understood by the Apex runtime interpreter and then saves those instructions as metadata.

When end user triggers the execution of Apex, by clicking a button / accessing the Visualforce page, the platform App server retrieves compiled instructions form metadata and sends them through the runtime interpreter before retrieving the results. Enduser observes no difference in execution time from standard platform requests.

When should you use Apex?

Apex is used to create email services, to perform complex validation over multiple objects, to create complex business processes that are not supported by workflows, to create custom transactional logic to attach custom logic to another o such as saving records.

What are the Apex programming elements?

Apex programme contains different elements like Identifiers, keywords, operators, constants, Strings, SOQL queries, varaiable declarations, Control statemetns, Arrays and DML operations.

Different ways to comment in Apex?

  • For single comment: —//
  • For multi line comment: /* your comment */

Do-while loop

</>
Copy
do
{
Statement1;
Statement2;
}
While(Exp1);
Statement3;

While loop

</>
Copy
While(condition1)
{
Statement1;
Statement2;
}
Statement3;

For loop

</>
Copy
for(expresson1,expresson2,expresson3)
{
Statement1;
Statement2;
}
Statement3;

How to see system.debug() results?

System.debug() results can be monitored in debug logs. To create navigate to Setup | Admin setup | Monitor | logs | Debug logs.

Difference between Constructor and Method?

ConstructorMethod
Constructor name will be the class name.Method name need not be the class name
Constructors are called automatically whenever an object is created.Methods need to be called with the help of object.
A Constructor will be executed once on an object.A Method can be called any number of times.
If no constructor in a class, then default constructor with zero params will be provided.If no method in class then no methods will be added.
A constructor can’t be static and will not be inherited.Methods can be static and will be inherited.
A Constructor is used to initialize the state of an object.Methods can be overridden.
A constructor can’t be abstract, final, native, static and synchronized.Methods can be abstract, final, static and synchronized.
No object can be created without a constructor.No methods needed to create an object.

Different types of variables?

  • Local variables: belongs to a method.
  • Object variables: belongs to object (Instance variables).
  • Class variables: belongs to class (Static variables).

Types of methods we can writer in Apex?

  • Constructor.
  • Setter methods.
  • Getter methods.
  • Operations (Actions).

When to make the method as final?

If it’s implementation is perfect and does not require any further modifications then we make that method as final.

When to make the method as abstract?

If we don’t know the implementation of the method now and can implement latter then make the method as abstract.

When to make the method as virtual?

If a method is implemented for a specific purpose and can be modified for any other purpose them make that method as virtual.