Skip to content

Java Core Fundamentals Interview Q&A

== behaves differently depending on the type:

  • For primitive types, it compares values.
  • For objects, it compares references (memory addresses).

equals() compares logical equality (content), provided the class overrides it.

String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2); // false
System.out.println(s1.equals(s2)); // true
  • Object.equals() behaves like == unless overridden.
  • String overrides equals() to compare characters.
  • Mention String Pool behavior:
    • "abc" == "abc" is true because of interning.
    • new String("abc") == new String("abc") is false.

The JVM runtime memory consists of:

  • Heap
  • Stack
  • Metaspace
  • Program Counter Register
  • Native Method Stack
flowchart TD
    JVM --> Heap
    JVM --> Stack
    JVM --> Metaspace
    JVM --> PC["Program Counter"]
    JVM --> Native["Native Method Stack"]
  • Objects
  • Arrays
  • String Constant Pool
  • Young Generation
  • Old Generation
  • Method frames
  • Local variables
  • Object references

Mention Young Generation, Old Generation, and Metaspace replacing PermGen (Java 8).

Reasons:

  • String Pool optimization
  • Thread safety
  • Security
  • HashCode caching

Methods like concat() create new objects rather than mutating the original.

Feature ArrayList LinkedList
Access O(1) O(n)
Insert/Delete O(n) O(1) (known node)
Memory Less More
Random Access Yes No

Use ArrayList for read-heavy workloads.

Feature HashMap Hashtable
Thread Safe No Yes
Null Keys One None
Null Values Many None

Prefer ConcurrentHashMap over Hashtable.

  • final
    • variable cannot be reassigned
    • method cannot be overridden
    • class cannot be extended
  • finally
    • always executes after try/catch
    • used for resource cleanup
  • finalize()
    • deprecated
    • avoid using

Checked Exceptions:

  • IOException
  • SQLException

Unchecked Exceptions:

  • NullPointerException
  • ArrayIndexOutOfBoundsException

Use try-with-resources whenever possible.

Abstract Class Interface
Constructors No constructors
State allowed Constants only
Single inheritance Multiple inheritance

Java 8+ interfaces support default and static methods.

Must contain exactly one abstract method.

Examples: Predicate, Consumer, Supplier, Function, BiFunction.

@FunctionalInterface
interface Calculator {
int add(int a, int b);
}

Guarantees:

  • Visibility
  • Prevents instruction reordering

Does NOT guarantee:

  • Atomicity
  • == compares references for objects; equals() compares content — but only if the class overrides it. String literals share a pool, so == can be misleadingly true for literals but false for new String(...).
  • The heap holds objects/arrays (further split into Young/Old generation); the stack holds method frames and local variables; Metaspace replaced PermGen in Java 8.
  • ArrayList wins on random access (O(1)); LinkedList wins on insertion/deletion at a known node (O(1)) — choose based on the access pattern, not habit.
  • volatile guarantees visibility and ordering across threads but never atomicity — count++ on a volatile field is still a race condition.