
Jobs Bangalore and Chennai 🏢
March 1, 2025 at 08:13 AM
Here are some basic Java interview questions and answers:
1. What is Java?
Answer:
Java is a high-level, object-oriented programming language developed by Sun Microsystems (now owned by Oracle). It is platform-independent due to its "Write Once, Run Anywhere" (WORA) capability, thanks to the Java Virtual Machine (JVM).
2. What are the main features of Java?
Answer:
Platform Independent – Runs on any operating system with JVM.
Object-Oriented – Follows OOP concepts like inheritance, encapsulation, and polymorphism.
Secure – Provides security features like bytecode verification and no explicit pointers.
Multithreaded – Allows concurrent execution of multiple threads.
Robust – Handles memory management and exceptions effectively.
High Performance – Uses Just-In-Time (JIT) compiler for better performance.
3. What is JVM, JRE, and JDK?
Answer:
JVM (Java Virtual Machine) – Converts Java bytecode into machine code to run on different platforms.
JRE (Java Runtime Environment) – Contains JVM and libraries required to run Java applications.
JDK (Java Development Kit) – Includes JRE, compiler (javac), and tools needed for Java development.
4. What is the difference between Primitive and Non-Primitive Data Types?
Answer:
Primitive Data Types Non-Primitive Data Types
Predefined by Java Created by programmers (e.g., Classes, Arrays)
Stores actual value Stores reference to an object
Memory efficient Requires more memory
Example: int, char, float, boolean Example: String, Array, Object
5. What are OOP principles in Java?
Answer:
Encapsulation – Wrapping data and methods inside a class.
Inheritance – Acquiring properties of one class in another (using extends).
Polymorphism – Multiple forms of the same method (Method Overloading and Overriding).
Abstraction – Hiding implementation details using abstract classes and interfaces.
6. What is the difference between == and .equals()?
Answer:
== compares memory references (checks if two variables refer to the same object).
.equals() checks the actual content of objects (especially for Strings).
java
Copy
Edit
String s1 = new String("Java");
String s2 = new String("Java");
System.out.println(s1 == s2); // false (different memory locations)
System.out.println(s1.equals(s2)); // true (same content)
7. What is the difference between Array and ArrayList?
Answer:
Array ArrayList
Fixed size Dynamic size (resizable)
Can store both primitives and objects Stores only objects
Performance is better Uses extra memory for resizing
Example of Array:
java
Copy
Edit
int[] arr = new int[5];
Example of ArrayList:
java
Copy
Edit
ArrayList list = new ArrayList<>();
8. What are Constructors in Java?
Answer:
A constructor is a special method used to initialize objects. It has the same name as the class and does not have a return type.
Example:
java
Copy
Edit
class Example {
int x;
Example() { // Constructor
x = 10;
}
}
9. What is the difference between "final", "finally", and "finalize"?
Answer:
Keyword Description
final Used to declare constants, prevent method overriding, or class inheritance.
finally A block that always executes after try-catch (used in exception handling).
finalize() A method called by the garbage collector before destroying an object.
10. What is Exception Handling in Java?
Answer:
Exception Handling is used to handle runtime errors to maintain normal program flow. Java provides try, catch, finally, throw, and throws keywords for handling exceptions.
Example:
java
Copy
Edit
try {
int a = 10 / 0; // ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
} finally {
System.out.println("This block always executes");
}
11. What is the difference between Checked and Unchecked Exceptions?
Answer:
Checked Exception Unchecked Exception
Checked at compile-time Checked at runtime
Must be handled using try-catch Can be avoided by writing safe code
Example: IOException, SQLException Example: NullPointerException, ArithmeticException
12. What is the difference between Interface and Abstract Class?
Answer:
Abstract Class Interface
Can have both abstract and non-abstract methods Only abstract methods (before Java 8)
Can have constructors No constructors
Uses extends keyword Uses implements keyword
Example: abstract class Animal {} Example: interface Animal {}
13. What is Multithreading in Java?
Answer:
Multithreading allows multiple threads to run concurrently, improving performance. Java provides the Thread class and Runnable interface for implementing multithreading.
Example using Thread class:
java
Copy
Edit
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running");
}
}
MyThread t1 = new MyThread();
t1.start();
Example using Runnable interface:
java
Copy
Edit
class MyRunnable implements Runnable {
public void run() {
System.out.println("Thread is running");
}
}
Thread t2 = new Thread(new MyRunnable());
t2.start();
14. What is the difference between HashMap and HashSet?
Answer:
HashMap HashSet
Stores key-value pairs Stores only unique values
Allows one null key Allows only one null value
Uses put() method Uses add() method
👍
2