Java interview questions, with the answers worked out.
Practice an object-oriented design question and a collections question, free, with full answers in Java.
Published November 1, 2026
Java interviews lean on object-oriented design and the standard collections. Here’s one question on each.
Free question 1 (design): Interface or abstract class?
You’re building checkout for an online store. Customers can pay by credit card, debit card or gift card, and more methods will be added later. Would you use an interface or an abstract class? Show a sketch.
Model answer
Start with the difference:
- An interface says what a class can do. A class can implement many interfaces. Since Java 8, interfaces can have default methods, but they can’t hold instance fields.
- An abstract class can hold shared fields and code. A class can only extend one class.
Then the decision: “I’d make PaymentMethod an interface, because every payment type needs to do the same thing, pay, but they don’t share much data. Credit and debit cards do share data, like the last four digits, so I’d add an abstract class just for cards.”
public interface PaymentMethod {
boolean pay(BigDecimal amount);
default String receiptLabel() {
return getClass().getSimpleName();
}
}
public abstract class CardPayment implements PaymentMethod {
protected final String lastFour;
protected CardPayment(String lastFour) {
this.lastFour = lastFour;
}
@Override
public String receiptLabel() {
return "Card ending " + lastFour;
}
}
public class CreditCard extends CardPayment {
public CreditCard(String lastFour) {
super(lastFour);
}
@Override
public boolean pay(BigDecimal amount) {
// call the card processor here
return true;
}
}
public class GiftCard implements PaymentMethod {
private BigDecimal balance;
public GiftCard(BigDecimal balance) {
this.balance = balance;
}
@Override
public boolean pay(BigDecimal amount) {
if (balance.compareTo(amount) < 0) return false;
balance = balance.subtract(amount);
return true;
}
}
Points to say out loud:
- Checkout code only ever uses PaymentMethod, so adding a new method later means a new class, not changes to checkout.
- Money uses BigDecimal, not double, because double can’t store amounts like 0.10 exactly.
- If the gift card could be used by two checkouts at once, the balance update would need to be thread safe or handled in the database.
What interviewers listen for: you explain both options before choosing, your choice follows from the problem, and you spot details like BigDecimal.
Free question 2 (collections): Why do equals and hashCode have to agree?
You use a Point class as a key in a HashMap. You put new Point(1, 2) in the map, then call map.get(new Point(1, 2)) and get null. Why, and how do you fix it?
Model answer
Why: HashMap first uses hashCode() to pick a bucket, then equals() to find the match inside it. If Point doesn’t override them, Java uses the default versions, which compare object identity. Two separate Point(1, 2) objects then usually land in different buckets and are never equal, so get returns null.
The fix: override both, using the same fields.
public final class Point {
private final int x;
private final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Point)) return false;
Point p = (Point) o;
return x == p.x && y == p.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
}
The rule to state: if two objects are equal, they must have the same hash code. Overriding equals without hashCode breaks HashMap and HashSet.
Extra points:
- Keys should be immutable. If you change a key’s fields after putting it in a map, its hash changes and the map can’t find it. That’s why the fields are final.
- On Java 16 or later, a record does this for you: public record Point(int x, int y) {} gets equals and hashCode based on both fields.
What interviewers listen for: you explain how HashMap uses both methods, you state the contract, and you mention immutability.
The full Java interview guide
One printable PDF. Part 1 is the prep section: what to wear, when to arrive or log in, key topics to review for this role, questions to ask, talking about pay, a story bank, the day before and the day itself, and a thank you note. Part 2 is 20 questions for this role, each with a possible answer and lined space for your notes. A checklist for the night before is included. Print it on US Letter and write in it.
- A full prep section: what to wear, when to arrive or log in, key topics to review, questions to ask, talking about pay, a story bank, the day before and the day itself, and a thank you note
- 6 coding questions: strings, arrays, maps, streams, recursion and a simple class design, with time and space notes
- 4 concept questions: exceptions, threads, immutability and how Spring Boot fits together
- 5 behavioural questions with sample STAR answers, plus lines for your own stories
- 5 situational questions, such as a slow endpoint or a disagreement in code review
- A checklist for the night before
One PDF, $24 CAD / $17 USD, paid once on this page and emailed to you. Visitors in Canada pay $24 CAD. Everyone else pays $17 USD.
About these questions
Are these Java interview questions for jobs in Canada?
Yes. Java questions come up in Canadian interviews for backend developer, Android and enterprise software roles, often at banks, insurers and government. Expect object-oriented design and collections questions like these two, plus a coding problem.
Is there a full interview guide?
Yes. The full printable Java interview guide is $24 CAD / $17 USD. You pay on this page and we email you the PDF. It has a prep section (what to wear, when to arrive or log in, key topics for this role, questions to ask, talking about pay, a story bank, the day before and the day itself, and a thank you note), 20 Java questions with possible answers and lined space for notes, and a checklist for the night before. The two questions on this page are free to use now.
Not getting interviews yet? Our software and IT resumé help shows what hiring managers screen for first.
More roles: interview questions by role.
Don't see your job? Tell us.
Tell us the job you're interviewing for, and we'll write interview questions and model answers for it. It's free to ask. We read every request. We can't promise to add every role, but the most requested ones go first.
Thanks. We got your request.