Explore advanced object-oriented programming concepts with this quiz focusing on reflection and metaprogramming techniques. Assess your understanding of runtime type inspection, dynamic method handling, and manipulating classes and objects programmatically.
Which OOP concept allows a program to examine and modify its own structure or behavior at runtime, such as determining the methods of an object?
Explanation: Reflection lets programs inspect and modify their own structure or behavior at runtime, such as listing available methods on an object. Serialization deals with converting objects to a storable format, which does not involve type inspection. Encapsulation protects object data but does not enable runtime inspection. Abstraction focuses on hiding complexity rather than examining object structure. Only reflection directly relates to runtime introspection.
In metaprogramming, what is the practice called when code determines and calls a method on an object based on a string value received at runtime?
Explanation: Dynamic method invocation involves determining and invoking a method at runtime using mechanisms like strings or function pointers. Static binding fixes the method at compile time and lacks runtime flexibility. Polymorphic dispatch chooses methods based on an object’s actual class, not via runtime-determined names. Inheritance mapping is not a relevant term in this context. Only dynamic method invocation fits the scenario described.
If you want to programmatically list all fields (attributes) of a class instance, which advanced OOP feature would you typically use?
Explanation: Reflection is used to inspect the structure of classes and instances at runtime, including listing all fields. Overloading refers to defining multiple methods with the same name but different parameters. Delegation involves passing work to another object, not inspecting attributes. Aggregation is an association between classes and does not relate to listing fields programmatically. Thus, the most accurate answer is reflection.
Which term describes the ability of a program to generate and add new methods or classes during its execution, often found in metaprogramming?
Explanation: Runtime code generation refers to creating new methods or classes dynamically during program execution, a technique central to metaprogramming. Static typing enforces type checks at compile time and does not enable adding code at runtime. Class overriding changes existing behavior but does not generate new code. Typecasting converts between types and is unrelated to code generation. Therefore, runtime code generation is correct.
What is a common risk associated with excessive or improper use of reflection in advanced OOP applications?
Explanation: Improper use of reflection can bypass access controls and expose internal details, increasing security risks. Reflection tends to decrease performance rather than improve it, making the efficiency option incorrect. It can also complicate code, reducing readability. Garbage collection is related to memory management and is unaffected by reflection. The primary concern is increased security vulnerabilities.