🧱 Object-Oriented Java — interview questions

11 questions. Filter by what the role asks for; open one for the six layers of the answer. The course is where the answers come from.

11 of 11

01`new Config("api")` prints `name=null` from inside the constructor, even though `this.name = name` is the very first line of the constructor body someone wrote. The field being read is a *different* field, assigned by an initialiser. What's actually happening, and why does the order matter?Constructioneasy0-2y02A constructor calls `bus.subscribe(this)` as its first line, reasoning "the object is live as soon as it exists, so registering it immediately is fine." A field assigned on the next line is occasionally seen as `null` by the listener callback. What's wrong with the reasoning?Constructionmedium0-2y03A teammate asks why the codebase prefers `Money.of(500, EUR)` over `new Money(500, EUR)` everywhere, calling it "just a style preference." Is it?Static memberseasy0-2y04A library author insists `Registry.getInstance()` is a singleton — "one per JVM, that's what static means." Two web applications deployed in the same Tomcat each see a different set of registrations from the same library jar. Who's right?Static membersmedium3-5y05A `ReportView` holding an 8 MB rendered buffer registers `bus.subscribe(new Listener() { public void onEvent(Event e) { refresh(); } })` and is never explicitly unsubscribed. After a few hundred report screens, the heap is gone, and a heap dump's histogram shows `byte[]` as the biggest consumer — which is true and doesn't explain anything. What's actually holding the memory, and what's the one-word category of fix?Nested classesmedium3-5y06A platform team adds `boolean healthy();` to their published `Plugin` interface and ships it as a minor release, reasoning "every plugin author will just recompile." Forty third-party plugins, none of them recompiled, are loaded into the platform the next day. What happens, and what should the team have shipped instead?Interfacesmedium3-5y07A reporting service declares `private static final SimpleDateFormat FMT = new SimpleDateFormat("yyyy-MM-dd HH:mm")` and reuses it across every request. It passes every test. Under 200 concurrent requests in production, about one row in a thousand carries a date from a *different* request, and occasionally a `NumberFormatException` comes out of `parse`. What's actually happening, and why is `static` — not `SimpleDateFormat` alone — the other half of the bug?Static membershard5-8y08`class InstrumentedSet<E> extends HashSet<E> { int added = 0; @Override public boolean add(E e) { added++; return super.add(e); } @Override public boolean addAll(Collection<? extends E> c) { added += c.size(); return super.addAll(c); } }` — calling `addAll(List.of("a", "b", "c"))` on a fresh instance leaves `added == 6`, not 3. Where do the extra three come from, and what does it say about extending a class you don't own?Inheritancehard5-8y09A `Plugin` interface adds `default boolean healthy() { return true; }`. A month later, one plugin author reports their health check is being ignored — the platform always treats their plugin as healthy regardless of what their code does. Their class extends a shared `BasePlugin`, which happens to already declare a public `boolean healthy()` method, unrelated to the interface, returning whether the plugin's background thread is alive. No compiler ever mentioned a conflict. Why not, and what actually happens at the call site?Interfaceshard5-8y10`class Base { Base() { init(); } void init() {} }` and `class Derived extends Base { final List<String> items = new ArrayList<>(); @Override void init() { items.add("x"); } }` — constructing a `Derived` throws `NullPointerException` inside `init()`, on a field marked `final` that's initialised right there at its declaration. How, precisely, does an object's class pointer already being `Derived` from the moment of allocation explain this, and why doesn't `final` help?Inheritancesenior8+y11`interface Parser<T> { T parse(String s); }` and `class IntParser implements Parser<Integer> { public Integer parse(String s) { return Integer.valueOf(s); } }` — `javap -p IntParser` shows *two* `parse` methods in the class file, not one. Neither is dead code. What produced the second one, and which one actually runs when a caller holds a `Parser<Integer>` reference?Generics and dispatchsenior8+y