Interview Q&A / Java
Java interview questions
Collections, the JVM, strings, exceptions and threads, the backbone of most campus placement rounds.
Prefer rapid revision? Practise Java short interview answers →
- What is the difference between checked and unchecked exceptions in Java?
Checked exceptions (subclasses of Exception other than RuntimeException) must be caught or declared with throws, and the compiler enforces this. Unchecked exceptions (RuntimeException and its subclasses, plus Errors) are not checked at compile time and usually signal programming bugs.
BeginnerIn-depth3 min read - What is the difference between final, finally and finalize in Java?
final is a keyword that stops change (a variable cannot be reassigned, a method cannot be overridden, a class cannot be extended). finally is a block that runs after try/catch, usually for cleanup. finalize() was a method the garbage collector might call before removing an object; it is deprecated for removal and should not be used.
BeginnerIn-depth3 min read - Can a try block exist without a catch block in Java?
Yes, as long as it has either a finally block or resources in try-with-resources. A bare try with neither is a compile error.
BeginnerQuick answer1 min read - How do you create a custom exception in Java?
Extend Exception for a checked custom exception or RuntimeException for an unchecked one, and add constructors that pass the message and optionally a cause to the parent. Use it to express a business rule clearly.
BeginnerQuick answer1 min read - What is the difference between Error and Exception in Java?
Both extend Throwable. An Exception represents a problem a program can reasonably catch and handle, while an Error such as OutOfMemoryError signals a serious JVM-level failure that applications normally should not try to recover from.
BeginnerQuick answer1 min read - What is the difference between throw and throws in Java?
throw is a statement that actually raises one exception object at that moment. throws is part of a method signature that warns callers which checked exceptions the method might pass on.
BeginnerQuick answer1 min read - What is try-with-resources in Java and why is it better than try-finally?
It declares AutoCloseable resources in the try parentheses and closes them automatically, in reverse order, even when an exception occurs. It is shorter than try-finally and keeps the original exception when close() also fails.
BeginnerQuick answer1 min read - In what order should catch blocks be written, and what is multi-catch in Java?
Catch blocks are checked top to bottom, so subclasses must come before their parents or the code will not compile. Multi-catch uses a pipe to handle several unrelated exceptions in one block.
IntermediateQuick answer1 min read - What does a Java method return when both try and finally contain a return?
The finally block runs before the method actually returns, and a return inside finally overrides the earlier one, so the finally value wins. If finally only changes a variable, a primitive value already chosen for return is not affected.
IntermediateQuick answer1 min read - What is exception chaining in Java and why keep the cause?
Exception chaining wraps a low-level exception inside a higher-level one using the cause argument, so callers get a meaningful message and the stack trace still shows the original failure through getCause().
IntermediateQuick answer1 min read