Object Oriented Programming: Unit III: Exception Handling and Multithreading

Two marks Questions with Answers

Exception Handling and Multithreading | Object Oriented Programming

Exception is a mechanism which is used for handling unusual situation that may occur in the program.

Two Marks Questions with Answers

 Q.1 What is an exception? Give example.

Ans: 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.

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

Ans. An uncaught exception results in invoking of the uncaughtException() method. As a result eventually the program will terminate in which it is thrown.

Q.3 What is the benefit of exception handling?

Ans. When calling method encounters some error then the exception can be thrown. This avoids crashing of the entire application abruptly.

Q.4 What is the difference between error and exception in java?

Ans. 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.

Q.5 What is compile time and run time error?

Ans;

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.

 Q.6 What is the use of try, catch keywords?

Ans. : 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.

 Q.7 What is the difference between throw and throws?

Ans. :

1. The throw keyword is used to explicitly throw an exception.

2. The throws keyword is used to declare an exception.

Q.8 What is ArrayIndexOutOfBoundsException?

Ans. When we use an array index which is beyond the range of index then ArrayIndexOutOfBoundsException occurs.

Q.9 What is the need of multiple catch?

Ans.:

• 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.

Q.10 There are three statements in a try block - statement1, statement2 and statement3. After that there is a catch block to catch the exceptions occurred in the try block. Assume that exception has occurred in statement2. Does statement3 get executed or not?

Ans. No. Once a try block throws an exception, remaining statements will not be executed. Control comes directly to catch block.

Q.11 Explain the types of exceptions.

Ans. 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.

Q.12 Does finally block get executed If either try or catch blocks are returning the control?

Ans. Yes, finally block will be always executed no matter whether try or catch blocks are returning the control or not.

Q.13 Can we have empty catch block?

Ans. 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.

Q.14 Which class is the super class for all types of errors and exceptions in java ?

Ans. The java.lang.Throwable is the super class for all types of errors and exceptions in java.

Q.15 What is runtime exceptions?

Ans. 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 exceptions of the Java.

Q.16 What is exception handling?

Ans. 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.

Thus exception handling is a mechanism that prevents the program from crashing when some unusual situation occurs.

Q.17 What happens when the statement int value = 25/0 is executed?

Ans. The exception Arithmetic Exception: Divide by zero will occur.

Q.18 What do you mean by threads in Java ?

Ans. Thread is tiny program running continuously. It is a light weight process in Java..

 Q.19 Give the difference between process and thread.

Ans. 

Q.20 What are different stages in thread?

Ans. Various stages in thread are

1. New state

2. Time waiting state

3.Runnable state

4. Waiting state

5.Blocked state

6. Terminated state

Q.21 What do you mean by synchronization ?

Ans. 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.

Q.22 What are the three ways by which the thread can enter in waiting stage?

Ans. :

i) 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.

Q.23 What is multithreading?

Ans. 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.

Q.24 Mention two mechanisms for protecting a code block from concurrent access.

Ans. There are two mechanisms for protecting a code block from concurrent access -

1. Use of reentrant code

2. Use synchronized block

Q.25 What is meant by notify methods in multithreading?

Ans. 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.

Q.26 What are the two ways of creating a thread?

Ans. Thread can be created using

1. Thread class 2. runnable interface.

Q.27 Why do we need run() and start() method both? Can we achieve it with only run() method?

Ans. 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.

Q.28 What is the need for thread?

Ans. In Java threads are used to handle multiple tasks together. This kind of programming is called concurrent programming.

Q.29 Name any four thread constructor.

Ans. :

1. Thread()

2. Thread(String name).

3. Thread(Runnable target)

4. Thread (Runnable target, String name)

Q.30 When thread is initiated and created, what is its initial stage?

Ans. : 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

state.

Q.31 "Thread is a lightweight process" - Comment

Ans.

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.

Q.32 Sketch the life cycle of thread

Ans. 


Object Oriented Programming: Unit III: Exception Handling and Multithreading : Tag: : Exception Handling and Multithreading | Object Oriented Programming - Two marks Questions with Answers