DEV Community

Vivek Nishant
Vivek Nishant

Posted on • Edited on

Core Java Interview Questions

Ultimate Guide to Java Interview Questions

If you're preparing for a Java interview, mastering these questions will give you a strong foundation. This post covers all critical Java topics, from Core Java concepts to Advanced topics, including coding challenges. Let's dive in!

Here’s a detailed explanation of the requested topics:


Core Java Basics

Key Features of Java:

  1. Platform Independent: Java code runs on any platform using the JVM.
  2. Object-Oriented: Encourages encapsulation, inheritance, and polymorphism.
  3. Simple and Easy to Learn: Syntax similar to C++ but without complexity like pointers.
  4. Secure: Provides runtime checking, bytecode verification, and a security manager.
  5. Robust: Exception handling and garbage collection help avoid errors.
  6. Multithreaded: Supports concurrent programming with built-in thread management.
  7. High Performance: Uses Just-In-Time (JIT) compiler.
  8. Distributed and Networked: Simplifies distributed system development using APIs.
  9. Dynamic and Extensible: Adapts to evolving requirements through dynamic linking.

Difference Between JDK, JRE, and JVM:

  1. JDK (Java Development Kit): Tools for developing and debugging Java programs, including JRE and compilers.
  2. JRE (Java Runtime Environment): Executes Java applications; includes JVM and libraries.
  3. JVM (Java Virtual Machine): Runs Java bytecode; ensures platform independence.

Difference Between == and .equals():

  • ==: Compares references (memory locations) for objects.
  • .equals(): Compares content or state of objects. Can be overridden.

Class and Object in Java:

  • Class: Blueprint defining the properties (fields) and behaviors (methods).
  • Object: Instance of a class, representing a real-world entity.

public static void main(String[] args) Explained:

  • public: Accessible from anywhere.
  • static: No need to instantiate the class.
  • void: No return value.
  • main: Entry point of the program.
  • String[] args: Command-line arguments.

Difference Between String, StringBuilder, and StringBuffer:

  1. String: Immutable; thread-safe.
  2. StringBuilder: Mutable; not thread-safe but faster.
  3. StringBuffer: Mutable; thread-safe with synchronized methods.

Access Modifiers:

  1. Private: Visible within the class.
  2. Default: Visible within the package.
  3. Protected: Visible within the package and subclasses.
  4. Public: Visible everywhere.

Difference Between final, finally, and finalize:

  • final: Used to declare constants, prevent inheritance, or override.
  • finally: Block to ensure cleanup after a try block.
  • finalize(): Method invoked by the garbage collector before destroying an object.

Object-Oriented Programming (OOP)

Four Pillars of OOP:

  1. Encapsulation: Restricts access to internal details. Example: Private fields with public getters/setters.
  2. Abstraction: Hides complexity using interfaces or abstract classes. Example: List interface.
  3. Inheritance: Reuse code by extending parent classes. Example: class Dog extends Animal.
  4. Polymorphism: Behavior changes based on the object. Example: Method overriding.

Difference Between Abstraction and Encapsulation:

  • Abstraction: Focuses on "what" an object does.
  • Encapsulation: Focuses on "how" the data is protected.

Method Overloading vs. Overriding:

  • Overloading: Same method name, different parameters.
  • Overriding: Subclass modifies a parent class method.

Interface vs. Abstract Class:

  • Interface: 100% abstraction; methods are implicitly abstract.
  • Abstract Class: Can have abstract and concrete methods.

Multiple Interfaces: A class can implement multiple interfaces using the implements keyword.


Constructor Overloading Example:

class Example {
    Example() { }
    Example(int a) { }
    Example(String s) { }
}
Enter fullscreen mode Exit fullscreen mode

super and this:

  • this: Refers to the current object.
  • super: Refers to the parent class object.

Collections and Data Structures

ArrayList vs. LinkedList:

  • ArrayList: Faster for random access.
  • LinkedList: Faster for insertions/deletions.

HashMap, LinkedHashMap, TreeMap:

  • HashMap: Unordered, fastest.
  • LinkedHashMap: Maintains insertion order.
  • TreeMap: Sorted order.

Significance of hashCode() and equals() in HashMap:

  • Ensures proper key-value mapping by preventing duplicates.

Iterator vs. ListIterator:

  • Iterator: Traverses in one direction.
  • ListIterator: Traverses in both directions.

Fail-Fast vs. Fail-Safe:

  • Fail-Fast: Concurrent modification triggers ConcurrentModificationException.
  • Fail-Safe: Works on a copy, no exception.

Internal Working of HashMap: Uses buckets for storage, hashing for key-value mapping, and linked lists to resolve collisions.


Multithreading and Concurrency

Creating Threads in Java:

  • Extending Thread:
  class MyThread extends Thread { public void run() { } }
Enter fullscreen mode Exit fullscreen mode
  • Implementing Runnable:
  class MyRunnable implements Runnable { public void run() { } }
Enter fullscreen mode Exit fullscreen mode

synchronized Method vs. Block:

  • Method: Locks entire method.
  • Block: Locks a specific section.

wait(), notify(), notifyAll():

  • Used for inter-thread communication.

Volatile Keyword: Ensures visibility of changes across threads.


Exception Handling

Checked vs. Unchecked Exceptions:

  • Checked: Caught during compile-time (IOException).
  • Unchecked: Occurs during runtime (ArithmeticException).

Custom Exception Example:

class MyException extends Exception { MyException(String msg) { super(msg); } }
Enter fullscreen mode Exit fullscreen mode

Java 8+ Features

Lambda Expression:

(numbers) -> numbers.stream().mapToInt(Integer::intValue).sum();
Enter fullscreen mode Exit fullscreen mode

Need a deeper dive into specific areas? Let me know!
Common Coding Questions

  • Write a program to check if a string is a palindrome.
  • Write a program to find the second-largest element in an array.
  • Write a program to reverse a linked list.
  • Write a program to implement a thread-safe Singleton class.
  • Write a program to find the frequency of characters in a string.
  • Write a program to demonstrate deadlock and resolve it.
  • Write a program to check if two strings are anagrams.

Top comments (0)