Skip to content
December Code

Java · 6 topic pages · 450 questions in the bank

Java for placements: interview questions by topic

Java is the language many campus placement tests assume you know, and it is examined in two ways: as the language of the coding round, and as a subject in its own right, asked as a few lines of code followed by 'what does this print?' or 'does this compile?'. Those questions are rarely about syntax. They sit where Java's rules surprise people: integer division and overflow, == on strings and boxed numbers, a switch that falls through, a static block that runs before a constructor, a list that throws when you remove from it inside a loop.

Interviews add the questions behind the code: how a HashMap finds a key, why equals() and hashCode() travel together, what final means in three different places, when to pick an ArrayList over a LinkedList, and what type erasure leaves behind at run time. Product-company rounds add the newer language — records, switch expressions, sealed types — to check that your Java is current.

The pages below take one topic each, with answers whose code has been compiled and run, and one question exactly as the diagnostic asks it. Exceptions, memory and pass-by-value, and Java 8 features will get pages once each topic's question pool is deep enough to publish a sample without giving the diagnostic away; the diagnostic itself already draws from every Java topic in the bank.

New to this? How to prepare for placements — the method — and what the readiness test measures.

On this page3 sections

Java topics, one page each

One question, exactly as the diagnostic asks it

From the Java bank — 450 questions across 9 topics. The answer is marked because this one is public; in a sitting you choose first, then see why each option is right or wrong.

Strings · easyJAVA-004

What does this Java code print?

Java
String a = "hi";
String b = "hi";
String c = new String("hi");
System.out.println((a == b) + " " + (a == c) + " " + a.equals(c));
  1. 1
    true true true
  2. 2
    true false true
    correct
  3. 3
    false false true
  4. 4
    true false false
Java
a and b are the same literal, so the compiler and the string pool give them the same object and a == b is true. new String("hi") creates a separate object with the same characters, so a == c is false while a.equals(c), which compares contents, is true. true true true assumes == compares contents. false false true assumes every literal is a separate object. true false false assumes equals() also compares references, which it does only for classes that do not override it; String does.

More on this topic: Java String interview questions

Measure it

Reading the answers tells you what’s true. A diagnostic tells you what you get wrong.

10 Java questions across its topics, easy to hard, about fifteen minutes. You get a readiness figure with the arithmetic shown, the topics you missed named, and a practice set sized for today. Free: a diagnostic in every subject, a retake a week and 15 problems a day. No card.

By Harshit · updated