Courseiva

SF-PD2 Advanced Developer Fundamentals Practice Question

A developer needs to dynamically instantiate a class at runtime based on a string value. Which feature should they use?

⚠ Common exam trap

Test-takers frequently confuse instance methods with the static Type class methods, attempting to instantiate classes using incorrect syntax.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

Type.forName()

The Type class in Apex provides the ability to perform dynamic instantiation using the forName method. This is a common pattern in advanced development, particularly for implementing factory patterns or plugin architectures where the specific implementation class is determined by configuration or user input rather than being hardcoded, thus promoting loose coupling and better modularity.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    System.JSON.deserialize()

    Why it's wrong here

    JSON.deserialize is designed for parsing JSON strings into Apex objects or primitive types. It cannot instantiate a class from a string unless that string represents the serialized state of an existing object. It does not provide a mechanism to invoke a class constructor dynamically via a string name.

  • ✓

    Type.forName()

    Why this is correct

    Type.forName() accepts a string representing the fully qualified name of an Apex class and returns a Type object. This Type object can then be used to call the newInstance() method, allowing for dynamic instantiation of the class at runtime, which is essential for flexible and extensible application design.

  • ✗

    String.valueOf()

    Why it's wrong here

    String.valueOf is a method used to convert an object into its string representation. It has no functional role in dynamic class instantiation or factory patterns. Using this method on a class or type will merely return the name of the object as a string, not the object itself.

  • ✗

    Class.getInstance()

    Why it's wrong here

    There is no native Class.getInstance() method in Apex. The pattern for dynamic creation is strictly handled through the Type class. Invoking non-existent methods will cause a compilation error. Developers must use the documented Type.forName and Type.newInstance methods to achieve the desired functionality for dynamic object creation.

About these practice questions

Courseiva writes every SF-PD2 question from scratch — 226 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official Salesforce exam blueprint

This SF-PD2 practice question is part of Courseiva's free Salesforce certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the SF-PD2 exam.