Unit-2 Questions
III. Packages and Interfaces
B. Access Protection, Importing Packages
What is a package in Java? Explain their purpose (e.g., namespace management, access control) and how they are defined.
What are access specifiers? List the four types (
public
,private
,protected
, and default) and explain what level of visibility each one provides.Create a package named
shape
. Inside this package, create classes representing common shapes likeSquare
,Triangle
, andCircle
. Write another program that imports and uses these classes from theshape
package.Demonstrate Packages and Access Control with a Program: Create a
Balance
class (e.g., for a bank account) and organize it into a package. Demonstrate access protection and importing this package in another class. Write a program to show how classes from different packages can interact using thisBalance
class.Create a package named
geometry
. Inside this package, create a public class Circle with a method to calculate its area. Write a separate program in a different package that imports theCircle
class and uses it.
C. Interfaces
What is an interface in Java? Discuss its significance or use.
Briefly explain how to define an interface and how a class can implement an interface.
How does Java support the implementation of multiple interfaces by a single class? Provide an example.
Differentiate between classes and interfaces, highlighting their key differences, including syntax.
Differentiate between
extends
(for class inheritance) andimplements
(for interface implementation) with respect to inheritance concepts. Illustrate with a suitable example.What are default interface methods?
D. Programming with Interfaces
Write a Java program to implement basic stack operations (e.g., push, pop, peek) using an interface.
Write a Java program to implement basic queue operations (e.g., enqueue, dequeue, peek) using an interface.
Create an interface named
Searchable
with a methodsearch(int[] arr, int key)
that searches for an element in an array of integers. Create two classes,LinearSearch
andBinarySearch
, that implement theSearchable
interface and provide their own implementations of thesearch()
method.Demonstrate the usage of an interface by:
- Creating an interface named
Shape
. - Declaring appropriate abstract methods in
Shape
(e.g.,calculateArea()
). You might also consider data members if appropriate for the interface's design (though interfaces primarily define behavior). - Implementing this
Shape
interface in three classes:Circle
,Triangle
, andSquare
, each providing a concrete implementation to compute its area.
- Creating an interface named
IV. Exception Handling
Part A: Fundamentals of Exception Handling
What is an exception? What is the purpose of exception handling?
Write and explain the general form of an exception-handling block (using
try
,catch
,finally
).Explain the five keywords used in Java exception handling (
try
,catch
,finally
,throw
,throws
). Illustrate their roles with a single, comprehensive code example.Explain how to handle multiple exception types using multiple
catch
blocks. Also, demonstrate the functionality of a nestedtry
block.
Part B: Exception Types and Custom Exceptions
What is the difference between checked and unchecked (runtime) exceptions? Provide examples of common built-in exceptions like
ArithmeticException
,ArrayIndexOutOfBoundsException
,NullPointerException
,FileNotFoundException
andIOException
.Explain how and why you would create a custom (user-defined) exception class in Java. Illustrate with an example.
Part C: Practical Application
Write a banking application program that demonstrates comprehensive exception handling. Create a custom InsufficientFundsException. The program should handle deposits and withdrawals. A withdrawal attempt for an amount greater than the balance should throw the InsufficientFundsException. Use a try-catch block to handle the custom exception gracefully. Use a finally block to display the account's final balance after any transaction attempt.
Write a program that takes one integer number as input from the user. The program should throw a custom exception if the input number is greater than 20 (or 10, as per different versions of the question).
Create custom exceptions named
InvalidAgeException
andArrayOutOfBoundException
. Write a Java program that takes command-line arguments. Based on the arguments (e.g., an age value, an array index), the program should handle the appropriate custom exception gracefully.