CTCaliberTek
Interview Questions

Java Core and OOP Interview Questions and Answers

16 core Java questions: platform independence, strings, equals and hashCode, OOP principles, exceptions, and memory.

  1. 1. Why is Java called platform independent?

    The compiler turns source code into bytecode, not machine code. Any machine with a compatible JVM can run that bytecode, and the JVM handles the differences between operating systems. So the same compiled class files run on Windows, Linux, and macOS. The JVM itself is platform specific.

  2. 2. What is the difference between primitive types and wrapper classes? What is autoboxing?

    Primitives such as int and boolean hold raw values and cannot be null. Wrapper classes such as Integer and Boolean are objects, so they can be null and be used in collections and generics. Autoboxing is the automatic conversion between them. Be careful: unboxing a null wrapper throws a NullPointerException, and boxing in tight loops creates extra objects.

  3. 3. Why are Strings immutable, and what is the String pool?

    A String cannot change after it is created. This makes strings safe to share between threads, safe to use as HashMap keys, and allows the JVM to cache their hash code and reuse literals. The string pool is a cache of string literals, so two identical literals refer to the same object.

    String a = "java";
    String b = "java";
    String c = new String("java");
    System.out.println(a == b);       // true, same pooled object
    System.out.println(a == c);       // false, different object
    System.out.println(a.equals(c));  // true, same content
  4. 4. What is the difference between String, StringBuilder, and StringBuffer?

    String is immutable, so every change creates a new object. StringBuilder is a mutable, non-synchronized buffer and is the right choice for building strings in loops on a single thread. StringBuffer is the older synchronized version, which is slower and rarely needed because sharing one builder across threads is unusual.

  5. 5. What is the difference between == and equals(), and what is the equals and hashCode contract?

    == compares references for objects (and values for primitives). equals() compares logical equality as defined by the class. The contract: if two objects are equal according to equals(), they must return the same hashCode(). Equal hash codes do not require equal objects. Breaking the contract makes objects get lost in HashMap and HashSet. Override both together.

  6. 6. What are the four pillars of object-oriented programming?

    Encapsulation hides state behind methods and access modifiers. Inheritance lets a class reuse and extend another. Polymorphism lets one interface have many implementations, chosen at runtime for overridden methods. Abstraction exposes what an object does while hiding how, through abstract classes and interfaces.

  7. 7. What is the difference between method overloading and overriding?

    Overloading means several methods with the same name but different parameter lists in the same class, resolved at compile time. Overriding means a subclass provides its own implementation of an inherited method with the same signature, resolved at runtime. An overriding method can widen access but not narrow it, and use @Override so the compiler checks it.

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

    An abstract class can have fields, constructors, and both abstract and concrete methods, and a class can extend only one. An interface defines a contract that a class can implement many of; since Java 8 it can have default and static methods, and since Java 9 private methods, but no instance state. Use an abstract class for shared state and partial implementation, and an interface for capabilities.

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

    final is a modifier: a final variable cannot be reassigned, a final method cannot be overridden, and a final class cannot be extended. finally is a block that runs after try and catch whether or not an exception occurred. finalize was a method the garbage collector could call before reclaiming an object; it is deprecated for removal and should not be used. Use try-with-resources or Cleaner instead.

  10. 10. What does the static keyword mean?

    A static member belongs to the class rather than to any instance. Static fields are shared by all instances, static methods can be called without creating an object (and cannot use this), static blocks run once when the class is loaded, and static nested classes do not hold a reference to an outer instance.

  11. 11. What are the access modifiers in Java?

    private: visible only within the class. Default (no keyword): visible within the same package. protected: visible within the package and in subclasses. public: visible everywhere. Prefer the most restrictive level that works.

  12. 12. What is the difference between checked and unchecked exceptions?

    Checked exceptions extend Exception (but not RuntimeException) and the compiler forces you to catch them or declare them with throws, for example IOException. Unchecked exceptions extend RuntimeException or Error, such as NullPointerException, and need no declaration. Use checked exceptions for recoverable conditions the caller should handle, and unchecked ones for programming errors.

  13. 13. What is try-with-resources?

    It closes resources automatically when the block ends, even if an exception is thrown. The resource must implement AutoCloseable. It replaces verbose finally blocks and keeps exceptions from close() as suppressed exceptions.

    try (BufferedReader reader = Files.newBufferedReader(Path.of("data.txt"))) {
        System.out.println(reader.readLine());
    } catch (IOException e) {
        System.err.println(e.getMessage());
    }
  14. 14. Is Java pass-by-value or pass-by-reference?

    Java is always pass-by-value. For objects, the value that is passed is a copy of the reference. So a method can change the object the reference points to, but reassigning the parameter inside the method does not affect the caller's variable.

  15. 15. How would you create an immutable class?

    Make the class final (or its constructor private), make all fields private and final, set them only in the constructor, provide no setters, and make defensive copies of any mutable objects going in or out (for example collections or Date). Since Java 16 a record gives you a shallowly immutable data class with much less code.

  16. 16. What is the difference between the stack and the heap, and how does garbage collection work?

    Each thread has a stack that holds method frames, local variables, and references. Objects live on the heap, which is shared by all threads. The garbage collector reclaims heap objects that are no longer reachable from any live reference (GC roots such as stack variables and static fields). Modern collectors such as G1 work mostly in the background and are generational, because most objects die young.

Interview Tips

  • Explain with a short code example when you can; interviewers like to see you reason about output.
  • Know the equals and hashCode contract well, since it connects strings, collections, and bugs.
  • Mention the Java version when a feature is recent, such as records, text blocks, or switch expressions.