Skip to Content, PHP, MySQL, Javascript, Ajax, Htacces

Home / Salesforce Interview Questions and Answer / SFDC Developer Interview Question and Answer Part – 2

SFDC Developer Interview Question and Answer Part – 2

(11). Variable and Method Access Modifiers

• Link classes, methods and variables have different levels depending on the keywords used in the declaration.
private: This method/variable is accessible within the class it is defined.
protected: This method/variable is also available to any inner classes or subclasses. It can only be used by instance methods and member variables.
public: This method/variable can be used by any Apex in this application namespace.
global: this method/variable is accessible by all Apex everywhere.
• All methods/variable with the webService keyword must be global.
– The default access modifier for methods and variables is private.

(12). Casting

Apex enables casting: A data type of one class can be assigned to a data type of another class, but only if one class is a child of other class.
• Casting converts an object from one data type to another.
• Casting between the generic sObject type and the specific
sObject type is also allowed.
For Example:
sObject s = new Account();
Account a = (Account)s;
Contact c = (Contact)s //this generates a run time error.

(13). Exceptions Statements

Similar to Java, Apex uses exception to note errors and other events that disrupt script execution with the following keywords:
Throw: signals that an error has occurred and provides an exception object.
Try: identifies the block of code where the exception can occur.
Catch: identifies the block of code that can handle a particular exception. There may be multiple catch blocks for each try block.
Finally: optionally identifies a block of code that is guaranteed to execute after a try block.

Exception Example:
public class OtherException extends BaseException {}
//Add code here
throw new OtherException(‘Something went wrong here…’);
} Catch (OtherException oex) {
//Caught a custom exception type here
} Catch (Exception ex){
//Caught all other exceptions here

(13). Exception Methods

All exceptions support built-in methods for returning the error message and exception type, below is the some of the Exception Methods,

(14). Loops

• Apex supports the following five types of procedural loops:
do {statement} while (Boolean_condition);
while (Boolean_condition) statement;
for (initialization; Boolean_exit_condition; increment) statement;
for (variable : array_or_set) statement;
for (variable : [inline_soql_query]) statement;
• All loops allow for loop control structures:
break; exits the entire loop
continue; skips to the next iteration of the loop


Leave a Reply