yoklainterview sim

Backend Java Generics Interfaces Interview Questions

75 verified Backend Java Generics Interfaces interview questions — solve with answers, learn from explanations, test yourself in a real simulation.

Try the real simulation →

Sample questions

Java Generics InterfacesDifficulty 1
What best describes "type erasure" in Java generics?
  • aThe compiler generates a separate specialized class for each type argument, similar to C++ templates.
  • bThe compiler checks generic types at compile time, then removes the type parameter information from the runtime bytecode.
  • cThe JVM checks generic type parameters at runtime and throws an error on any mismatch.
  • dThe compiler stores the exact type argument as bytecode metadata so reflection can always read it directly.
Explanation:Java generics are implemented via type erasure: the compiler enforces type-safety during compilation, then erases type parameters, replacing them with their bound (or Object) in the generated bytecode. This is why generic type information is largely unavailable at runtime.
Java Generics InterfacesDifficulty 1
You write List list = new ArrayList(); without a type argument. What is the risk of this raw type usage?
  • aIt behaves exactly like List<Object> in every respect, with no practical difference.
  • bIt is a compile error in current Java versions, so the risk does not arise.
  • cIt only causes a problem if the list is left empty after creation.
  • dThe compiler can no longer check element types for that list, risking a ClassCastException later at runtime.
Explanation:Raw types disable generic type checking for that variable. The compiler issues an unchecked warning, and code that puts the wrong type in can compile fine yet blow up with a ClassCastException later when an element is read and cast.
Java Generics InterfacesDifficulty 1
class Box<T> {
    private T value;
    Box(T value) { this.value = value; }
    T get() { return value; }
}

public class Main {
    public static void main(String[] args) {
        Box<String> box = new Box<>("hello");
        System.out.println(box.get().length());
    }
}

What does this program print?
  • a5
  • bhello
  • cCompile error: cannot call length() on a generic type T.
  • dRuntime exception: ClassCastException.
Explanation:Box<String> binds T to String, so box.get() returns a String, and calling .length() on "hello" gives 5. The generic type is fully resolved at the call site, so this compiles and runs cleanly.
Java Generics InterfacesDifficulty 2
class Pair<T extends Comparable<T>> {
    T first, second;
    Pair(T a, T b) { first = a; second = b; }
    T max() { return first.compareTo(second) >= 0 ? first : second; }
}

public class Main {
    public static void main(String[] args) {
        Pair<Integer> p = new Pair<>(3, 7);
        System.out.println(p.max());
    }
}

What does this print?
  • a3
  • bCompile error: Integer does not satisfy the bound T extends Comparable<T>.
  • c7
  • dRuntime exception: NullPointerException on compareTo.
Explanation:Integer implements Comparable<Integer>, satisfying the bound, so this compiles. first.compareTo(second) compares 3 to 7, which is negative, so max() returns second, i.e. 7.
Java Generics InterfacesDifficulty 1
Which statement correctly describes a functional interface in Java?
  • aAn interface with exactly one abstract method; it may still have any number of default and static methods.
  • bAn interface with exactly one method in total, counting default and static methods as well.
  • cAny interface that is used together with a lambda expression somewhere in the codebase.
  • dAn interface annotated with @FunctionalInterface regardless of how many abstract methods it declares.
Explanation:A functional interface has exactly one abstract method (the target for a lambda or method reference); default and static methods don't count toward that limit. @FunctionalInterface is only a compiler check that enforces this rule — it does not itself make an interface functional if there are two or more abstract methods.
Java Generics InterfacesDifficulty 2
import java.util.function.Predicate;

public class Main {
    public static void main(String[] args) {
        Predicate<String> isLong = s -> s.length() > 3;
        System.out.println(isLong.test("hi"));
    }
}

What is printed?
  • atrue
  • bCompile error: Predicate requires a return statement in braces.
  • c2
  • dfalse
Explanation:Predicate<String> defines boolean test(T t). The lambda checks "hi".length() > 3, i.e. 2 > 3, which is false.

Test yourself against the 3300-question Backend bank.

Start interview