Unit summary

  • A package groups related classes, prevents name conflicts, controls access and maps to a directory.
  • System packages such as java.lang, java.util and java.io provide the Java API; java.lang is imported automatically.
  • User-defined packages are created with a package statement and used with import.
  • An exception is an object describing an abnormal event; Throwable splits into Error and Exception.
  • Checked exceptions must be caught or declared; unchecked ones derive from RuntimeException.
  • try, catch and finally handle exceptions; multiple catch, nested try and finally control the flow.
  • throw raises an exception, throws declares one, and user-defined exceptions extend Exception.

Glossary

Term Meaning
Access modifier A keyword controlling visibility.
catch A block that handles an exception.
Checked exception An exception checked by the compiler.
CLASSPATH The list of places where Java finds classes.
Exception An object describing an abnormal event.
finally A block that always runs.
Fully qualified name A class name with its package.
import A statement that makes classes usable by short name.
Package A named group of related classes.
Stack trace The list of method calls at the time of an exception.
System package A package of the Java standard library.
throw A statement that throws an exception.
Throwable The superclass of all errors and exceptions.
throws A clause declaring exceptions.
try A block containing code that may throw.
Unchecked exception A RuntimeException subclass.
User-defined exception An exception class written by the programmer.

Question bank

Short Answer Questions (2 marks each)

  1. Define a package.
  2. Name four system packages of Java.
  3. How is a user-defined package created?
  4. What is the CLASSPATH?
  5. Define an exception.
  6. Distinguish checked and unchecked exceptions.
  7. What is the use of finally?
  8. Distinguish throw and throws.
  9. What is a nested try?
  10. How is a user-defined exception created?

Medium Answer Questions (5 marks each)

  1. Explain the benefits of packages and the system packages.
  2. Explain how to create and access a user-defined package.
  3. Explain access protection in Java.
  4. Explain the exception hierarchy.
  5. Explain multiple catch statements with an example.
  6. Write a program with a user-defined exception.

Long Answer Questions (15 marks each)

  1. Explain packages: basics, system packages, creating, accessing and adding classes to user-defined packages.
  2. Explain exceptions, their types and the exception hierarchy.
  3. Explain try, catch, finally, multiple catch and nested try, with examples.
  4. Explain throw, throws and user-defined exceptions, with examples.

Sources

  • Official BCA syllabus Object Oriented Programming using Java, course code 25BCA402SEC01, Unit IV