A quick reference guide for coding interviews.


🔹 1. Basics

Data Types

  • Primitives → byte, short, int, long, float, double, char, boolean
  • Non-primitive → String, Arrays, Objects

Syntax

class Main {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

Input/Output

import java.util.Scanner;
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
System.out.println(x);

🔹 2. Control Structures

  • If-Else, Switch
  • Loops → for, while, do-while, enhanced-for

for (int x : arr) {
    System.out.println(x);
}

🔹 3. Methods

int add(int a, int b) {
    return a + b;
}
  • Overloading → same name, diff params
  • Overriding → subclass redefines parent method
  • Static methods → belong to class, not object

🔹 4. Memory & Garbage Collection

  • Stack → local variables, method calls
  • Heap → objects, arrays
  • Garbage Collector reclaims unused objects
  • System.gc(); → request GC (not guaranteed)


🔹 5. References

  • Java uses references, not pointers

String s = new String("Hello");
String t = s; // both point to same object

🔹 6. OOP Essentials

Encapsulation

class Account {
    private int balance;
    public void deposit(int amt) { balance += amt; }
}

Inheritance

class Dog extends Animal {}

Polymorphism

  • Overloading → compile-time
  • Overriding → runtime

Abstraction

abstract class Shape { abstract void draw(); }
interface Drawable { void draw(); }

Important Keywords

  • final → constant, no override/inheritance
  • static → class-level
  • this → current object
  • super → parent object
  • new → object creation

🔹 7. Collections Framework

  • ListArrayList, LinkedList
  • SetHashSet, TreeSet
  • MapHashMap, TreeMap

ArrayList<Integer> list = new ArrayList<>();
list.add(10);
System.out.println(list.get(0));

🔹 8. Strings

  • Immutable → String s = "abc";
  • Mutable → StringBuilder, StringBuffer

StringBuilder sb = new StringBuilder("Hi");
sb.append(" Java");

🔹 9. Exceptions

try {
    int x = 5/0;
} catch (ArithmeticException e) {
    System.out.println("Error: " + e);
} finally {
    System.out.println("Cleanup");
}

  • Checked (compile-time) → IOException, SQLException
  • Unchecked (runtime) → NullPointerException, ArithmeticException


🔹 10. Multithreading

class MyThread extends Thread {
    public void run() {
        System.out.println("Running...");
    }
}
new MyThread().start();
  • Runnable alternative → new Thread(() -> {...}).start();
  • synchronized → lock critical section
  • volatile → ensures visibility across threads


🔹 11. Common Libraries

  • java.util → Scanner, Collections, Date
  • java.lang → String, Math, Object
  • java.io → File handling
  • java.sql → Database

🔹 12. Interview Q&A Shortcuts

👉 final vs finally vs finalize

  • final → keyword

  • finally → exception block

  • finalize() → called before GC removes object

👉 String vs StringBuilder

  • String → immutable

  • StringBuilder → mutable, faster

👉 == vs equals()

  • == → reference comparison

  • equals() → content comparison

👉 Interface vs Abstract Class

  • Interface → full abstraction (Java 8+: default + static methods)

  • Abstract → partial abstraction

👉 Pass by value/reference

  • Always pass-by-value (object reference value is copied)


✅ Pro Interview Tips

  • Always explain time & space complexity.

  • Be fluent with Strings, Arrays, HashMaps, OOP design.

  • Write clean, structured code with comments.


⚡ High-Yield Practice

  • Reverse a string / check palindrome

  • Find duplicates in array using HashSet

  • LRU cache (LinkedHashMap)

  • Producer-Consumer with threads

  • Difference: HashMap vs ConcurrentHashMap