Interview Q&A / Java

INTERVIEW Q&A · 72 QUESTIONS

Java interview questions

Collections, the JVM, strings, exceptions and threads, the backbone of most campus placement rounds.

  1. What is a functional interface in Java and which built-in ones should you know?

    A functional interface has exactly one abstract method, so a lambda can implement it. Core ones are Predicate (test), Function (apply), Consumer (accept) and Supplier (get). @FunctionalInterface makes the compiler check the rule.

    BeginnerQuick answer1 min read
  2. What is a lambda expression in Java and where can you use it?

    A lambda is a short anonymous function written as (parameters) -> body. It can be used wherever a functional interface, an interface with exactly one abstract method, is expected, such as Runnable, Comparator or Predicate.

    BeginnerQuick answer1 min read
  3. What is a method reference in Java and when would you use it instead of a lambda?

    A method reference is a shorthand like String::length for a lambda that only calls one existing method. It makes code shorter and clearer when the lambda adds nothing beyond that call.

    BeginnerQuick answer1 min read