Java Interview Questions

Java Interview Questions

1. What is multithreading in Java, and how do you create a thread?

Answer: Multithreading is a Java feature that allows concurrent execution of two or more threads. It improves the performance of CPU-intensive tasks by utilizing multiple processors.

You can create a thread in two ways:
By extending the Thread class:

class MyThread extends Thread {
public void run() {
// Code for the thread
}
}
MyThread t = new MyThread();
t.start();


By implementing the Runnable interface:

class MyRunnable implements Runnable {
public void run() {
// Code for the thread
}
}
Thread t = new Thread(new MyRunnable());
t.start();


2. What is the significance of the static keyword in Java?

Answer:

Static Variable: A variable declared as static is shared among all instances of the class. It means that there is only one copy of the variable for all objects of that class.

Static Method: A method declared as static belongs to the class rather than to instances of the class. It can be called without creating an object of the class.

Static Block: A block of code that is executed once when the class is loaded into memory, often used for initialization.


3. What are the different types of exceptions in Java?

Answer:

Checked Exceptions: These are exceptions that are checked at compile-time, and the programmer must handle them using a try-catch block or declare them with throws. Examples include IOException, SQLException.

Unchecked Exceptions: These are exceptions that occur at runtime. These are not checked at compile-time, and the program may or may not handle them. Examples include NullPointerException, ArithmeticException.

Error: These are serious issues that typically can't be handled by the program, such as OutOfMemoryError, StackOverflowError.


4. What is the difference between String, StringBuilder, and StringBuffer in Java?

Answer:

String: It is immutable, meaning its value cannot be changed once created. Any operation on a String results in a new String object being created.

StringBuilder: It is mutable, meaning the value of the object can be modified. It is generally used for situations where a large number of string manipulations are needed. It is not thread-safe.

StringBuffer: It is similar to StringBuilder but is thread-safe. It is used when thread safety is required for string manipulation.


5. What are final, finally, and finalize in Java?

Answer:

final: It is a keyword used to define constants, prevent method overriding, or prevent class inheritance. For example:

final variable: Constant value that cannot be changed.
final method: Cannot be overridden by subclasses.
final class: Cannot be subclassed.
finally: It is a block that is used to ensure code execution after a try-catch block, regardless of whether an exception is thrown or not. It is used for cleanup tasks (e.g., closing files, database connections).

finalize(): It is a method in the Object class that is called by the garbage collector before an object is destroyed. It is generally used to perform cleanup actions, but its use is not recommended because it can lead to unreliable behavior.


6. What is the difference between ArrayList and LinkedList in Java?

Answer:

ArrayList: It is backed by a dynamic array. It provides fast access to elements via an index (O(1)), but insertion and removal of elements (especially in the middle of the list) are slower (O(n)), as it requires shifting elements.

LinkedList: It is backed by a doubly-linked list. Insertion and removal of elements are fast (O(1)) because there is no need to shift elements, but access time for elements is slower (O(n)), as it requires traversing the list.


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

Answer:

== (Reference Equality): This compares the memory addresses (references) of two objects. It checks whether two references point to the same object in memory.

.equals() (Object Equality): This method compares the actual content of the objects. By default, equals() checks for reference equality, but classes like String, List, etc., override this method to compare the values/content of the objects.


8. What are the four pillars of Object-Oriented Programming (OOP)?

Answer: The four main principles of OOP are:

Encapsulation: Bundling the data (variables) and the methods (functions) that operate on the data into a single unit (class), and restricting direct access to some of an object's components to protect its integrity.

Inheritance: A mechanism where a new class (subclass) inherits properties and behaviors (methods) from an existing class (superclass).

Polymorphism: The ability of an object to take on multiple forms. In Java, this can be achieved through method overloading (compile-time polymorphism) and method overriding (runtime polymorphism).

Abstraction: Hiding the complex implementation details and showing only the essential features of an object. It is achieved using abstract classes or interfaces.


9. What is the difference between JDK, JRE, and JVM?

Answer:

JVM (Java Virtual Machine): It is an abstract machine that provides an environment to run Java bytecode. It is responsible for converting bytecode into machine language so the program can run on any platform.

JRE (Java Runtime Environment): It provides libraries, Java Virtual Machine (JVM), and other resources necessary to run Java applications. It does not include development tools such as compilers or debuggers.

JDK (Java Development Kit): It includes the JRE and additional development tools, such as compilers (javac), debuggers, and other utilities for developing Java applications.


10. What is Java?

Answer: Java is a high-level, object-oriented programming language developed by Sun Microsystems (now owned by Oracle). It is designed to have as few implementation dependencies as possible, making it platform-independent through the use of the Java Virtual Machine (JVM). Java follows the "Write Once, Run Anywhere" philosophy, which means that once a program is written, it can run on any device that supports JVM, regardless of the underlying hardware or operating system.


Page 1 of 1