Section 3: Scenario-Based

Answer in your own words.

21. Explain when to use a struct instead of a class.

chevron-rightAnswershashtag
  • The data is small and mutable.

  • You want value-type behaviour (no reference sharing).

  • Performance optimisation is needed for stack allocation.


22. Why is readonly useful in Unity scripts?

chevron-rightAnswershashtag
  • It ensures variables can only be set during initialisation.

  • Prevents accidental modification after initialisation.


23. How does static affect methods?

chevron-rightAnswershashtag
  • Belong to the class rather tahn an instance.

  • Can be accessed without created an object.

Example:

public static void PrintMessage() {
    Console.WriteLine("Hello");
}

24. Why are events useful in Unity?

chevron-rightAnswershashtag
  • They allow scripts to react dynamically to changes.

  • Enable decoupling of code (e.g., handling game state changes).


25. Explain inheritance in Unity.

chevron-rightAnswershashtag
  • Reducing duplication.

  • Allowing shared logic across multiple game objects.


26. What are abstract classes, and when should you use them?

chevron-rightAnswershashtag
  • Use them when you need a base class that should not be instantiated.

  • Certain methods must be implemented by derived classes.


27. When should you use a Dictionary instead of a List?

chevron-rightAnswershashtag
  • When you need fast lookups based on a unique key.


28. Explain polymorphism and its benefits.

chevron-rightAnswershashtag
  • Polymorphism allows different objects to share the same inheritance but have unique behaviour.


29. What is dependency injection, and why is it useful?

chevron-rightAnswershashtag
  • It allows for better unit testing.

  • Decouples objects from dependencies.


30. What is a singleton pattern, and when should you use it?

chevron-rightAnswershashtag
  • A singleton ensures only one instance of a class exists at runtime.

  • Commonly used for GameManager in Unity.

Last updated