UNIT 3 QB Oops
UNIT 3 QB Oops
Exception Handling basics - Multiple catch Clauses- Nested try Statements - Java’s built-
in exceptions-User defined exception. Multithreaded Programming: Java Thread Model,
Creating a thread and multiple threads- Priorities- Synchronization- Inter-Thread
communication-Suspending-Resuming and Stopping Threads- Multithreading. Wrappers-
Auto boxing.
UNIT-III/ PART-A
1 What are the types of errors?
Compile time errors
Run time errors
2 Define Java Exception.
An Exception is an event that occurs during program execution which disrupts the normal
flow of a program. It is an object which is thrown at runtime.
27 Why do we need run() and start() method both? Can we achieve it with only run method?
• The separate start() and run() methods in the Thread class provide two ways to create threaded
programs. The start() method starts the execution of the new thread and calls the run() method. The
start() method returns immediately and the new thread normally continues until the run() method
returns.
• The Thread class' run() method does nothing, so sub-classes should override the method with
code to execute in the second thread. If a Thread is instantiated with a Runnable argument, the thread's
run() method executes the run() method of the Runnable object in the new thread instead.
• Depending on the nature of your threaded program, calling the Thread run() method directly can
give the same output as calling via the start() method, but in the latter case the code is actually executed
in a new thread.
The suspend () method of thread class puts the thread from running to waiting state. This method is
used if you want to stop the thread execution and start it again when a certain event occurs. This
method allows a thread to temporarily cease execution. The suspended thread can be resumed using
the resume() method.
public final void suspend()
30 Define Resume().
The resume() method of thread class is only used with suspend() method. This method is used to
resume a thread which was suspended using suspend() method. This method allows the suspended
thread to start again.
A Wrapper class in Java is a class whose object wraps or contains primitive data types. When we
create an object to a wrapper class, it contains a field and, in this field, we can store primitive data
types.
32 What is Autoboxing and Unboxing?
Autoboxing:
The automatic conversion of primitive types to the object of their corresponding wrapper classes is
known as autoboxing. For example – conversion of int to Integer, long to Long, double to Double,
etc.
Unboxing:
It is just the reverse process of autoboxing. Automatically converting an object of a wrapper class to
its corresponding primitive type is known as unboxing. For example – conversion of Integer to int,
Long to long, Double to double, etc.
33 Define Arithmatic Exception with example
ArithmeticException: An arithmetic exception in java is a Runtime exception . JVM throws
Arithmetic Exception when a wrong mathematical expression occurs in a java program. such as
divide by zero
class Simple
{
public static void main(String args[])
{int data=50/0;
System.out.println("rest of the code...");
}
Output: Exception in thread main java.lang.ArithmeticException:/ by zero
35 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.
Unit –III/Part B
1. Explain in detail the important methods of Java Exception Class.
2. Explain the different scenarios causing “Exception in thread main”.
3. How will you create your Own Exception? (user defined Exception)
4. Explain in detail the various exception types with its hierarchy.
5. Write programs to illustrate arithmetic exception, ArrayIndexOutOfBounds Exception and
NumberFormat Exception.
6. Write a calculator program using exceptions.
7. Create two exception classes that can be used by the stack classes developed by TRY.
8. What are the two ways of thread creation? Explain with suitable examples.
9. With illustrations explain multithreading, interrupting threads, thread states and thread
properties.
10. Describe the life cycle of thread and various thread methods.
11. Explain in detail about suspending, resuming and stopping threads.
12. Write a java program that synchronizes three different threads of the same program and
displays the contents of the text supplies through the threads.
13. Write a java program for inventory problem to illustrate the usage of thread synchronized
keyword and inter thread communication process. They have three classes called consumer,
producer and stock.
14. i)What is the purpose of thread priorities? What is the different thread
priorities that exist?
ii)Difference between throw and throws. Give example for both.
15. i)Describe the criteria for creating single and multithread using an example.
ii)Using an example, explain inter-thread communication in java.
16. Create a software for departmental stores to maintain the following details like item no, item
description , requested quantity, cost price. Provide the options to update the stock. Calculate the
selling price (SP = CP *20%).
Create an exception whenever the selling price of item exceeds the given amount
17. Discuss about try , catch and finally keywords in Exception handling with example
18. List and explain datatypes and their corresponding Wrapper class
19. Explain Exception hierarchy in detail
20. Write about pre-defined Exceptions (Built in Exceptions)