Assignment 2
Assignment 2
1. Define Inheritance in Java. Explain how it is implemented and list the advantages of
using inheritance in OOP.
2. What is the role of the super keyword in inheritance? Provide examples of how
super is used to refer to parent class variables and methods.
3. Explain the concept of a Multilevel Inheritance hierarchy with an example.
4. What happens when constructors are executed in an inheritance chain? Describe
the order in which constructors are invoked in a class hierarchy.
5. What is Method Overriding? Discuss how it is implemented in Java, and explain the
difference between method overloading and overriding.
6. Explain Dynamic Method Dispatch with an example. Why is it important in runtime
polymorphism?
7. Discuss the use of Abstract Classes in Java. How do abstract classes differ from
interfaces, and when should you use one over the other?
8. What is the use of the final keyword in inheritance? Describe how final is used
with classes, methods, and variables in inheritance.
9. Explain Local Variable Type Inference and its impact on inheritance. Provide an
example.
10. What is the Object class in Java? Describe the role it plays in the inheritance
hierarchy, including its commonly used methods like toString(), equals(), and
hashCode().
Interfaces:
1. What is an Interface in Java? Discuss how interfaces are used and give examples of
their application.
2. Explain the concept of Default Interface Methods. How do they help in backward
compatibility of interfaces?
3. Discuss the use of static methods in interfaces. Provide examples of scenarios
where static methods in interfaces would be beneficial.
4. What are Private Methods in interfaces? Explain their role and how they can be used
in the implementation of default methods in interfaces.
5. What is the difference between an abstract class and an interface? Discuss the
cases where an abstract class should be preferred over an interface and vice versa.
6. Write a Java program that demonstrates multiple interfaces being implemented by
a single class. Discuss any challenges or advantages of such an approach.
7. Explain why an interface can extend another interface in Java. Provide an example
to illustrate this.
8. What are the limitations of interfaces in Java, especially before the introduction of
default methods in Java 8?
Module 4
Packages:
1. What is a Package in Java? Explain the purpose and advantages of using packages in
Java.
2. Describe the difference between built-in packages and user-defined packages in
Java. Provide examples.
3. Explain how to create a package in Java. Provide a code example showing the
creation and usage of a simple package.
4. What are access modifiers in the context of packages? Discuss how the default,
private, protected, and public access modifiers work in terms of package-level access.
5. How do you import a package in Java? Describe the syntax for importing a specific
class and the entire package.
6. Differentiate between import package.*; and import package.classname;
Explain when to use each.
7. What is the significance of the import statement? How does it help in organizing
and managing large Java programs?
8. Explain the concept of "Package Naming Conventions" in Java. Why is it important
to follow these conventions?
9. Can two different packages contain classes with the same name? Discuss with
examples.
Exceptions:
1. What is Exception Handling in Java? Discuss why it is important for building robust
applications.
2. Explain the difference between checked and unchecked exceptions in Java.
Provide examples of both.
3. What are Uncaught Exceptions? Explain how Java handles exceptions that are not
caught during execution.
4. Write a Java program demonstrating the use of try and catch blocks. Explain how
exceptions are handled in the program.
5. Explain the concept of Multiple Catch Clauses. Provide an example where multiple
exceptions are handled in a single try-catch block.
6. What is a Nested try Statement in Java? Explain with an example how you can use a
try block inside another try block for exception handling.
7. Describe the use of the throw keyword in Java. Provide an example where a custom
exception is thrown.
8. Explain the throws keyword in Java. How is it used in method signatures, and what is
its role in exception propagation?
9. What is the role of the finally block in exception handling? Explain with an
example when the finally block is executed.
10. What are Java’s built-in exceptions? Provide a list of common built-in exceptions and
explain when they might occur.
11. How can you create your own exception subclasses in Java? Write a program to
define a custom exception and demonstrate its usage.
12. What are Chained Exceptions in Java? Explain how exceptions can be linked
together, and give an example of how chaining works.
13. What is the difference between Exception and Error in Java? Provide examples
where each would occur.
14. Write a Java program that handles both a specific exception (e.g.,
FileNotFoundException) and a general exception (e.g., IOException) using a
multiple catch clause.
Module 5