Java Interview Questions and answer You Need to Know
Before we start learning the java interview question and answer lets we discuss some popular topics to be considered important for a Java Interview.
If you are preparing for the java interview then you need to focus on the few popular topics that are considered important for a java interview. Following are the some core Java concepts that are popular for a java interview:
- Basic OOPS concepts such as Data Abstraction, Encapsulation, Inheritance, Polymorphism.
- String Handling
- Loops and Data Types
- Collection framework
- Exception handling
- Multi threading
- Synchronisation and Generics
- Serialisation and De-serialisation
Following are the some advanced java and popular frameworks:
- JDBC (Java Database Connectivity)
- Servlet
- JSP
- Struts and Spring (includes MVC, Core, JDBC, ORM, AOP)
- Hibernate ORM framework
- JSF and Web Services (SOAP & REST)
Core Java Interview Questions and Answers
- How would you call wait() method?
We can call wait() method using loop. The wait() method holds the current thread to execute and thread wait until notify() or notifyAll() method is not invoked by other thread for this object. It is recommended to check the condition in a loop before continuing.
- What is the step to take thread dump in Java?
To take thread dump we uses kill command (Linux command). To dump just write the following command in Linux:
kill -3 PID
Where, PID is the java process id.
In Windows use the following shortcut:
Ctrl + Break
This tells the Java Virtual machine to print the thread dump in standard out and may go to log or console (it is totally depends on the configuration of application).
- Is Swing is thread-safe?
The answer is No; swing is not thread-safe. You can update the swing components (such as JTable, JList, etc.) using GUI and AWT threads. To do this swing has following methods:
- invoke and Wait()
- invokeLater()
- Do know what the difference between sleep() is and wait() methods in Java?
In java both methods are used to hold (or pause) a thread. sleep() is used for a ‘short pause’ and release no lock while wait() is a conditional wait. So when condition fulfills then the paused thread free from the wait. So wait() is actually conditional wait and release a lock.
- Explain the uses of run() and start() methods of Thread.class?
When we call a thread then Thread.run() will be executed while to creating a new thread Thread.start() will executed.
- What is defined as an immutable object?
When an object is not able to change their state (once defined) then that object is knows as immutable object. It means if you try to change the state of that object then the new object is created. For example, String, Integer, and other wrapper class.
- What’s the difference between Callable and Runnable?
Callable | Runnable |
This methods returns value. | This method cannot return a value. |
Throw a checked exception. | Cannot throw exception. |
Introduced in Java 1.5. | Since Java 1.0. |
- Is ++ operator is thread-safe in Java?
‘++’ operator is not thread safe because this operator can be used for multiple purpose like reading a value, implicating that value and storing the value back into the memory. So this is overlapped between multiple threads.
- Which one will take more memory: an int or Integer?
Integer takes more memory because int is primitive and Integer stores metadata overhead about the object.
- Which class contains method: Cloneable or Object?
cloneable is a marker interface and found under the lang package (java.lang.Cloneable) this interface does not have any method. clone() method is defined under the Object class. clone() is native method.
- Explain the concept of constructor chaining in Java?
When programmers call one constructor from another constructor then this is known as constructor chaining. A good example of this is to overload the constructor in a class.
- Write the default values of Java primitives?
Data Types | Value |
byte | 0 |
int | 0 |
boolean | False |
short | 0 |
long | 0L |
float | 0.0f |
double | 0.0d |
char | ‘\u0000’ |
- What is transient keyword used in Java?
In java when transient keyword is used then it show that field in the class should not be serialized.
- Explain the size of an int variable in 32-bit and 64-bit JVM?
In both JVMs the size of int is identical. It should be 32-bits or 4 bytes.
- Can you guarantee the garbage collection process?
No, we cannot guarantee the garbage collection process. We can just make a request using Runtime.gc() or System.gc() methods.
- What is the difference between final, finalize and finally?
In java final is a modifier and can be applied on a method, variable or class. When you add final keyword with a variable then value of that variable cannot be changed once initialized.
finalize is a method of Object class (java.lang.Object.finalize()) called by the garbage collector. When an object have no more reference then this method is called.
finally keyword is used with try and catch block in exception handling. The main feature of this keyword is that this is always implemented whether exception is thrown or not.
- What happens when a finally block has a return statement?
Any value that is returned by the corresponding try block is override by the return value.
- Is this possible to override a static method?
No, we cannot override a static method.
- Write the difference between Comparator and Comparable in Java?
Comparator | Comparable |
Describe custom order | Define natural order |
To define a custom order multiple comparators can be used | Always be one |
- Why multiple inheritances are not supported in java?
The compiler is confused when we extends two classes (let’s say A, B) by a class ‘C’ with the common method (let’s say show()) in both the classes.
So to achieve this concept you need to use interface because interfaces have only methods signature not code or logic. So when we implements two classes in one then only signature is copied. For example, if we take two interfaces A and B and class C implements both the interface then the code is written as below:
interface A {
public void show();
}
interface B {
public void show();
}
public class C implements A,B{
public static void main(String[] args) {
C object = new C();
object.show();
}
public void show() {
System.out.print(” This the show method”);
}
}
- Can we achieve multiple inheritances in java?
Yes, we can achieve this concept using Interface.
- What is the super keyword in java?
In java super keyword provide the reference the object of the immediate parent class. so super class can be used for:
- To refer immediate parent class instance variable.
- To invoke immediate parent class method and constructor.
- What is Method Overriding in Java?
In java, overriding process you can implements the super class method having same signature (means same name and argument or parameter) in the child class.
- Can we override a private method in Java?
No, we cannot override a private method in java because private methods are not visible in the other classes.
- Can we override static method in Java?
Static methods are the part of the class. They are not a part of object state so, we cannot override the static methods.
- How Inheritance can be implemented in java?
To implement inheritance you need to follow the below examples,
- Class A extends Class B
- Interface A extends Interface B
- Class A implements Interface B
- Why we need to use Inheritance?
In simple term inheritance is defined as ‘reusability of code’. When programmer write a logic then he never want to write the same logic everywhere (where required) again and again. So using inheritance they can reuse the code.
- Can a class extend itself?
No, class cannot extend to itself.
- How to restrict a member of a class from inheriting by its sub classes?
When programmer wants to restrict a member of a class from inheriting then simple he/she define that member to private. To define a member as private you need to use private access specifier.
- Explain covariant method overriding in Java.
Using this you can remove typecasting on the client-side. It also returns a subtype of the overridden method.
Above is the important Java Interview Questions & Answers.
Must Read – Classes and Interfaces