Skip to content

Unit-2 Questions

III. Packages and Interfaces

B. Access Protection, Importing Packages

  1. What is a package in Java? Explain their purpose (e.g., namespace management, access control) and how they are defined.

  2. What are access specifiers? List the four types (public, private, protected, and default) and explain what level of visibility each one provides.

  3. Create a package named shape. Inside this package, create classes representing common shapes like Square, Triangle, and Circle. Write another program that imports and uses these classes from the shape package.

  4. 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 this Balance class.

  5. 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 the Circle class and uses it.

C. Interfaces

  1. What is an interface in Java? Discuss its significance or use.

  2. Briefly explain how to define an interface and how a class can implement an interface.

  3. How does Java support the implementation of multiple interfaces by a single class? Provide an example.

  4. Differentiate between classes and interfaces, highlighting their key differences, including syntax.

  5. Differentiate between extends (for class inheritance) and implements (for interface implementation) with respect to inheritance concepts. Illustrate with a suitable example.

  6. What are default interface methods?

D. Programming with Interfaces

  1. Write a Java program to implement basic stack operations (e.g., push, pop, peek) using an interface.

  2. Write a Java program to implement basic queue operations (e.g., enqueue, dequeue, peek) using an interface.

  3. Create an interface named Searchable with a method search(int[] arr, int key) that searches for an element in an array of integers. Create two classes, LinearSearch and BinarySearch, that implement the Searchable interface and provide their own implementations of the search() method.

  4. 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, and Square, each providing a concrete implementation to compute its area.

IV. Exception Handling

Part A: Fundamentals of Exception Handling

  1. What is an exception? What is the purpose of exception handling?

  2. Write and explain the general form of an exception-handling block (using try, catch, finally).

  3. Explain the five keywords used in Java exception handling (try, catch, finally, throw, throws). Illustrate their roles with a single, comprehensive code example.

  4. Explain how to handle multiple exception types using multiple catch blocks. Also, demonstrate the functionality of a nested try block.

Part B: Exception Types and Custom Exceptions

  1. What is the difference between checked and unchecked (runtime) exceptions? Provide examples of common built-in exceptions like ArithmeticException, ArrayIndexOutOfBoundsException, NullPointerException, FileNotFoundException and IOException.

  2. Explain how and why you would create a custom (user-defined) exception class in Java. Illustrate with an example.

Part C: Practical Application

  1. 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.

  2. 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).

  3. Create custom exceptions named InvalidAgeException and ArrayOutOfBoundException. 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.

Made with ❤️ for students, by a fellow learner.