Java Theory Question Answer
Java Theory Question Answer
The loop body executes when the The loop body executes once, even
condition becomes true. if the condition is true or false.
Control after 'break' resumes the control of the 'continue' resumes the control of the
break/continue program to the end of loop program to the next iteration of that
Causes It causes early termination of loop. It causes early execution of the next
iteration.
Continuation 'break' stops the continuation of 'continue' do not stops the continuation
iteration.
Other uses 'break' can be used with 'switch', 'continue' can not be executed with
Class Interface
Variables and methods in a class can be declared All variables and methods in an
using any access specifier(public, private, default, interface are declared as
protected). public.
16. Describe the different states that a thread goes through during its lifetime.
1. NEW – a newly created thread that has not yet started the
execution
2. RUNNABLE – either running or ready for execution but it's waiting
for resource allocation
3. BLOCKED – waiting to acquire a monitor lock to enter or re-enter a
synchronized block/method
4. WAITING – waiting for some other thread to perform a particular
action without any time limit
5. TIMED_WAITING – waiting for some other thread to perform a
specific action for a specified period
6. TERMINATED – has completed its execution
17. Draw the state transition diagram of thread life cycle. [ Note]
18. Which methods are required to go from active state to blocked state of a
thread.
When sleep() method is invoked on a thread to sleep for specified time period,
the thread is out of queue during this time period.
19. Which methods are required to go from blocked state to runnable state of
a thread.
The start() method of the Thread class is used to initiate the execution of a
thread and it goes into runnable state and the sleep() and wait() methods of
the Thread class sends the thread into non runnable state. After non runnable
state, thread again comes into runnable state and starts its execution.
20. What is runnable and running state of a thread.
Running State of a thread where the currently executing in the processor is
said to in a Running state. It is the responsibility of the thread scheduler to give
the thread, time to run.
The second phase of a new-born thread is the execution phase. When the
start() method is called on a the new instance of a thread, it enters into a
runnable state. In the runnable state, thread is ready for execution and is
waiting for availability of the processor (CPU time).
21. What is thread priority.
In Java, a thread's priority is an integer in the range 1 to 10. The larger the
integer, the higher the priority.
Java Thread setPriority() method
1. public static int MIN_PRIORITY: It is the maximum priority of a thread.
The value of it is 1.
2. public static int NORM_PRIORITY: It is the normal priority of a thread.
The value of it is 5.
3. public static int MAX_PRIORITY: It is the minimum priority of a thread.
The value of it is 10.
22. What is an exception. Give an example. [ Note]
An exception (or exceptional event) is a problem that arises during
the execution of a program. When an Exception occurs the normal
flow of the program is disrupted and the program/Application
terminates abnormally, which is not recommended, therefore, these
exceptions are to be handled.
23. Describe the exception handling mechanism in java.
24. What is finally statement in java. Give an example.
The finally block in java is used to put important codes such as clean up code
e.g. closing the file or closing the connection. The finally block executes
whether exception rise or not and whether exception handled or not. A finally
contains all the crucial statements regardless of the exception occurs or not.
o finally block in Java can be used to put "cleanup" code such as closing a
file, closing connection, etc.
o The important statements to be printed can be placed in the finally
block.
25. List some of the most common types of exceptions that might occur in java
• NullPointerException. A NullPointerException is thrown when a Java
program attempts to process an object which contains a null value. ...
• ArrayIndexOutOfBoundsException. ...
• IllegalStateException. ...
• ClassCastException. ...
• ArithmeticException. ...
• IllegalArgumentException.