Core Java interview questions

15 questions from the Core Java Fundamentals: Syntax, OOP and Collections course, each with the correct answer and an explanation. Try to answer before you open the answer.

Module 1: Getting Started

1. What does `javac` produce from a `.java` file?

  1. Native machine code for your CPU
  2. Platform-neutral bytecode in a .class file
  3. An executable .exe
  4. Nothing — Java is interpreted line by line
Show answer

Answer: Platform-neutral bytecode in a .class file

The compiler emits JVM bytecode; the JVM then executes (and JIT-compiles) it at runtime.

Lesson: Your First Program

Module 3: String Handling Deep Dive

2. Why can `s1 == s2` be true for two String literals but false after `new String(...)`?

  1. == always compares content for strings
  2. Literals are shared from a string pool; new String() forces a fresh object
  3. It's a JVM bug
  4. Strings are primitives so == is undefined
Show answer

Answer: Literals are shared from a string pool; new String() forces a fresh object

Literals are interned into a shared pool, so identical literals share one object. new String(...) opts out of pooling and always allocates a new object on the heap.

Lesson: Strings: Pool, Builders & Gotchas

Module 4: Wrapper Classes & Autoboxing

3. What is the safest way to compare two Integer objects for equal value?

  1. == always works for Integer
  2. .equals(), or unbox both to int first
  3. compareTo() only, never equals()
  4. They can never be compared
Show answer

Answer: .equals(), or unbox both to int first

== compares references and only accidentally works for the small cached range (-128..127). .equals() (or comparing primitive int values) is always correct.

Lesson: Wrapper Classes & Autoboxing

Module 11: The Object Contract

4. You override equals() but forget hashCode(). What breaks?

  1. Nothing, they're independent
  2. Compilation fails
  3. "Equal" objects can land in different HashMap buckets and lookups fail
  4. equals() itself throws at runtime
Show answer

Answer: "Equal" objects can land in different HashMap buckets and lookups fail

Hash-based collections use hashCode() to pick a bucket first, then equals() within it. Mismatched hashCode() means equal objects can be looked for in the wrong bucket and never be found.

Lesson: equals() & hashCode()

Module 12: Core Utilities

5. You need to store unique user IDs and check membership quickly. Which type?

  1. ArrayList
  2. HashSet
  3. LinkedList
  4. int[]
Show answer

Answer: HashSet

HashSet gives O(1) average contains() and rejects duplicates automatically.

Lesson: Collections

Module 13: Exceptions Deep Dive

6. Which base class should a custom exception extend if callers should NOT be forced to declare or catch it?

  1. Exception
  2. Throwable
  3. RuntimeException
  4. Error
Show answer

Answer: RuntimeException

RuntimeException (and its subclasses) are unchecked — the compiler does not require a throws declaration or a catch block.

Lesson: Custom Exceptions & Multi-Catch

Module 15: File I/O Basics

7. Which class represents a point on the UTC timeline?

  1. Instant
  2. LocalDate
  3. Period
  4. SimpleDateFormat
Show answer

Answer: Instant

Instant is a machine timestamp; convert it to a zone for display.

Lesson: Dates & Times with java.time

8. What does javac produce?

  1. Native machine code
  2. An .exe file
  3. Platform-neutral bytecode
  4. Nothing
Show answer

Answer: Platform-neutral bytecode

The JVM runs the bytecode.

Lesson: Dates & Times with java.time

9. How should you compare String contents?

  1. ==
  2. compare with +
  3. hashCode()
  4. equals()
Show answer

Answer: equals()

== compares references.

Lesson: Dates & Times with java.time

10. Which class avoids O(n²) string concatenation in loops?

  1. StringBuilder
  2. String
  3. Scanner
  4. Objects
Show answer

Answer: StringBuilder

StringBuilder mutates one buffer.

Lesson: Dates & Times with java.time

11. What must you do when overriding equals()?

  1. Make the class final only
  2. Override hashCode() consistently
  3. Return null
  4. Nothing
Show answer

Answer: Override hashCode() consistently

Equal objects must share a hash code.

Lesson: Dates & Times with java.time

12. Which collection stores unique values with fast membership checks?

  1. ArrayList
  2. LinkedList
  3. HashSet
  4. int[]
Show answer

Answer: HashSet

HashSet offers average O(1) contains.

Lesson: Dates & Times with java.time

13. Which base class should an unchecked custom exception extend?

  1. Exception
  2. Throwable
  3. Error
  4. RuntimeException
Show answer

Answer: RuntimeException

Callers are not forced to catch it.

Lesson: Dates & Times with java.time

14. What does try-with-resources do?

  1. Closes AutoCloseable resources automatically
  2. Skips exceptions
  3. Starts threads
  4. Compiles faster
Show answer

Answer: Closes AutoCloseable resources automatically

Resources close even if an exception occurs.

Lesson: Dates & Times with java.time

15. Which java.time class suits a date with no time?

  1. Instant
  2. LocalDate
  3. Duration
  4. ZonedDateTime
Show answer

Answer: LocalDate

LocalDate holds year, month and day only.

Lesson: Dates & Times with java.time

← Back to the course