Cosmic Module
J
Qubits of DPK
March 14, 2026
Core Java
What is Enum?
A special class that represents a fixed set of constants.
java
QUBITS OF DPK
Enum with if and switch
java
QUBITS OF DPK
Enum as Class (with fields/methods)
java
QUBITS OF DPK
Annotations
Metadata that provides information to the compiler or JVM about the code.
java
QUBITS OF DPK
Types of Interfaces & Functional Interface
javascript
QUBITS OF DPK
java
QUBITS OF DPK
Lambda Expressions
A concise way to represent a functional interface implementation.
javascript
QUBITS OF DPK
java
QUBITS OF DPK
Common Built-in Functional Interfaces
java
QUBITS OF DPK
️ All Traps
java
QUBITS OF DPK
Interview Answer (SDE-2)
"Enum is a type-safe way to represent fixed constants — more powerful than int constants since it's a full class with methods, fields, and constructors. Annotations are metadata markers that inform the compiler or runtime — built-ins include @Override, @Deprecated. A Functional Interface has exactly one abstract method — SAM (Single Abstract Method) type — and can be implemented with a lambda expression. Lambda is a concise anonymous function — it enables functional programming style in Java. Common functional interfaces: Function, Predicate, Consumer, Supplier."
Interview Questions & MAANG-Level Answers
Q1. What is enum and why use it over constants?
Enum is a special class representing a fixed set of named constants. Over int constants: (1) Type safety — Day day = Day.MONDAY prevents passing invalid int like 99. (2) Readability — Day.FRIDAY is self-documenting vs magic number 5. (3) Rich behavior — enums can have fields, methods, constructors. (4) switch support — works cleanly in switch. (5) Singleton guarantee — each enum constant is a singleton. Java's values() method iterates all constants.
Q2. Can enum have methods and constructors?
Yes. Enum is a full class — it can have fields, constructors (always private), and methods:
java
QUBITS OF DPK
Q3. What is a functional interface?
A functional interface has exactly ONE abstract method (SAM — Single Abstract Method). It can have multiple default/static methods. Annotated with @FunctionalInterface (optional but recommended — compiler enforces SAM). Can be implemented with a lambda expression. Built-in examples: Runnable (run()), Callable (call()), Comparator (compare()), and Java 8's Function, Predicate, Consumer, Supplier.
Q4. What is a lambda expression?
A lambda is a concise anonymous function — shorthand for implementing a functional interface. Syntax: (params) -> expression or (params) -> { statements; return value; }. It captures its enclosing scope's effectively final variables. Key benefits: removes boilerplate anonymous inner class code, enables functional programming style, works with Stream API:
java
QUBITS OF DPK
Q5. What is the difference between @Override and @Deprecated?
@Override is a compile-time annotation that verifies the method actually overrides a parent method — compiler error if not. @Deprecated marks a method/class as outdated and discourages use — compiler generates a warning when deprecated code is called. Neither changes runtime behavior, both are informational. @SuppressWarnings("deprecation") suppresses the warning when you must use deprecated code.
Q6. What are the built-in functional interfaces in Java?
In java.util.function:
java
QUBITS OF DPK
Used heavily in Stream API and CompletableFuture.
Q7. Can a lambda expression throw checked exceptions?
Not directly if the functional interface's method doesn't declare it. Consumer<String> accepts (String) -> void — if your lambda throws IOException (checked), it won't compile. Workarounds: (1) Wrap in try-catch inside lambda. (2) Create a custom functional interface that declares the checked exception: @FunctionalInterface interface ThrowingConsumer<T> { void accept(T t) throws Exception; }. (3) Use utility methods that wrap checked exceptions as unchecked.