December Code

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.

Java topics, one page each

6 of the 9 Java topics in the bank have a page so far; the diagnostic draws from all 9.

  1. Java data types and operators interview questions

    Java data types and operators as interviews test them: primitive sizes, int overflow, integer division, casting, char arithmetic and ++ traps.

  2. Java String interview questions

    Java String questions for interviews: == vs equals, the string pool and intern, immutability, StringBuilder vs StringBuffer, split and char[] passwords.

  3. Java loops and control flow interview questions

    Java control flow as interviews test it: switch fall-through, switch expressions, labeled break and continue, for-each limits and the ternary operator.

  4. Java OOP interview questions

    OOP as Java implements it: equals and hashCode, final, initialisation order, records, sealed classes, inner classes, pattern matching and immutability.

  5. Java Collections interview questions

    Java Collections for interviews: how HashMap works, ArrayList vs LinkedList, HashMap vs ConcurrentHashMap, Comparable vs Comparator, fail-fast iterators.

  6. Java wrapper classes and generics interview questions

    Java wrapper classes and generics for interviews: autoboxing, the Integer cache, == on wrappers, type erasure, wildcards, PECS and bounded type parameters.

One question, exactly as the diagnostic asks it

From the Java bank — 30 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?

String a = "hi";
String b = "hi";
String c = new String("hi");
System.out.println((a == b) + " " + (a == c) + " " + a.equals(c));
  1. 1true true true
  2. 2true false truecorrect
  3. 3false false true
  4. 4true false false

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: 1 diagnostic a month and 15 problems a day. No card.

By Harshit · updated