A class in OOP acts as a blueprint or template for creating objects. It defines the attributes (data) and methods (functions) that the created objects will have, allowing for object instantiation.
Which of the following cannot be used to instantiate a Abstract Class?
A Constructor
B Inheritance
C Direct instantiation
D Object cloning
An abstract class cannot be instantiated directly. It is meant to be inherited by other classes, which provide the implementation of the abstract methods, making it a blueprint for concrete classes.
What is the purpose of a Destructor in OOP?
A Allocate memory
B Initialize object state
C Handle errors
D Destroy object
A destructor is called when an object is about to be destroyed. It allows for cleanup activities, such as deallocating memory or releasing resources, to avoid memory leaks or resource wastage.
Which access modifier provides the highest level of access in OOP?
A Public
B Default
C Protected
D Private
The public access modifier allows members of a class (attributes or methods) to be accessible from any other class or package. This provides the highest level of accessibility compared to other access modifiers.
What is the default access modifier for a class member in Java if none is specified?
A Private
B Public
C Protected
D Package-private
In Java, if no access modifier is specified, the default access level is package-private. This means the class member can only be accessed by other classes within the same package, but not outside it.
What does a Constructor do when an object is created in OOP?
A Defines class properties
B Initializes object
C Executes first
D Returns object reference
A constructor is a special method automatically invoked when an object is created. Its purpose is to initialize the object’s properties (attributes) and set up the initial state of the object.
What is the main advantage of Method Overloading in OOP?
A Reduces errors
B Increases performance
C Increases flexibility
D Simplifies inheritance
Method overloading allows the same method to be defined multiple times with different parameters. This provides flexibility by enabling a method to perform different tasks depending on the input, improving code readability and reuse.
Which of the following is a characteristic of Object-Oriented Programming?
A Focuses on objects
B Does not allow classes
C Uses only functions
D Functions as data
Object-Oriented Programming focuses on organizing code into objects, which represent both data and functions that manipulate that data. This approach helps in modeling real-world entities and making the code more modular.
Which of the following is NOT an example of an Access Modifier?
A Default
B Public
C Private
D Static
Static is not an access modifier; it is a keyword used to indicate that a member (method or variable) belongs to the class itself rather than an instance of the class. Access modifiers control visibility, while static handles class-level members.
What is the key difference between Static Binding and Dynamic Binding in OOP?
A Object vs class
B Memory management vs coding style
C Compile-time vs runtime
D Method overloading vs overriding
Static binding (or early binding) occurs during compile-time, where method calls are resolved based on the method signature. Dynamic binding (or late binding) occurs at runtime, where method calls depend on the actual object type.
Which of the following allows a class to inherit only from one parent class in OOP?
A Polymorphism
B Multiple inheritance
C Single inheritance
D Interface
Single inheritance allows a class to inherit from only one parent class. This simplifies the hierarchy but may limit flexibility. Multiple inheritance, where a class can inherit from more than one parent, is not allowed in many OOP languages.
Which OOP principle is best described by “a class should have only one reason to change”?
A Single Responsibility
B Dependency Inversion
C Abstraction
D Encapsulation
The Single Responsibility Principle (SRP) states that a class should have only one reason to change, meaning it should have one responsibility or function. This leads to more maintainable and modular code, improving system flexibility.
What is an example of a method that is often used in OOP for error handling?
A Main method
B Catch method
C Constructor method
D Run method
The catch method is used in exception handling to catch exceptions thrown during runtime. It allows for graceful handling of errors, ensuring the program doesn’t crash and the error can be managed properly.
Which design pattern ensures that a class has only one instance?
A Observer
B Strategy
C Singleton
D Factory
The Singleton design pattern ensures that a class has only one instance and provides a global point of access to it. This is useful for managing shared resources, such as database connections or logging systems.
Which concept in OOP refers to objects interacting with each other through method calls?
A Message Passing
B Polymorphism
C Inheritance
D Encapsulation
Message passing refers to the process by which objects interact with each other by sending messages (method calls). It is a core concept of OOP, allowing objects to communicate and perform actions within a system.