You are on page 1of 5

UNIT-3

1. What is an Exception?
A: An exception is an unwanted or unexpected event, which occurs during the execution of a
program i.e. at run time that disrupts the normal flow of the program’s instructions.

2.Write benefits of exception handlings?


• Exception handling allows to control normal flow of the programme by using exception
handling in programme.
• It throws an exception whenever a called method encounters an error providing that the
calling method takes care of the error.
• It also gives us the scope of organizing and differentiating btw different error type using a
separate block of block.

3. Distinguish between exception and error?


A: Error: An Error indicates serious problem that a reasonable application should not try to
catch.
Exception: Exception indicates conditions that a reasonable application might try to catch.

4. Write classifications of exceptions?


A: 1.Synchronous exceptions
2 .Asynchronous exceptions

• Synchronous exceptions happen at a specific program statement, no matter, how


many times to run a program in similar execution environment.

• Asynchronous exceptions can raise practically anywhere. It follows that asynchronous


exception handling can’t be required by the compiler.

5. Define checked and unchecked exceptions?


1) Checked: are the exceptions that are checked at compile time. If some code within a method
throws a checked exception, then the method must either handle the exception or it must specify the
exception using throws keyword

2) Unchecked: The exception that are checked by the JVM are called unchecked exceptions
Unchecked exceptions are considered as uncoverable and programmer cannot do anything when
the unchecked error occurred in our programmer.

6. Write the java exceptional handlings keywords?


There are 5 keywords used in java exception handling.

(i).TRY: Java try block is used to enclose the code that might throw an exception. It must be used
within the method. Java try block must be followed by either catch or finally block.

(ii).CATCH: Java catch block is used to handle the Exception. It must be used after the try block
only. You can use multiple catch block with a single try.

(iii).THROW: The Java throw keyword is used to explicitly throw an exception. We can throw either
checked or unchecked exception in java by throw keyword. The throw keyword is mainly used to
throw custom exception. We will see custom exceptions later.

(iv).FINALLY: Java finally block is a block that is used to execute important code such as closing
connection, stream etc. Java finally block is always executed whether exception is handled or not Java
finally block follows try or catch block.

(v).THROWS: 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.

7. Define java rethrowing exceptions?


A: Rethrow exception allows you to specify more specific exceptions types in the throw clause
of a method declaration.

8. What are built in exceptions in java with examples?

A: Built-in exceptions are the exceptions which are available in Java libraries. These exceptions are
suitable to explain certain error situations. Below is the list of important built-in exceptions in Java.

Examples: Arithmetic exceptions, Array index out of the bound exceptions, Class not found
exceptions, IO exceptions, File not found exceptions.

9. Difference between throw and throws?


A:

Throw Throws
(i)Java throw keyword is used to explicitly throw (i)Java throws keyword is used to declare an exception.
an exception
(ii) Throws is followed by class.
(ii) Throw is followed by an instance.
(iii) You can declare multiple exceptions e.g.
(iii) You cannot throw multiple exceptions. public void method()throws IO Exception, SQL
Exception.
10. Define multithreading?

A: Multithreading is a process of executing multiple threads simultaneously. Its main advantage


is:

Threads share the same address space.

Thread is lightweight.

Cost of communication between process is low.

11. Define multitasking?

A: Executing multiple tasks simultaneously is called multitasking and is divided into two
categories namely process based multitasking and thread based multitasking.

12. what are advantages of multitasking?

• Multithreading is supported by java


• Which increases the performance
• The time taken to create a task will be reduced.
• The execution of program becomes fast.
• The effectiveness of program is increased.

13. Differences between process based and thread based multitasking ?

Process Based Thread based


(i)Executing multiple programs simultaneously. (i)Executing two or more parts of same
programs simultaneously.
(ii)These are heavy weight components
(ii)These are light weight components.
(iii)Which occupies more memory space
(iii) Which occupies less memory space.
(iv) It is depends on operating system.
(iv) It depends upon interpreter and compiler.

14. What is Threads, how can we create threads in java?


A: Group of or sequence of instructions is called thread. There are two ways to create threads
first by implementing runnable interface and then creating a thread a thread object from it and
second is to extend the thread class.

15. What is a daemon thread?

A: The daemon threads are basically the low priority threads that provides the background
support to the user threads. It provides services to the user threads.

16. Write thread states?

A: The life cycle of the thread in java is controlled by JVM. The java thread states are as
follows:

1. New
2. Runnable
3. Running
4. Non-Runnable (Blocked)
5. Terminated.

17. How threads are interrupting ?

A: If any thread is in sleeping or waiting state (i.e. sleep () or wait () is invoked), calling the
interrupt () method on the thread, breaks out the sleeping or waiting state throwing Interrupted
Exception. If the thread is not in the sleeping or waiting state, calling the interrupt () method
performs normal behaviour and doesn't interrupt the thread but sets the interrupt flag to true.

18. Write about the thread priorites?


A: Each thread have a priority. Priorities are represented by a number between 1 and 10. In most
cases, thread schedular schedules the threads according to their priority. But it is not guaranteed
because it depends on JVM specification that which scheduling it chooses.

19. How threads are synchronized?

A: Synchronization in java is the capability to control the access of multiple threads to any
shared resource.

Java Synchronization is better option where we want to allow only one thread to access the
shared resource .There are two types of synchronization

1. Process Synchronization
2. Thread Synchronization
20. Define inter thread communication?

A: Inter-thread communication or Co-operation is all about allowing synchronized threads to


communicate with each other.

Cooperation (Inter-thread communication) is a mechanism in which a thread is paused running in


its critical section and another thread is allowed to enter (or lock) in the same critical section to
be executed. It is implemented by following methods of Object class:

o wait()
o notify()
o notify All()

You might also like