0% found this document useful (0 votes)
3 views

Java Interview questions(1)

The document explains the concepts of FailFast and FailSafe iterators, detailing their behaviors and providing examples from Java Collections. It also covers exception handling in Java, outlining the types of exceptions (built-in and user-defined), differences between errors and exceptions, and the hierarchy of exception classes. Additionally, it discusses runtime exceptions, specifically NullPointerException, and distinguishes between checked and unchecked exceptions.

Uploaded by

prasanna.mallari
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Java Interview questions(1)

The document explains the concepts of FailFast and FailSafe iterators, detailing their behaviors and providing examples from Java Collections. It also covers exception handling in Java, outlining the types of exceptions (built-in and user-defined), differences between errors and exceptions, and the hierarchy of exception classes. Additionally, it discusses runtime exceptions, specifically NullPointerException, and distinguishes between checked and unchecked exceptions.

Uploaded by

prasanna.mallari
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Explain the FailFast iterator and FailSafe iterator along with examples for
each.

A FailFast iterator is an iterator that throws a ConcurrentModificationException if


it detects that the underlying collection has been modified while the iterator is
being used. This is the default behavior of iterators in the Java Collections
Framework. For example, the iterator for a HashMap is FailFast.

A FailSafe iterator does not throw a ConcurrentModificationException if the


underlying collection is modified while the iterator is being used. Alternatively,
it creates a snapshot of the collection at the time the iterator is created and
iterates over the snapshot. For example, the iterator for a ConcurrentHashMap is
FailSafe.

2. What is Exception Handling?

An Exception is an Event that interrupts the normal flow of the program and
requires special processing. During the execution of a program, errors and
unplanned occurrences can be dealt with by using the Java Exception Handling
mechanism. Below are some reasons why Exceptions occur in Java:

Device failure
Loss of Network Connection
Code Errors
Opening an Unavailable file
Invalid User Input
Physical Limitations (out of disk memory)

3. How many types of exceptions can occur in a Java program?

There are generally two types of exceptions in Java:

Built-in Exceptions: Built-in exceptions in Java are provided by the Java


Libraries. These exceptions can be further divided into two subcategories i.e.,
checked and unchecked Exceptions. Below are some of the built-in exceptions in
Java:
- ArrayIndexOutOfBoundsExceptions
- ClassNotFoundException
- FileNotFoundException
- IOException
- NullPointerException
- ArithmeticException
- InterruptedException
- RuntimeException

User-Defined Exceptions: User-defined exceptions are defined by the programmers


themselves to handle some specific situations or errors which are not covered by
built-in exceptions. To define user-defined exceptions a new class that extends the
appropriate exception class must be defined. User-defined Exceptions in Java are
used when the built-in exceptions are in Java.

4. Difference between an Error and an Exception.


Errors -> Recovering from Errors is not possible.
Errors are all unchecked types in Java.
Errors are mostly caused by the environment in which the program is running.
Errors can occur at compile time as well as run time. Compile Time: Syntax Error,
Run Time: Logical Error.
They are defined in java.lang.Error package.
Examples: java.lang.StackOverflowError, java.lang.OutOfMemoryError

Exceptions -> Recover from exceptions by either using a try-catch block or throwing
exceptions back to the caller.
It includes both checked as well as unchecked types that occur.
The program is mostly responsible for causing exceptions.
All exceptions occur at runtime but checked exceptions are known to the compiler
while unchecked are not.
They are defined in java.lang.Exception package
Examples: Checked Exceptions: SQLException, IOException Unchecked Exceptions:
ArrayIndexOutOfBoundException, NullPointerException, ArithmeticException.

5. Explain the hierarchy of Java Exception classes.

All exception and error types in Java are subclasses of the class throwable, which
is the base class of the hierarchy. This class is then used for exceptional
conditions that user programs should catch. NullPointerException is an example of
such an exception. Another branch, error is used by the Java run-time system to
indicate errors having to do with the JRE. StackOverflowError is an example of one
of such error.

6. Explain Runtime Exceptions.

Runtime Exceptions are exceptions that occur during the execution of a code, as
opposed to compile-time exceptions that occur during compilation. Runtime
exceptions are unchecked exceptions, as they aren’t accounted for by the JVM.

Examples of runtime exceptions in Java include:

- NullPointerException: This occurs when an application attempts to use a null


object reference.
- ArrayIndexOutOfBoundsException: This occurs when an application attempts to
access an array index that is out of bounds.
- ArithmeticException: This occurs when an application attempts to divide by zero.
- IllegalArgumentException: This occurs when a method is passed on an illegal or
inappropriate argument.

Unlike checked exceptions, runtime exceptions do not require a declaration in the


throws clause or capture in a try-catch block. However, handling runtime exceptions
is advisable in order to provide meaningful error messages and prevent a system
crash. Because runtime exceptions provide more specific information about the
problem than checked exceptions, they enable developers to detect and correct
programming errors more easily and quickly.

7. What is NullPointerException?

It is a type of run-time exception that is thrown when the program attempts to use
an object reference that has a null value. The main use of NullPointerException is
to indicate that no value is assigned to a reference variable, also it is used for
implementing data structures like linked lists and trees.

8. What is the difference between Checked Exception and Unchecked Exception?

Checked Exception:
Checked Exceptions are the exceptions that are checked during compile time of a
program. In a program, if some code within a method throws a checked exception,
then the method must either handle the exception or must specify the exception
using the throws keyword.
Checked exceptions are of two types:

- Fully checked exceptions: all its child classes are also checked, like
IOException, and InterruptedException.
- Partially checked exceptions: some of its child classes are unchecked, like an
Exception.

Unchecked Exception:
Unchecked are the exceptions that are not checked at compile time of a program.
Exceptions under Error and RuntimeException classes are unchecked exceptions,
everything else under throwable is checked.

You might also like