Core Java Interview Question
On this page20
- 1What are the main features of Java?
- 2What is the difference between JDK, JRE, and JVM?
- 3What is the difference between == and equals() in Java?
- 4What is the difference between String, StringBuilder, and StringBuffer?
- 5What are the access modifiers in Java?
- 6What is the difference between final, finally, and finalize()?
- 7What is the difference between Array and ArrayList?
- 8What is the difference between HashMap and Hashtable?
- 9What is the difference between Abstract Class and Interface?
- 10What is the difference between throw and throws?
- 11What is the difference between checked and unchecked exceptions?
- 12What is the difference between static and instance variables?
- 13What is the difference between overloading and overriding?
- 14What is the purpose of the transient keyword?
- 15What is the purpose of the synchronized keyword?
- 16What is the difference between wait(), notify(), and notifyAll()?
- 17What is the difference between Comparable and Comparator?
- 18What is the purpose of the volatile keyword?
- 19What is the difference between HashSet and TreeSet?
- 20What is the purpose of the default keyword in interfaces (Java 8)?
1. What are the main features of Java?
- Object-Oriented: Everything in Java is an object.
- Platform-Independent: Java code is compiled into bytecode, which can run on any platform with a JVM.
- Robust: Java has strong memory management, exception handling, and garbage collection.
- Secure: Java provides a secure runtime environment with features like bytecode verification and no explicit pointer usage.
- Multithreaded: Java supports concurrent execution of threads.
- High Performance: Java uses Just-In-Time (JIT) compilers for better performance.
2. What is the difference between JDK, JRE, and JVM?
- JDK (Java Development Kit): A development environment that includes tools like the compiler (
javac), debugger, and JRE. - JRE (Java Runtime Environment): Provides the libraries and JVM required to run Java applications.
- JVM (Java Virtual Machine): Executes Java bytecode and provides platform independence.
3. What is the difference between == and equals() in Java?
- == compares references (memory addresses) for objects and values for primitives.
- equals() compares the content of objects. By default, it behaves like ==, but it can be overridden in classes like String.
Example:
Strings1=newString("Hello");
Strings2=newString("Hello");
System.out.println(s1 == s2); // false (different references)
System.out.println(s1.equals(s2)); // true (same content)
4. What is the difference between String, StringBuilder, and StringBuffer?
- String: Immutable; every modification creates a new object.
- StringBuilder: Mutable and not synchronized; faster for single-threaded operations.
- StringBuffer: Mutable and synchronized; thread-safe but slower than StringBuilder.
5. What are the access modifiers in Java?
- Public: Accessible from anywhere.
- Protected: Accessible within the same package and subclasses.
- Default (no modifier): Accessible within the same package.
- Private: Accessible only within the same class.
6. What is the difference between final, finally, and finalize()?
- final: A keyword used to declare constants, prevent method overriding, or inheritance.
- finally: A block in exception handling that always executes, regardless of whether an exception is thrown.
- finalize(): A method called by the garbage collector before an object is destroyed (deprecated in modern Java).
7. What is the difference between Array and ArrayList?
- Array: Fixed size, can store primitives and objects.
- ArrayList: Dynamic size, can only store objects, and provides utility methods like add(), remove(), etc.
8. What is the difference between HashMap and Hashtable?
- HashMap: Not synchronized, allows one null key and multiple null values.
- Hashtable: Synchronized, does not allow null keys or values.
9. What is the difference between Abstract Class and Interface?
- Abstract Class:
- Can have abstract and concrete methods.
- Supports constructors.
- Can have instance variables.
- Interface:
- All methods are abstract (until Java 8, which introduced default and static methods).
- Cannot have constructors.
- Only constants (static final variables) are allowed.
10. What is the difference between throw and throws?
- throw: Used to explicitly throw an exception.
- throws: Declares exceptions that a method might throw.
Example:
voidmethod() throws IOException {
thrownewIOException("Exception thrown");
}
11. What is the difference between checked and unchecked exceptions?
- Checked Exceptions: Must be handled or declared using throws (e.g., IOException, SQLException).
- Unchecked Exceptions: Runtime exceptions that do not need to be declared or handled (e.g., NullPointerException, ArithmeticException).
12. What is the difference between static and instance variables?
- Static Variables: Shared across all instances of a class; belong to the class.
- Instance Variables: Unique to each object; belong to the instance.
13. What is the difference between overloading and overriding?
- Overloading: Same method name, different parameter lists (compile-time polymorphism).
- Overriding: Subclass provides a specific implementation of a method in the parent class (runtime polymorphism).
14. What is the purpose of the transient keyword?
The transient keyword is used to mark a variable that should not be serialized.
15. What is the purpose of the synchronized keyword?
The synchronized keyword ensures that only one thread can access a block of code or method at a time, preventing race conditions.
16. What is the difference between wait(), notify(), and notifyAll()?
- wait(): Causes the current thread to wait until another thread calls notify() or notifyAll() on the same object.
- notify(): Wakes up one thread waiting on the object's monitor.
- notifyAll(): Wakes up all threads waiting on the object's monitor.
17. What is the difference between Comparable and Comparator?
- Comparable: Used to define natural ordering of objects. Implemented using compareTo().
- Comparator: Used to define custom ordering. Implemented using compare().
18. What is the purpose of the volatile keyword?
The volatile keyword ensures that changes to a variable are immediately visible to all threads. It prevents caching of the variable by threads.
19. What is the difference between HashSet and TreeSet?
- HashSet: Unordered, allows null, and uses a hash table for storage.
- TreeSet: Ordered (sorted), does not allow null, and uses a red-black tree for storage.
20. What is the purpose of the default keyword in interfaces (Java 8)?
The default keyword allows interfaces to have concrete methods with a default implementation. This helps in adding new methods to interfaces without breaking existing implementations.
