yoklainterview sim

Backend Java Stdlib Http Interview Questions

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

Try the real simulation →

Sample questions

Java Stdlib HttpDifficulty 1
Which statement about ArrayList vs LinkedList in Java is correct?
  • aLinkedList gives O(1) random access via get(index), same as ArrayList.
  • bArrayList gives O(1) random access via get(index), while LinkedList must walk the list node by node for get(index).
  • cArrayList is a doubly linked list internally, so insertion at the head is O(1).
  • dBoth classes store elements in a fixed-size array, so neither can grow beyond its initial capacity.
Explanation:ArrayList is backed by a resizable array, so get(index) is a direct array access — O(1). LinkedList is a doubly linked list, so get(index) must traverse from the head or tail depending on which is closer — O(n) in the worst case. Insertion at the head is cheap for LinkedList (O(1)) but expensive for ArrayList (O(n), shifting elements).
Java Stdlib HttpDifficulty 2
What is a key behavioral difference between HashSet and TreeSet?
  • aHashSet keeps elements sorted by natural order; TreeSet keeps insertion order.
  • bTreeSet allows duplicate elements while HashSet does not.
  • cTreeSet keeps elements in sorted order (via Comparable or a supplied Comparator), while HashSet gives no ordering guarantee.
  • dHashSet requires elements to implement Comparable, while TreeSet does not.
Explanation:TreeSet is backed by a red-black tree and maintains elements in sorted order according to their natural ordering (Comparable) or a Comparator passed to its constructor. HashSet is backed by a HashMap and gives no ordering guarantee at all — iteration order depends on hash bucket layout. Both sets reject duplicates (per equals/hashCode or compareTo==0). It's TreeSet, not HashSet, that needs Comparable or a Comparator to order elements.
Java Stdlib HttpDifficulty 2
You need a Map that preserves insertion order when iterating over its entries, without sorting. Which implementation fits best?
  • aLinkedHashMap, since it maintains a doubly linked list of entries in insertion order alongside the hash table.
  • bTreeMap, since it always iterates in the order elements were inserted.
  • cHashMap, since Java guarantees insertion order for all Map implementations since Java 8.
  • dAny Map implementation works identically here, since Map never has a defined iteration order.
Explanation:LinkedHashMap extends HashMap and additionally maintains a doubly linked list running through all its entries, so iteration order matches insertion order (or access order, if configured). TreeMap sorts by key according to Comparable/Comparator, not insertion order. Plain HashMap gives no iteration order guarantee at all — this was never changed in Java 8.
Java Stdlib HttpDifficulty 2
class Employee implements Comparable<Employee> {
    String name;
    int age;

    Employee(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(Employee other) {
        return Integer.compare(this.age, other.age);
    }
}

List<Employee> list = new ArrayList<>(List.of(
    new Employee("Ana", 35),
    new Employee("Bo", 22),
    new Employee("Cy", 29)
));
Collections.sort(list);
System.out.println(list.get(0).name);

What does this print?
  • aAna
  • bBo
  • cCy
  • dIt throws a ClassCastException because Employee does not override equals().
Explanation:compareTo compares by age using Integer.compare, so Collections.sort(list) orders the employees ascending by age: Bo (22), Cy (29), Ana (35). After sorting, list.get(0) is Bo. Comparable does not require overriding equals() to be used correctly by Collections.sort — no ClassCastException occurs here.
Java Stdlib HttpDifficulty 2
List<String> words = new ArrayList<>(List.of("banana", "fig", "apple", "kiwi"));
words.sort(Comparator.comparing(String::length).thenComparing(Comparator.naturalOrder()));
System.out.println(words);

What does this print?
  • a[apple, banana, fig, kiwi]
  • b[banana, apple, fig, kiwi]
  • c[fig, kiwi, apple, banana]
  • d[kiwi, fig, banana, apple]
Explanation:Comparator.comparing(String::length) sorts primarily by string length: fig(3), kiwi(4), apple(5), banana(6). thenComparing(naturalOrder()) is only a tie-breaker for equal lengths, and no two words share a length here, so it has no effect. The result is [fig, kiwi, apple, banana].
Java Stdlib HttpDifficulty 1
List<Integer> nums = List.of(1, 2, 3);
nums.add(4);

What happens when this runs?
  • aIt compiles fine and quietly adds 4 to the underlying list.
  • bIt compiles but silently ignores the add call.
  • cIt throws a ClassCastException at runtime.
  • dIt throws an UnsupportedOperationException at runtime.
Explanation:List.of(...) returns an immutable list implementation. Any structural modification method (add, remove, set, clear) throws UnsupportedOperationException at runtime — the code compiles fine since List still declares add(), but the immutable implementation refuses the call.

Test yourself against the 3300-question Backend bank.

Start interview