Core Java Interview Questions and Answers – Part 5

In this post, Java Interview Questions and Answers are discussed, which are helpful for Java beginners and Experienced persons. When you go for Selenium Test Automation Interview, you can expect similar kind of questions.

81. Explain when NoClassDefFoundError will be raised ?

This error is thrown when JVM tries to load the class but no definition for that class is found NoClassDefFoundError will occur. The class may exist at compile time but unable to find at runtime. This might be due to misspelled classname at command line, or classpath is not specified properly , or the class file with byte code is no longer available.

82. What is process ?

A process is a program in execution.

Every process have their own memory space.Process are heavy weight and requires their own address space. One or more threads make a process.

83. What is thread in java?

Thread is separate path of execution in program.

Threads are

  1. Light weight
  2. They share the same address space.
  3. creating thread is simple when compared to process because creating thread requires less resources when compared to process
  4. Threads exists in process. A process have atleast one thread.

84. Difference between process and thread?

Process Thread
1) Program in execution. Separate path of execution in program. One or more threads is called as process.
2) Processes are heavy weight Threads are light weight.
3) Processes require separate address space. Threads share same address space.
4) Interprocess communication is expensive. Interthread communication is less expensive compared to processes.
5) Context switching from one process to another is costly. Context switching between threads is low cost.

85. What is multitasking ?

Multitasking means performing more than one activity at a time on the computer. Example Using spreadsheet and using calculator at same time.

86. What are different types of multitasking?

There are two different types of multitasking :

  1. Process based multitasking
  2. Thread based multitasking

Process based multitasking : It allows to run two or more programs concurrently. In process based multitasking a process is the smallest part of code .

Example : Running Ms word and Ms powerpoint at a time.

Thread based multitasking : It allows to run parts of a program to run concurrently. Example : Formatting the text and printing word document at same time .

Java supports thread based multitasking and provides built in support for multithreading.

87. What are the benefits of multithreaded programming?

Multithreading enables to use idle time of cpu to another thread which results in faster execution of program. In single threaded environment each task has to be completed before proceeding to next task making cpu idle.

88. Explain thread in java?

  1. Thread is independent path of execution with in a program.
  2. A thread consists of three parts Virtual Cpu, Code and data.
  3. At run time threads share code and data i.e they use same address space.
  4. Every thread in java is an object of java.lang.Thread class.

89. List Java API that supports threads?

java.lang.Thread : This is one of the way to create a thread. By extending Thread class and overriding run() we can create thread in java.

java.lang.Runnable : Runnable is an interface in java. By implementing runnable interface and overriding run() we can create thread in java.

java.lang.Object : Object class is the super class for all the classes in java. In object class we have three methods wait(), notify(), notifyAll() that supports threads.

java.util.concurrent : This package has classes and interfaces that supports concurrent programming. Ex : Executor interface, Future task class etc.

90. Explain about main thread in java?

Main thread is the first thread that starts immediately after a program is started. Main thread is important because :

  1. All the child threads spawn from main thread.
  2. Main method is the last thread to finish execution.

When JVM calls main method() it starts a new thread. Main() method is temporarily stopped while the new thread starts running.

92. In how many ways we can create threads in java?

We can create threads in java by any of the two ways :

  1. By extending Thread class
  2. By Implementing Runnable interface.

93. Explain creating threads by implementing Runnable class?

This is first and foremost way to create threads . By implementing runnable interface and implementing run() method we can create new thread.

Method signature : public void run()

Run is the starting point for execution for another thread within our program. Example :

public class MyClass implements Runnable { 

@Override
public void run() {
// T
}
}

94. Explain creating threads by extending Thread class ?

We can create a thread by extending Thread class. The class which extends Thread class must override the run() method.

Example :

public class MyClass extends Thread { @Override

public void run() {
// Starting point of Execution
}
}

95. Which is the best approach for creating thread ?

The best way for creating threads is to implement runnable interface. When we extend Thread class we can’t extend any other class.

When we create thread by implementing runnable interface we can implement Runnable interface. In both ways we have to implement run() method.

96. Explain the importance of thread scheduler in java?

Thread scheduler is part of JVM use to determine which thread to run at this moment when there are multiple threads. Only threads in runnable state are choosen by scheduler.

Thread scheduler first allocates the processor time to the higher priority threads. To allocate microprocessor time in between the threads of the same priority, thread scheduler follows round robin fasion.

97. Explain the life cycle of thread?

A thread can be in any of the five states :

  1. New : When the instance of thread is created it will be in New state. Ex : Thread t= new Thread();

In the above example t is in new state. The thread is created but not in active state to make it active we need to call start() method on it.

  1. Runnable state : A thread can be in the runnable state in either of the following two ways :
  2. When the start method is invoked or
  3. A thread can also be in runnable state after coming back from blocked or sleeping or waiting state.
  4. Running state : If thread scheduler allocates cpu time, then the thread will be in running state.
  5. Waited /Blocking/Sleeping state:

In this state the thread can be made temporarily inactive for a short period of time. A thread can be in the above state in any of the following ways:

  1. The thread waits to acquire lock of an object.
  2. The thread waits for another thread to complete.
  3. The thread waits for notification of other thread.

5) Dead State : A thread is in dead state when thread’s run method execution is complete. It dies automatically when thread’s run method execution is completed and the thread object will be garbage collected.

98. Can we restart a dead thread in java?

If we try to restart a dead thread by using start method we will get run time exception since the thread is not alive.

99. Can one thread block the other thread?

No one thread cannot block the other thread in java. It can block the current thread that is running.

100. Can we restart a thread already started in java?

A thread can be started in java using start() method in java. If we call start method second time once it is started it will cause RunTimeException(IllegalThreadStateException). A runnable thread cannot be restarted.

Leave Comment

Your email address will not be published. Required fields are marked *

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

https://www.youtube.com/automationtalks

Get the Framework code at Github Repo: https://github.com/prakashnarkhede?tab=repositories