Java traps · Chapter 7 of 16

Arrays.asList: fixed size and a shared array

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Everyday traps
  • 3 tasks
  • Java
  • In the all labs plan

The first 2 chapters of Java traps are free. This one comes with the all labs plan.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Repro.java: a PUNE special that crashes the menu

    Write reproduce() in Repro.java. Ask Menu.menuFor for a menu with one special, poha, and return what came back as text (the list's toString()). If menuFor throws instead, catch the exception and return its simple name.

    The check runs your reproduce() twice: beside the original Menu.java it must return "UnsupportedOperationException", and beside a fixed Menu.java it must return "[masala chai, cutting chai, elaichi chai, bun maska, samosa, vada pav, poha]". So it must really ask menuFor for a menu with poha in it. No real waiting and no reading input: reproduce() must finish within 3 seconds.

  2. Task 2

    Menu.java: specials on the menu, house order kept

    Fix menuFor(List<String> specials) and board() in Menu.java. menuFor gives the house items in house order, then the specials in their order (with no specials, just the house items); board() gives the house items A to Z. Call them in any order, as often as you like: HOUSE never changes, and every list they return is the caller's own, so a caller that changes it changes no other menu or board.

    Keep HOUSE as it is, and leave the specials list you are given alone. The check uses its own specials, built at run time, calls board() twice before it asks for a menu, and reverses the lists it gets back to see whether anything else changes.

  3. Task 3

    MenuTest.java: a test that would have caught it

    Write a JUnit test class, MenuTest.java, that would have caught both bugs before they reached the counters. The check runs your file three times: beside the original Menu.java, where at least one test must fail; beside the fixed Menu.java, where every test must pass; and beside a wrong fix that copies the list in menuFor but still sorts HOUSE in board(), where at least one test must fail.

    So test both sides: a menu with a special has the house items in house order, then the special; and after board() has run, menuFor still gives the house order (call board() first in the same test). No real waiting: each test must finish within 3 seconds.

What you will learn

  • Repro.java: a PUNE special that crashes the menu
  • Menu.java: specials on the menu, house order kept
  • MenuTest.java: a test that would have caught it

More in Everyday traps

Build "Arrays.asList: fixed size and a shared array" yourself

Tapri Counter runs on Java, and Kabir, its senior developer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Java traps are free. This one comes with the all labs plan.