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

Unit IV 2 Marks Copy

The document outlines key concepts of exception handling and multithreading in Java, including definitions, types of exceptions, and the lifecycle of threads. It discusses the importance of handling exceptions to prevent program crashes, the differences between checked and unchecked exceptions, and various thread states and synchronization. Additionally, it includes questions and topics for further exploration related to these concepts.

Uploaded by

techytalk2025
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 views4 pages

Unit IV 2 Marks Copy

The document outlines key concepts of exception handling and multithreading in Java, including definitions, types of exceptions, and the lifecycle of threads. It discusses the importance of handling exceptions to prevent program crashes, the differences between checked and unchecked exceptions, and various thread states and synchronization. Additionally, it includes questions and topics for further exploration related to these concepts.

Uploaded by

techytalk2025
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/ 4

8115 - K.

RAMAKRISHNAN COLLEGE OF ENGINEERING


(AUTONOMOUS)
Department of Computer Science and Engineering
UCS1303 – Object Oriented Programming with Java

UNIT IV
PART A
Sl. No. Questions
1. What is an exception? Give example.
Exception is a mechanism which is used for handling unusual situation that may occur in
the program. For example -
ArithmeticException: This exception is used to handle arithmetic exceptions such as
divide by zero.
IOException: This exception occurs when an illegal input/output operation is performed.

2. What will happen if an exception is not caught?


An uncaught exception results in invoking of the uncaughtException() method. As a result
eventually the program will terminate in which it is thrown.
3. What are stack trace elements.
Stack trace element is created to represent the specific execution point. The syntax of stack
trace element is StackTraceElement(String ClassName, String MethodName, String
The class name represents the name of the class for which the execution point is specified.
4. What is the benefit of exception handling?
When calling method encounters some error then the exception can be thrown. This avoids
crashing of the entire application abruptly.
5. What is difference between error and exception in Java?
Errors are mainly caused by the environment in which an application is running. For
example, OutOfMemoryError happens there is shortage of memory. Where as exceptions
are mainly caused by the application itself. For example, NullPointerException occurs
when an application tries to access null object.
6. What is compile time and run time error ?
1. The errors that are detected by the Java compiler during the compile time are called
compiler time errors.
2. The runtime errors are basically the logically errors that get caused due to wrong logic.
7. What is the use of try, catch keywords?
try - A block of source code that is to be monitored for the exception.
catch - The catch block handles the specific type of exception along with the try block

8. What is the need of multiple catch?


There may be the situations in which different exceptions may get raised by a single try
block statements and depending upon the type of exception thrown it must be caught.
To handle such situation multiple catch blocks may exist for the single try block
statements.

9. Explain the types of exceptions.


There are two types of exceptions:
1 Checked Exception: These types of exceptions need to be handled explicitly by the code
itself either by using the try-catch block or by using throws. These exceptions are extended
from the java.lang.Exception class.
For example: IOException which should be handled using the try-catch block.

2 Unchecked Exception: These type of exceptions need not be handled explicitly. The Java
Virtual Machine handles these type of exceptions. These exceptions are extended from
java.lang.RuntimeException class.
For example: ArrayIndexOutOfBounds, NullPointerException, RunTimeException.
10. Can we have empty catch block?
Yes we can have empty catch block, but it is an indication of poor programming practice.
We should never have empty catch block because if the exception is caught by that block,
we will have no information about the exception and it will create problem while
debugging the code.

11. What is runtime exceptions?


RuntimeException is the superclass of those exceptions that can be thrown during the
normal operation of the Java Virtual Machine. Runtime Exception is the parent class in all
expectations of the java.
12. What is exception handling?
Exception handling is a mechanism in which the statements that are likely to cause an
exception are enclosed within try block. As soon as exception occurs it is handled using
catch block.
13. What is assert keyword?
The assert keyword is used for testing. It can be used for assumption about the program.
While executing the assertion it is assumed to be true.

14. Difference between ‘Final’, ‘Finally’, and ‘Finalize’ in Java.


Final : final is the keyword and access modifier which is used to apply restrictions on a
class, method or variable.
Finally : finally is the block in Java Exception Handling to execute the important code
whether the exception occurs or not.
Finalize : finalize is the method in Java which is used to perform clean up processing just
before object is garbage collected.

15. What are different stages in thread?


Various stages in thread are
1. New state
2. Runnable state
3. Waiting state
4. Time waiting state
5. Blocked state
6. Terminated state
16. What do you mean by synchronization?
When two or more threads need to access shared memory, then there is some way to
ensure that the access to the resource will be by only one thread at a time. The process of
ensuring one access at a time by one thread is called synchronization.
17. What are the three ways by which the thread can enter in waiting stage?
Waiting state: Sometimes one thread has to undergo in waiting state because another
thread starts executing. This can be achieved by using wait() state.
ii) Timed waiting state: There is a provision to keep a particular threading waiting for
some time interval. This allows to execute high prioritized threads first. After the timing
gets over, the thread in waiting state enters in runnable state. This can be achieved by
using the sleep() command.
iii) Blocked state: When a particular thread issues an input/output request then operating
system sends it in blocked state until the I/O operation gets completed. After the I/O
completion the thread is sent back to the runnable state. This can be achieved by using
suspend() method.
18. What is multithreading?
Multithreading is an environment in which multiple threads are created and they can
execute simultaneously. The multiple threads can be created either by extending the thread
class or by implementing the runnable interface.

19. What is meant by notify methods in multithreading?


The notify() method is used for inter thread communication. If a particular thread is in the
sleep mode then that thread can be resumed by using the notify call.
20. Why do we need run() and start() method both? Can we achieve it with only run
method?
We use start() method to start a thread instead of calling run() method because the run()
method is not a regular class method. It should only be called by the JVM. If we call the
run() method directly then it will simply invoke the caller's thread instead of its own
thread. Hence the start() method is called which schedules the thread with the JVM.
21. When thread is initiated and created, what is its initial stage?
A thread is in "Ready" state after it has been created and started. This state signifies that
the thread is ready for execution. From here, it can be in the running.
22. "Thread is a lightweight process" - Comment
Threads do not require separate address for its execution. It runs in the address space of
the process to which it belongs to. Hence thread is a lightweight process.

23. Name any four thread constructor.


1. Thread()
2. Thread(String name)
3. Thread(Runnable target)
4. Thread(Runnable target,String name)
24. What do you mean by threads in Java?
Thread is a light-weight process.
Threads do not require separate address Each space for its execution. It runs in the address
space of the process to which it belongs to.

25. Sketch the life cycle of thread.

PART B
1. Define Exception.Discuss about exception hierarchy in Java with example.

2. Explain types of exceptions with example programs. [Checked Exception & Unchecked
Exception]
3. Explain the five keywords in exception handling with necessary syntax and examples.
4. Explain in detail about User-Defined Exception with Example Program.

5. Write a program to illustrate arithmetic exception,ArrayIndexOutOfBoundsException and


NumberFormatException.
6. Explain in detail about the thread. Describe the lifecycle and states of thread with examples.

7. Show how to extend Thread class and how to implement the runnable interface for creating and
starting threads.

8. Describe the creation of a single thread and multiple threads using an example.

9. Illustrate Thread Priorities & Inter-thread communication with producer/consumer problems

10. Create a simple real life application program in Java to illustrate the use of multithreads.

You might also like