Java Basics Interview Questions

Let’s explore the fundamental concepts that form the foundation of Java development.

What is Java?

Java is a high-level, class-based, object-oriented programming language designed to be platform-independent. It follows the “Write Once, Run Anywhere” (WORA) philosophy, meaning that compiled Java code can run on all platforms that support Java without the need for recompilation.

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

  • JDK (Java Development Kit): Contains tools for developing Java applications, including the JRE and development tools like compilers and debuggers.
  • JRE (Java Runtime Environment): Contains the JVM and libraries needed to run Java applications.
  • JVM (Java Virtual Machine): An abstract machine that provides a runtime environment in which Java bytecode can be executed.

What are the main features of Java?

  • Platform Independent: Java code can run on any platform that has a JVM.
  • Object-Oriented: Everything in Java is an object, which helps in organizing code.
  • Secure: Java’s security features help protect against viruses and malicious code.
  • Robust: Strong type checking and exception handling make Java programs reliable.
  • Multithreaded: Java supports multithreading, allowing concurrent execution of two or more threads.

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

  • == compares object references (memory addresses) for objects and values for primitives.
  • .equals() is a method that compares the contents of objects. By default, it behaves like ==, but it can be overridden to provide custom comparison logic.

Example:

String str1 = new String("hello");
String str2 = new String("hello");
System.out.println(str1 == str2); // false (different objects)
System.out.println(str1.equals(str2)); // true (same content)

What is the difference between String, StringBuilder, and StringBuffer?

  • String: Immutable, thread-safe, but inefficient for string manipulation.
  • StringBuilder: Mutable, not thread-safe, efficient for string manipulation.
  • StringBuffer: Mutable, thread-safe, efficient for string manipulation but slightly slower than StringBuilder due to synchronization.

What is the difference between an abstract class and an interface?

  • Abstract Class: Can have abstract methods (methods without implementation) and concrete methods (methods with implementation). A class can extend only one abstract class.
  • Interface: Can only have abstract methods (in Java 8 and earlier) or default/static methods (in Java 8 and later). A class can implement multiple interfaces.

What is the difference between final, finally, and finalize?

  • final: A keyword used to restrict the user. It can be applied to variables, methods, and classes. A final variable cannot be reassigned, a final method cannot be overridden, and a final class cannot be extended.
  • finally: A block used with try-catch to ensure that a section of code is always executed, regardless of whether an exception is thrown or not.
  • finalize: A method called by the garbage collector before an object is garbage collected. It can be overridden to perform cleanup operations.

What is the difference between HashMap and HashTable?

  • HashMap: Not synchronized, allows null keys and values, generally faster than HashTable.
  • HashTable: Synchronized, does not allow null keys or values, thread-safe but slower than HashMap.

What is the difference between ArrayList and LinkedList?

  • ArrayList: Backed by a dynamic array, good for random access and iteration, but inefficient for insertion and deletion in the middle.
  • LinkedList: Backed by a doubly-linked list, good for insertion and deletion in the middle, but inefficient for random access and iteration.

What is the difference between checked and unchecked exceptions?

  • Checked Exceptions: Exceptions that are checked at compile time. They must be either caught or declared to be thrown. Examples include IOException, SQLException.
  • Unchecked Exceptions: Exceptions that are not checked at compile time. They do not need to be caught or declared to be thrown. Examples include NullPointerException, ArrayIndexOutOfBoundsException.

Share & Connect