Core Java · Chapter 46 of 48
Interview: core Java
By the end of this chapter you can predict, and explain out loud, six core Java behaviours that interviewers ask about and that cause real bugs. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The interview
- 6 tasks
- Java
- In the all labs plan
The first 2 chapters of Core Java are free. This one comes with the all labs plan.
Why you need this
Tapri is hiring Java developers for fifty new stations, and Dev, the CTO, wants you on the interview panel. First he puts you through his own round. Every question is a bug that reached a Tapri till: a UPI reference compared with ==, a printed bill that changed afterwards, a train lookup that returned null for a train that exists.
You have used all of this for forty chapters. Such code compiles and usually works, which is the problem: it passes the test on your laptop. Dev's rule: call the output before anyone runs the code, and say why.
What you build: 6 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1 · interview question
Interview: CoreQuiz.java, predict == and equals on pooled and new strings
In CoreQuiz.java, make stringPool() return the lines the code in its comment prints, one string per line, in order, written exactly as Java prints them (true or false in lower case).
The check runs the same code and compares your list with what Java really printed.
- Task 2 · interview question
Interview: CoreQuiz.java, predict == on Integers inside and outside the cache
In CoreQuiz.java, make integerCache() return the lines the code in its comment prints, one string per line, in order, written exactly as Java prints them (true or false in lower case).
The check runs the same code and compares your list with what Java really printed.
- Task 3 · interview question
Interview: Bill.java, make the Bill class truly immutable
In Bill.java, make Bill immutable. Declare the class final, and make every field private and final (any field you add too). The constructor keeps its own copy of the items list, so changing the caller's list afterwards does not change the bill. items() returns a list that throws UnsupportedOperationException when someone tries to add to it. withItem(item, price) returns a new Bill, never this one, with the same station, the items plus the new item at the end, and the total plus the price; this bill stays as it was.
For example, a bill for SBC built from a list holding Masala chai with total 40 still holds only Masala chai after Free samosa is added to that list. withItem("Bun maska", 50) on it returns a new SBC bill with [Masala chai, Bun maska] and total 90, and the old bill still has [Masala chai] and 40.
- Task 4 · interview question
Interview: CoreQuiz.java, predict what a method can and cannot change in its caller
In CoreQuiz.java, make passByValue() return the lines the code in its comment prints, one string per line, in order, written exactly as Java prints them (a list prints in square brackets, like [a, b]).
The check runs the same code and compares your list with what Java really printed.
- Task 5 · interview question
Interview: CoreQuiz.java, predict the order of static blocks, instance blocks and constructors
In CoreQuiz.java, make initOrder() return the lines the code in its comment prints, one string per line, in order, written exactly as Java prints them.
The check runs the same code and compares your list with what Java really printed.
- Task 6 · interview question
Interview: CoreQuiz.java, predict what char arithmetic prints
In CoreQuiz.java, make charArithmetic() return the lines the code in its comment prints, one string per line, in order, written exactly as Java prints them (a number as digits, a character as itself).
The check runs the same code and compares your list with what Java really printed.
What you will learn
- The string pool: when two strings are one object.
- Boxed numbers: the Integer cache, unboxing, equals across types.
- Immutable classes: what a final field does not give you.
- Pass by value, initialisation order, char arithmetic: three rules to state.
More in The interview
Build "Interview: core Java" yourself
Tapri runs chai counters at railway stations, and every counter runs a Java program you write. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Core Java are free. This one comes with the all labs plan.