Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
2 views5 pages

Unit 3

The document provides an overview of Java packages, interfaces, and exception handling, detailing their definitions, usage, and differences. It explains how to import packages, the types of exceptions (checked and unchecked), and the importance of exception handling in writing robust programs. Additionally, it covers keywords related to exceptions, such as try, catch, throw, and finally, along with examples and best practices for exception management.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Unit 3

The document provides an overview of Java packages, interfaces, and exception handling, detailing their definitions, usage, and differences. It explains how to import packages, the types of exceptions (checked and unchecked), and the importance of exception handling in writing robust programs. Additionally, it covers keywords related to exceptions, such as try, catch, throw, and finally, along with examples and best practices for exception management.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

UNIT III - DATA ABSTRACTION

PART - A

1. What is a package?

Packages contain a set of classes in order to ensure that class names are unique. Packages are
containers for classes that are used to compartmentalize the class name space. Packages are
stored in a hierarchical manner and are explicitly imported into new class definition. A period
is used as separator to enable this.

2. Write a note on import statement?

Classes external to a program be imported before they can be used. To import a class the
import keyword should be used as given below import <classname>

The classes in Java are arranged in hierarchical order. The Java library consists of a number
of packages. These packages contain a set of related classes. The whole path of the class must
be specified to import a class from the Java library, For instance, to import the Data class
from the util package use the following code.

import java.util.Date;

It is also possible to import all classes that belong to a package using the * symbol.

3. Explain the usage of Java packages

This is a way to organize files when a project consists of multiple modules. It also helps
resolve naming conflicts when different packages have classes with the same names.
Packages access level also allows you to protect data from being used by the nonauthorized
classes.

4. Define interface?

An interface is a collection of abstract behavior that individual classes can implement. It is


defined like a class. An interface consists of a set of method definition. Any class
implementing it should provide code for all its methods.

5 What is the useful of Interfaces?

 Declaring methods that one or more classes are expected to implement


 Capturing similarities between unrelated classes without forcing a class relationship.
 Determining an object’s programming interface without revealing the actual body of
the class.

6. Define an exception.

An exception is an abnormal condition, which occurs during the execution of a program


Exceptions are erroneous events like division by zero, opening of a file that does not exist,
etc. A java execution is an object, which describes the error condition that has materialized in
the program.

7. How to access package from another package?

There are three ways to access the package from outside the package.

1. import package.*;
2. import package.classname;
3. fully qualified name.

8. What are the types of Exception in Java.

There are mainly two types of exceptions: checked and unchecked. Here, an error is
considered as the unchecked exception.

1. Checked Exception
2. Unchecked Exception.

9. Difference between Checked and Unchecked Exceptions.

1) Checked Exception

The classes which directly inherit Throwable class except RuntimeException and Error are
known as checked exceptions e.g. IOException, SQLException etc. Checked exceptions are
checked at compile-time.

2) Unchecked Exception

The classes which inherit RuntimeException are known as unchecked exceptions e.g.
ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc.
Unchecked exceptions are not checked at compile-time, but they are checked at runtime

10. What are the keyword in Exception.

1. try
2. catch
3. throw
4. throws
5. finally

11. What is use of try block?

The "try" keyword is used to specify a block where we should place exception code. The try
block must be followed by either catch or finally. It means, we can't use try block alone.

12. What is catch block?

The "catch" block is used to handle the exception. It must be preceded by try block which
means we can't use catch block alone. It can be followed by finally block later.
13. What is use of ‘throw statement’ give an example? (or) state the purpose of the
throw statement.

Whenever a program does not want to handle exception using the try block, it can use the
throws clause. The throws clause is responsible to handle the different types of exceptions
generated by the program. This clause usually contains a list of the various types of exception
that are likely to occur in the program.

14. What is throws in java?

The Java throws keyword is used to declare an exception. It gives an information to the
programmer that there may occur an exception so it is better for the programmer to provide
the exception handling code so that normal flow can be maintained.

return_type method_name() throws exception_class_name{


//method code }
throw

15. Difference between throw and throws.

throw throws
Java throw keyword is used to explicitly Java throws keyword is used to declare an
throw an exception. exception.
Checked exception cannot be propagated Checked exception can be propagated with
using throw only. throws.

16. What is finally in Java with example?

The finally block always executes immediately after try-catch block exits. The finally block
is executed incase even if an unexpected exception occurs. The runtime system always
executes the code within the finally block regardless of what happens in the try block.

17. What is the difference between error and exception?

An Error "indicates serious problems that a reasonable application should not try to catch."

An Exception "indicates conditions that a reasonable application might want to catch." Error
along with RuntimeException & their subclasses are uncheckedexceptions. All other
exception classes are checked exceptions.

18. How does nested try catch work in Java?

Nested try catch blocks. Exception handlers can be nested within one another. Atry, catch or
a finally block can in turn contains another set of try catch finally sequence. In such a
scenario, when a particular catch block is unable to handle an Exception, this exception is
rethrown.
19. Mention the use of try block?

Program statements that you want to monitor for exceptions are contained within a try
block. If an exception occurs within the try block, it is thrown. Your code can catch this
exception (using catch) and handle it in some rational manner.

20. How to create own exceptions?

Define a subclass of Exception ( a subclass of Throwable). subclasses don’t need to actually


implement anythingit is their existence in the type system that allows you to use them as
exceptions.

The Exception class does not define any methods of its own. It does, of course, inherit those
methods provided by Throwable. Thus, all exceptions, including those that we create, have
the methods defined by Throwable available to them.

21. Write a program to handle division by zero?

class Exc2 {
public static void main(String args[])
{
int d, a;
try {
d = 0;
a = 42 / d;
catch (ArithmeticException e) {
System.out.println("Division by zero."); }
System.out.println("After catch statement.");
}}

PART - B

1. What are packages in Java? Explain the types of packages and how packages help in
organizing Java code.

2. Describe how to create and use user-defined packages in Java with an example.

3. How are packages accessed in Java? Explain with syntax and examples the steps to import
and use classes from a package.

4. Explain abstract classes in Java. How do abstract classes differ from concrete classes?

5. What is an abstract method? Write a Java program to demonstrate the use of abstract
classes and methods.

6. Define an interface in Java. How is it different from an abstract class? Give suitable
examples.
7. Describe how to implement interfaces in Java. Write a program to demonstrate interface
implementation.

8. Explain the concept of extending interfaces. How is it useful? Illustrate with a suitable
example.

9. What is multiple inheritance? How is multiple inheritance achieved in Java using


interfaces?

10. Compare and contrast abstract classes and interfaces in Java in terms of syntax, usage,
and limitations.

11. Explain the importance of exception handling in Java. Describe its role in writing robust
programs.

12. Differentiate between errors and exceptions in Java with suitable examples.

13. Describe the exception hierarchy in Java. How are checked and unchecked exceptions
categorized?

14. What is the use of try, catch, and finally blocks in Java? Write a program to illustrate their
usage.

15. Explain the difference between throw and throws keywords in Java with examples.

16. Discuss any five built-in exceptions in Java. Explain how each is triggered and how it can
be handled.

17. Write a program in Java that demonstrates the use of multiple catch blocks to handle
different exceptions.

18. What are user-defined exceptions? How do you create and use them in Java? Provide a
program-based explanation.

19. Explain how exception handling improves the reliability and maintainability of a Java
application.

20. Discuss the best practices in exception handling in Java with examples that demonstrate
improper and proper usage.

PART - C

1. Write a JAVA program to copy a text file into another text file and to raise exceptions for
all cases.

You might also like