Unit-3 Two Marks Questions With Answers
Unit-3 Two Marks Questions With Answers
Ans: Exception is a mechanism which is used for handling unusual situation that
may occur in the program. For example -
Ans. When calling method encounters some error then the exception can be
thrown. This avoids crashing of the entire application abruptly.
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.
Ans. :
Ans. When we use an array index which is beyond the range of index then
ArrayIndexOutOfBoundsException occurs.
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.
For example: IOException which should be handled using the try-catch block.
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.
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.
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.
3.Runnable state
4. Waiting state
5.Blocked state
6. Terminated state
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.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
-
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.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.
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)
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.
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.
Ans.