Java Advanced Quiz
Equality, generics, collections, concurrency and the JVM memory model — read from real code rather than definitions.
Start the quizSample questions from this quiz
A preview of the style and depth. Try each one, then reveal the answer — or skip straight to the timed quiz.
1. What does this print?
Integer a = 127, b = 127;
Integer c = 128, d = 128;
System.out.println((a == b) + " " + (c == d));- true true
- true false
- false false
- false true
Show answer
Answer: true false. Integer caches boxed values from -128 to 127, so `a` and `b` are the same object and `==` is true. Above that range autoboxing creates new objects, so reference comparison fails. Always compare boxed types with `equals`.
2. This class breaks when used in a HashSet — duplicates appear. Which line is the cause?
1 class User {
2 final String email;
3 User(String e) { this.email = e; }
4 @Override public boolean equals(Object o) {
5 return o instanceof User u && u.email.equals(email);
6 }
7 }- The class overrides equals but not hashCode
- Line 5 — instanceof pattern matching is invalid in equals
- Line 2 — the field must not be final
- Line 3 — the constructor must be public
Show answer
Answer: The class overrides equals but not hashCode. HashSet finds the bucket by hashCode first and only then calls equals. Two equal Users inherit different identity hash codes, land in different buckets, and are never compared — so both are stored. The contract is absolute: if you override equals you must override hashCode.
3. What does this print?
String a = "hi";
String b = "hi";
String c = new String("hi");
System.out.println((a == b) + " " + (a == c) + " " + a.equals(c));- true true true
- true false true
- false false true
- true false false
Show answer
Answer: true false true. String literals are interned, so `a` and `b` reference the same pooled object. `new String(...)` forces a distinct object, so `==` fails while `equals` compares contents and succeeds. Calling `c.intern()` would return the pooled instance.
Frequently asked questions
How many questions are in this Java quiz?+
30 questions, with a 45-minute time limit. Every question includes a written explanation of the correct answer.
Is this Java quiz free?+
Yes. Every quiz on CodexQuizz is free and needs no account. You only enter a name if you choose to post your score to the leaderboard.
What level is the advanced quiz aimed at?+
Equality, generics, collections, concurrency and the JVM memory model — read from real code rather than definitions.
Can I use this to prepare for a Java interview?+
Yes. The questions cover the topics that come up in Java technical screens, and the explanations are written so that a wrong answer still teaches you the underlying concept.
Ready to test your Java?
30 questions, 45 minutes. Free, no sign-up.
Start now