Java
Course Index
Index

Java Interview Questions

1. What is the difference between Parallel programming and Asynchronous programming

Asynchronous programming involves some calculations time-intensive tasks, which on the one hand are engaging a thread in the background but do not affect the normal flow of the program. Parallel programming incorporates several threads to perform a task faster.

2. What is the difference between == and equals() in Java

'==' compares the reference equality of two objects, while equals() compares the content equality of two objects. In other words, '==' checks whether two objects are the same instance, while equals() checks whether two objects have the same content.

3. What is the difference between HashMap and HashTable in Java

HashMap is not thread-safe and allows null values for both keys and values, while HashTable is thread-safe and does not allow null values for both keys and values. HashMap is generally faster than HashTable, but is not suitable for concurrent access without external synchronization.

4. What is a lambda expression in Java

A lambda expression is a concise way to represent a functional interface, which is an interface that contains only one abstract method. Lambda expressions are used to create anonymous functions that can be passed as arguments to methods or stored in variables.

5. What is the difference between an abstract class and an interface in Java

An abstract class can contain both abstract and non-abstract methods, while an interface can only contain abstract methods. An abstract class can also contain instance variables and constructors, while an interface can only contain constants and static methods.

6. What is the difference between 'final', 'finally', and 'finalize' in Java

final is a keyword that indicates that a variable or method cannot be modified or overridden. finally is a block that is executed after a try-catch block, regardless of whether an exception is thrown or not. finalize is a method that is called by the garbage collector before an object is destroyed, allowing it to perform any necessary cleanup operations.

7. What is the difference between a StringBuilder and a StringBuffer in Java

StringBuilder is not thread-safe and has better performance, while StringBuffer is thread-safe but has slightly worse performance. Both classes are used to create and modify strings.

8. What is the difference between a Stack and a Queue in Java

A Stack is a Last-In-First-Out (LIFO) data structure, while a Queue is a First-In-First-Out (FIFO) data structure. In other words, a Stack processes the most recent element added first, while a Queue processes the oldest element added first.

9. What is the difference between 'super()' and 'this()' in Java

super() is used to call the constructor of the superclass, while this() is used to call the constructor of the current class. Both are used to initialize the object's state, but super() is used to initialize the superclass's state, while this() is used to initialize the current class's state.

10. What is the difference between a HashSet and a TreeSet in Java

HashSet is an unordered collection that uses hashing to store elements, while TreeSet is an ordered collection that uses a red-black tree to store elements. HashSet has faster insertion and retrieval performance, while TreeSet has faster searching and sorting performance.

11. What is the difference between a Comparator and a Comparable in Java

Both Comparator and Comparable are used to compare objects, but Comparable is implemented by the object being compared, while Comparator is implemented separately. Comparable defines a natural ordering for a class, while Comparator allows for custom ordering.

12. What is the difference between a LinkedList and an ArrayList in Java

LinkedList is a doubly linked list data structure, while ArrayList is an array-based data structure. LinkedList has better performance for insertion and deletion operations, while ArrayList has better performance for random access operations.

13. What is the difference between static and final in Java

static is a keyword that indicates that a member variable or method belongs to the class itself, while final is a keyword that indicates that a variable cannot be changed after it has been initialized. static final is used to create constants in Java.

14. What is the difference between a try-catch block and a throws clause in Java

A try-catch block is used to catch and handle exceptions that may be thrown by a method, while a throws clause is used to declare that a method may throw a specific exception, but does not handle it itself. try-catch blocks provide a way to recover from exceptions, while throws clauses are used to propagate exceptions up the call stack.

15. What is JIT

