Java Interview Questions for Freshers
The short answer
Java interviews test the core ideas. The 10 questions here cover JDK, JRE and JVM, == versus equals(), abstract classes versus interfaces, overloading versus overriding, the string pool and exceptions. Each comes with a note on what a good answer includes.
10 questions. Open one to see what a good answer covers.
What is the difference between JDK, JRE and JVM?
The JVM runs compiled bytecode, the JRE is the JVM plus the standard libraries needed to run programs, and the JDK adds the compiler and tools needed to develop them.
What is the difference between == and equals() in Java?
== compares references for objects, meaning whether they are the same object. equals() compares content when a class overrides it, as String does. Compare strings with equals().
What is the difference between an abstract class and an interface?
An abstract class can hold state and partly implemented methods and a class extends only one. An interface describes a contract and a class can implement many. Since Java 8 interfaces can have default methods.
What is the difference between method overloading and overriding?
Overloading is the same method name with different parameters in one class, decided at compile time. Overriding is a subclass giving its own version of a parent's method, decided at run time.
Why are strings immutable in Java, and what is the string pool?
Immutability makes strings safe to share, cache and use as keys. The string pool stores one copy of each literal, so equal literals can share memory. Use StringBuilder when you change text a lot.
What are checked and unchecked exceptions?
Checked exceptions must be handled or declared, such as IOException. Unchecked ones extend RuntimeException, such as NullPointerException, and the compiler does not force handling them.
How does a HashMap work internally?
It uses the key's hashCode to pick a bucket and equals to find the key inside it. Collisions go into a list, which becomes a tree when it grows long in Java 8 and later. It resizes as it fills.
What is the difference between ArrayList and LinkedList?
ArrayList uses an array, so reading by index is fast and inserting in the middle is slow. LinkedList is fast at inserting and removing at known positions but slow at reading by index.
What is garbage collection in Java?
The JVM automatically frees memory of objects that nothing refers to any more. You cannot force it, only suggest it, and good code avoids keeping references it no longer needs.
What do final, finally and finalize mean?
final marks a variable, method or class that cannot change or be overridden. finally is a block that always runs after try and catch. finalize was a method called before garbage collection and is now deprecated.
Everything runs in your browser. What you type is not sent anywhere.
How to: Java Interview Questions for Freshers
- 1Read the question and say your answer aloud first.
- 2Open the tip and see which points you missed.
- 3Write a three-line code example for the ones that need one.
- 4Practise again in practice mode with the timer.
At a glance
- Questions
- 10 Java questions
- Includes
- JVM, strings, OOP, exceptions
- Time to aim for
- About 60 seconds each
Frequently asked questions
Why are strings immutable in Java?
Immutable strings are safe to share, can be cached in the string pool, and can be used as keys in maps without changing. The bank has a note on how to explain this clearly.
What is the difference between checked and unchecked exceptions?
Checked exceptions must be handled or declared by the compiler's rules, while unchecked ones happen at run time and need not be. The tip explains with examples you can quote.
More quick answers
- HR Interview Questions for Freshers: Tell Me About Yourself
- Behavioural Interview Questions and the STAR Method
- Python Interview Questions for Freshers
- SQL Interview Questions for Freshers
- Data Structures and Algorithms Interview Questions for Freshers
- Web Development Interview Questions for Freshers
- CS Fundamentals Interview Questions: OOP, OS and Networks
- Excel and Data Analysis Interview Questions for Freshers