A) Encapsulation
B) Inheritance
C) Polymorphism
D) Compilation
Answer: D) Compilation
2. What is the process of hiding the internal implementation details and showing only the functionality is called?
A) Inheritance
B) Abstraction
C) Polymorphism
D) Encapsulation
Answer: B) Abstraction
A) The ability of different classes to be treated as instances of the same class
B) Combining data and functions that manipulate the data into a single unit
C) Deriving new classes from existing ones
D) Allowing multiple forms through a single interface
Answer: B) Combining data and functions that manipulate the data into a single unit
A) implements
B) extends
C) inherits
D) derives
Answer: B) extends
A) The ability of a class to have multiple constructors
B) The ability of different classes to respond to the same function call in different ways
C) The mechanism of hiding data
D) The process of creating a new class from an existing class
Answer: B) The ability of different classes to respond to the same function call in different ways
A) It is an instance of an object
B) It cannot contain methods
C) It defines the properties and behaviors of objects
D) It is always abstract
Answer: C) It defines the properties and behaviors of objects
A) A special method used to destroy objects
B) A method that performs a specific function
C) A special method used to initialize objects
D) A method that cannot be overridden
Answer: C) A special method used to initialize objects
A) Public
B) Private
C) Protected
D) Default
Answer: B) Private
class A {
A() {
System.out.println("A");
}
}
class B extends A {
B() {
System.out.println("B");
}
}
public class Test {
public static void main(String[] args) {
new B();
}
}A) A
B) B
C) A B
D) B A
Answer: C) A B
A) Two methods with the same name and same parameters
B) Two methods with the same name but different parameters
C) Two methods with different names and same parameters
D) Two methods with different names and different parameters
Answer: B) Two methods with the same name but different parameters
A) A class that cannot have any subclasses
B) A class that can have abstract methods
C) A class that cannot have any methods
D) A class that can be instantiated directly
Answer: B) A class that can have abstract methods
A) The process by which one class takes on the attributes and methods of another
B) The ability to define multiple methods with the same name
C) The technique of restricting access to certain details
D) The creation of a new class that is a modified version of an existing class
Answer: A) The process by which one class takes on the attributes and methods of another
A) Public
B) Private
C) Protected
D) Internal
Answer: B) Private
A) Encapsulation
B) Recursion
C) Inheritance
D) Polymorphism
Answer: B) Recursion
A) A class that can be instantiated
B) A blueprint of a class that contains static methods
C) A reference type that can contain only abstract methods and constants
D) A class that implements all methods of its superclass
Answer: C) A reference type that can contain only abstract methods and constants
A) Java supports multiple inheritance using classes
B) Java does not support multiple inheritance
C) Java supports multiple inheritance using interfaces
D) Both B and C
Answer: D) Both B and C
A) To refer to the current class object
B) To refer to the parent class object
C) To create a new object
D) To terminate the current method
Answer: B) To refer to the parent class object
A) ClassName obj = new ClassName();
B) ClassName obj();
C) ClassName obj;
D) object obj = ClassName();
Answer: C) ClassName obj;
A) The superclass object
B) The current object
C) A static method
D) An interface
Answer: B) The current object
A) Default constructor
B) Parameterized constructor
C) Copy constructor
D) Virtual constructor
Answer: D) Virtual constructor
A) To initialize object properties
B) To release resources before the object is destroyed
C) To create a new object
D) To copy an object
Answer: B) To release resources before the object is destroyed
A) Polymorphism
B) Composition
C) Encapsulation
D) Abstraction
Answer: B) Composition
23. What is the term for functions that have the same name but different implementations in different classes?
A) Overloading
B) Overriding
C) Hiding
D) Shadowing
Answer: B) Overriding
A) When you want to define a contract without any implementation
B) When you want to provide some common base functionality
C) When multiple inheritance is required
D) When you have only static methods
Answer: B) When you want to provide some common base functionality
A) Has-a
B) Uses-a
C) Is-a
D) Knows-a
Answer: C) Is-a
A) A function with a body that must be overridden
B) A function without a body that must be overridden
C) A function that cannot be overridden
D) A function that is static
Answer: B) A function without a body that must be overridden
#include <iostream>
using namespace std;
class Base {
public:
virtual void show() { cout << "Base"; }
};
class Derived : public Base {
public:
void show() override { cout << "Derived"; }
};
int main() {
Base *b = new Derived();
b->show();
return 0;
}A) Base
B) Derived
C) Compilation error
D) Runtime error
Answer: B) Derived
28. Which design principle promotes the idea that classes should be open for extension but closed for modification?
A) Single Responsibility Principle
B) Open/Closed Principle
C) Liskov Substitution Principle
D) Interface Segregation Principle
Answer: B) Open/Closed Principle
A) Objects of a superclass should be replaceable with objects of a subclass without affecting the correctness
B) A class should have only one reason to change
C) High-level modules should not depend on low-level modules
D) Interfaces should be specific to client needs
Answer: A) Objects of a superclass should be replaceable with objects of a subclass without affecting the correctness
A) Single Responsibility
B) Open/Closed
C) Liskov Substitution
D) Data Hiding
Answer: D) Data Hiding
A) A circle
B) A rectangle divided into three sections
C) A diamond
D) A triangle
Answer: B) A rectangle divided into three sections
A) To modify the value of a private variable
B) To retrieve the value of a private variable
C) To delete a private variable
D) To initialize a private variable
Answer: B) To retrieve the value of a private variable
A) A "has-a" relationship between two classes
B) A "is-a" relationship between two classes
C) A relationship where one class inherits from another
D) A relationship where one class contains only static members
Answer: A) A "has-a" relationship between two classes
A) Defining multiple methods with the same name in the same class
B) Changing the method signature in a subclass
C) Providing a specific implementation of a method already defined in its superclass
D) Hiding a method in the superclass
Answer: C) Providing a specific implementation of a method already defined in its superclass
A) A class can implement multiple interfaces
B) An interface can contain constructors
C) Interfaces can have instance variables
D) Interfaces can extend classes
Answer: A) A class can implement multiple interfaces
A) A class deriving from another class
B) A class containing objects of other classes
C) A class implementing an interface
D) A class hiding its data members
Answer: B) A class containing objects of other classes
A) sealed
B) static
C) abstract
D) final
Answer: A) sealed
A) Abstract classes cannot have implemented methods, interfaces can
B) Interfaces cannot have any methods, abstract classes can
C) Abstract classes can have implemented methods, interfaces cannot (prior to Java 8)
D) There is no difference
Answer: C) Abstract classes can have implemented methods, interfaces cannot (prior to Java 8)
A) A class that can be instantiated multiple times
B) A class that restricts the instantiation to one single object
C) A class that implements multiple interfaces
D) A class that inherits from multiple classes
Answer: B) A class that restricts the instantiation to one single object
A) A technique where a class creates its dependencies
B) A technique where dependencies are provided to a class from the outside
C) A way to hide dependencies within a class
D) A method of inheriting dependencies from a superclass
Answer: B) A technique where dependencies are provided to a class from the outside
In Object-Oriented Programming (OOP), association and composition are two fundamental relationships between objects. Let's break them down with explanations and Java examples.
- Definition: Association represents a "uses-a" or "has-a" relationship between two or more objects.
- Characteristics:
- It is a general relationship where objects can exist independently.
- There are two types of association:
- Unidirectional: One object can access the other.
- Bidirectional: Both objects can access each other.
class Student {
private String name;
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
class Course {
private String courseName;
public Course(String courseName) {
this.courseName = courseName;
}
public String getCourseName() {
return courseName;
}
}
public class AssociationExample {
public static void main(String[] args) {
Student student = new Student("Alice");
Course course = new Course("Mathematics");
System.out.println(student.getName() + " is enrolled in " + course.getCourseName());
}
}Here, Student and Course are associated. They exist independently and are connected through the enrolled concept.
- Definition: Composition is a "part-of" relationship where one object is a part of another object.
- Characteristics:
- Strong form of association.
- The lifecycle of the "part" is dependent on the "whole."
- If the "whole" object is destroyed, the "part" objects are also destroyed.
class Engine {
private String type;
public Engine(String type) {
this.type = type;
}
public String getType() {
return type;
}
}
class Car {
private String brand;
private Engine engine;
public Car(String brand, String engineType) {
this.brand = brand;
this.engine = new Engine(engineType); // Composition
}
public void showDetails() {
System.out.println("Car: " + brand);
System.out.println("Engine: " + engine.getType());
}
}
public class CompositionExample {
public static void main(String[] args) {
Car car = new Car("Toyota", "V8");
car.showDetails();
// If the car object is destroyed, the engine object will also be destroyed.
}
}In this example, Car contains an Engine. The Engine object does not exist independently; it is part of the Car.
| Aspect | Association | Composition |
|---|---|---|
| Dependency | Objects can exist independently. | Part objects depend on the whole object. |
| Lifetime | Independent of each other. | Lifecycle of part depends on the whole. |
| Type | General relationship (e.g., "uses-a"). | Strong relationship (e.g., "part-of"). |
Both are critical in modeling real-world relationships in OOP and help in creating robust, modular designs.