OOP questions and answers[1]
OOP questions and answers[1]
○ A: Abstraction is a process of hiding the implementation details and showing only functionality to the user12.
○ A: An abstract class is a class that is declared with the abstract keyword and may have abstract and non-abstract methods5.
○ A: An abstract method is a method that is declared with the abstract keyword and does not have a body6.
○ A: An interface is a blueprint of a class that has static constants and abstract, default, static and private methods7.
○ A: Interfaces can be used to achieve abstraction, multiple inheritance, loose coupling and polymorphism 9.
○ A: Some differences are: abstract classes can have constructors, non-abstract methods and non-static variables, while interfaces
cannot; abstract classes can extend another class and implement multiple interfaces, while interfaces can only extend another
interface; abstract classes can have protected and default access modifiers, while interfaces can only have public access
modifiers.
○ A: Default methods are methods that have a body and are tagged with the default keyword. Static methods are methods that
have a body and are tagged with the static keyword. Both methods can be invoked without creating an object of the interface.
○ A: Encapsulation is a process of wrapping code and data together into a single unit and hiding the data from the outside.
○ A: We can achieve encapsulation by making the data members of a class private and providing public setter and getter methods
to access and modify them1111.
○ A: Encapsulation can provide data security, data validation, control over the data, ease of testing and maintenance, and
modularity of the code.
○ A: A read-only class is a class that has only getter methods and no setter methods. A write-only class is a class that has only
setter methods and no getter methods.
○ A: Multiple inheritance is a feature that allows a class or an interface to inherit from more than one parent class or interface.
○ A: Multiple inheritance can be supported by using interfaces, as a class can implement multiple interfaces or an interface can
extend multiple interfaces12[12]1313.
○ A: A class can extend another class, an interface can extend another interface, but a class can only implement an
interface15[15]1616. A class that implements an interface must provide the implementation for all the abstract methods of the
interface1717.
Exception Handling
• Q: What is an exception?
o A: An exception is an unexpected, unwanted event that disturbs the normal flow of the program 3.
• Q: What is the main objective of exception handling?
o A: The main objective of exception handling is graceful termination of the program 4.
• Q: What are the two types of exceptions in Java?
o A: The two types of exceptions in Java are checked and unchecked exceptions 5.
• Q: What is the difference between checked and unchecked exceptions?
o A: Checked exceptions are those that are checked by the compiler for smooth execution of the program, and must be handled either
by try-catch or throws keyword6. Unchecked exceptions are those that are not checked by the compiler, and are usually caused by
logical errors or runtime conditions7.
• Q: What is the root class of Java exception hierarchy?
o A: The root class of Java exception hierarchy is java.lang.Throwable, which is inherited by two subclasses: Exception and Error8.
• Q: What is the difference between Exception and Error?
o A: Exception is a problem that can be handled by the programmer, and is usually caused by the program logic or input. Error is a
problem that cannot be handled by the programmer, and is usually caused by the lack of system resources or environment issues.
• Q: What is the syntax of try-catch-finally block?9
o A: The syntax of try-catch-finally block is:
try {
// risky code
} catch (Exception e) {
// handling code
} finally {
// cleanup code
}