What is the purpose of the Singleton design pattern?
A Single instance
B Multiple instances
C Code reusability
D Code optimization
The Singleton pattern ensures that a class has only one instance, which is shared globally throughout the application. It prevents multiple instances and provides a single point of access to the instance.
Which design pattern ensures that objects are created based on certain conditions?
A Singleton Pattern
B Prototype Pattern
C Observer Pattern
D Factory Pattern
The Factory Pattern provides an interface for creating objects, but the exact class of the object is determined at runtime. It allows for the creation of objects based on specific conditions or inputs.
What is the main purpose of the Observer Pattern?
A Object persistence
B Object communication
C Data hiding
D Event handling
The Observer Pattern is used to establish a one-to-many dependency between objects. When the state of an object changes, all dependent objects (observers) are notified and updated automatically, allowing for real-time updates.
What does the Strategy Pattern allow?
A Object serialization
B Object persistence
C Dynamic behavior selection
D Single method overriding
The Strategy Pattern allows algorithms or behaviors to be selected at runtime. It defines a family of algorithms and allows an object to choose which one to use, offering flexibility in how a task is performed.
Which of the following is the key feature of Exception Handling?
A Managing runtime errors
B Preventing method overloading
C Enhancing class inheritance
D Object initialization
Exception handling is used to detect, manage, and recover from runtime errors. It prevents program crashes by using try, catch, and finally blocks to handle exceptions and ensure the program continues running smoothly.
Which of the following is an example of a Checked Exception in Java?
A NullPointerException
B ArithmeticException
C IndexOutOfBoundsException
D IOException
A checked exception is a type of exception that the compiler forces the programmer to handle. IOException is a checked exception because it must be either caught or declared in the method signature.
Which of the following is a Design Pattern used to create objects without specifying the exact class of object that will be created?
A Observer Pattern
B Factory Pattern
C Singleton Pattern
D Prototype Pattern
The Factory Pattern is used to create objects without specifying the exact class of object that will be created. It defines an interface for object creation, allowing for flexibility in choosing the class at runtime.
In the context of design patterns, what is the role of the Prototype Pattern?
A Object cloning
B Event handling
C Method overriding
D Data abstraction
The Prototype Pattern involves creating new objects by copying an existing object (cloning). It helps to avoid the cost of creating an object from scratch by duplicating the original object’s state.
Which design pattern provides a way to ensure that a class has only one instance?
A Factory Pattern
B Prototype Pattern
C Observer Pattern
D Singleton Pattern
The Singleton Pattern ensures that a class has only one instance, and it provides a global point of access to that instance. It is useful in situations where shared resources or configurations are needed.
Which of the following is a benefit of Exception Handling in OOP?
A Object creation
B Runtime error management
C Code readability
D Simplified debugging
Exception handling allows developers to manage runtime errors, ensuring the program does not crash. It improves error detection, reporting, and recovery, making the system more robust and fault-tolerant.
What is the main advantage of the Factory Pattern?
A Implements multiple algorithms
B Provides multiple instances
C Controls object creation
D Reduces code complexity
The Factory Pattern controls the object creation process by abstracting the instantiation logic. It allows flexibility in creating objects of different types, making the system more scalable and easier to modify or extend.
Which of the following statements is true about the Observer Pattern?
A It allows real-time communication
B It is used for object cloning
C It involves method overloading
D It restricts object visibility
The Observer Pattern facilitates real-time communication between an object (subject) and its dependent objects (observers). When the state of the subject changes, all observers are immediately notified and updated, supporting dynamic behavior.
In the context of design patterns, what does the Facade Pattern provide?
A Dynamic behavior selection
B Direct access to an object
C Object initialization
D Simplified interface
The Facade Pattern provides a simplified interface to a complex system. It hides the system’s complexity by offering a unified and easy-to-use interface to the client, making the interaction with the system more efficient.
What is the role of Abstract Factory Pattern in OOP?
A Increases object mutability
B Defines object behaviors
C Creates families of related objects
D Manages object exceptions
The Abstract Factory Pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes. It allows for more flexible and scalable systems where objects must be compatible with each other.
What is the main purpose of Method Overriding in OOP?
A Method duplication
B Handling exceptions
C Modifying inherited behavior
D Increasing performance
Method overriding allows a subclass to provide a specific implementation of a method that it inherits from a superclass. It enables the subclass to modify or extend the behavior of the inherited method to meet its requirements.