The JIT (Just-In-Time) compiler is a part of the Java Virtual Machine (JVM) that helps improve the performance of Java programs.
Here’s how it works:

  • Java Code to Bytecode: When you write Java code, it's first compiled into an intermediate form called bytecode. This bytecode is platform-independent, meaning it can run on any machine that has a JVM.
  • Bytecode to Machine Code: The JVM doesn’t directly execute the bytecode. Instead, it uses the JIT compiler to convert the bytecode into machine code (code that your computer’s processor can understand and execute).
  • Compilation During Execution: The "just-in-time" part means that the bytecode is converted into machine code while the program is running, instead of all at once before the program starts. The JIT compiler watches which parts of the code are run frequently and compiles only those parts to machine code for faster execution.
  • Improved Performance: By compiling the frequently used parts of the program into machine code, the JIT compiler makes those parts run faster over time. So, as the program runs longer, it gets optimized and performs better.

16. What is JVM, JRE and JDK

  1. JVM (Java Virtual Machine):
    • What it is: The engine that runs Java programs.
    • Role: Converts bytecode into machine code (via JIT compiler) and executes it.
    • Think of it as: The machine that runs your Java programs.
  2. JRE (Java Runtime Environment):
    • What it is: A package that contains JVM + libraries and other components to run Java applications.
    • Role: Provides everything needed to run Java programs, but cannot compile them.
    • Think of it as: Everything you need to run Java programs (like the JVM and libraries).
  3. JDK (Java Development Kit):
    • What it is: A full toolkit that includes the JRE + tools like the compiler to develop Java programs.
    • Role: Allows you to write, compile, and run Java programs.
    • Think of it as: Everything you need to write and run Java programs (JRE + development tools).
Simple Comparison:
  • JVM: Runs Java programs
  • JRE: JVM + libraries to run Java programs
  • JDK: JRE + tools to develop and run Java programs

17. Explain different data types in Java

In Java, data types specify the type of data that a variable can hold. There are two main categories of data types: Primitive and Non-Primitive.

  1. Primitive Data Types:
    • byte : Stores small integers (-128 to 127)
    • short : Stores medium-sized integers (-32,768 to 32,767)
    • int : Stores large integers (-2^31 to 2^31 - 1)
    • long : Stores very large integers (-2^63 to 2^63 - 1)
    • float : Stores single-precision floating point numbers (decimals)
    • double : Stores double-precision floating point numbers (more precision for decimals)
    • char : Stores a single character (like 'A', 'b', '9')
    • boolean : Stores true or false values
  2. Non-Primitive Data Types (Reference Types):
    • String: Stores a sequence of characters (e.g., "Hello World").
    • Arrays: Stores multiple values of the same type (e.g., int[] numbers = {1, 2, 3}).
    • Objects: Instances of classes that can hold multiple properties and methods.

18. What are the differences between String, StringBuffer and StringBuilder

  1. String
    • Immutable: Once created, cannot be changed.
    • Use case: When you don't need to modify the value of the string.
    • Performance: Slower when performing many changes (like concatenation) because a new object is created each time.
  2. StringBuilder
    • Mutable: Can be changed, just like StringBuffer.
    • Not thread-safe: Not synchronized, so not safe for use in multi-threaded environments.
    • Use case: When you need to modify strings often, but thread safety is not a concern.
    • Performance: Faster than StringBuffer since there’s no synchronization overhead.
  3. StringBuffer
    • Mutable: Can be changed (append, insert, etc.) without creating a new object.
    • Thread-safe: Synchronized, meaning it can be safely used in multi-threaded environments.
    • Use case: When you need to modify strings often, and thread safety is important.
    • Performance: Slower than StringBuilder due to synchronization overhead.

19. What is a jagged array

A jagged array in Java is an array of arrays, where the inner arrays can have different lengths. In other words, it's like a 2D array where the rows (sub-arrays) don't have to be the same size.

20. What is the difference between new and newInstance

  1. new Keyword:
    • Purpose: Used to create a new object directly by calling the class constructor.
    • Syntax: ClassName object = new ClassName();
    • Compile-time check: The constructor must be known at compile time, and the class must be available.
  2. newInstance() Method:
    • Purpose: Used to create an object dynamically (at runtime) using reflection.
    • Syntax: ClassName object = ClassName.class.newInstance();
    • Runtime exception: Can throw exceptions like InstantiationException and IllegalAccessException.
    • No constructor arguments: The class must have a no-arg constructor (before Java 9) when using newInstance().