Coding Solution

Java MCQ test: 12 questions with answers (CodinGame, SkillValue type)

MCQJava12 questionsUpdated

This quiz is for developers preparing a Java technical screening or a job interview. It contains 12 code-reading and language-trap questions, each followed by a full explanation of the right answer and the wrong ones.

In short

Java recruitment tests on SkillValue, CodinGame and HackerRank keep coming back to the same core topics: String comparison with == versus equals, choosing and using collections (ArrayList, LinkedList, HashMap, HashSet), exception handling with try/catch/finally, generics and autoboxing, the Stream API with lambdas, and inheritance, polymorphism and the static/final keywords. Most questions are phrased as "what does this program print?" over a 5 to 15 line snippet. The trap is rarely algorithmic: it sits in a detail of the language semantics. This MCQ covers those topics with questions of increasing difficulty.

The topics that come up most

Practise: 12 questions with answers

Pick an answer to see the explanation.

What does this program print?
String a = "hello";
String b = "hello";
String c = new String("hello");
System.out.println((a == b) + " " + (a == c) + " " + a.equals(c));

Both "hello" literals are interned in the string pool, so a and b reference the same object and a == b is true. new String() always allocates a fresh object outside the pool, so a == c is false. equals compares the characters, so a.equals(c) is true. The other options mix up reference comparison with content comparison.

What does this program print?
String s = "java";
s.toUpperCase();
s.concat("!");
StringBuilder sb = new StringBuilder("java");
sb.append("!");
System.out.println(s + " " + sb);

String is immutable: toUpperCase() and concat() return a new string and never change the original. Since the results are discarded, s is still "java". StringBuilder is mutable: append() modifies sb in place, so it holds "java!". Option A wrongly assumes String methods mutate the receiver.

What is printed when this method is called?
static int f() {
    try {
        return 1;
    } finally {
        System.out.println("finally");
    }
}
// ...
System.out.println(f());

A finally block always runs, even after a return inside the try. The actual order is: return 1 is evaluated, finally runs and prints "finally", then the value 1 is handed back to the caller, which prints it. A return never skips finally (only System.exit() does), and this code compiles fine.

Which of these exceptions is unchecked, meaning the compiler does not force you to catch it or declare it with throws?

NullPointerException extends RuntimeException, so it is unchecked: no try/catch or throws clause is required. IOException, SQLException and ClassNotFoundException (via ReflectiveOperationException) extend Exception without going through RuntimeException, which makes them checked exceptions the compiler forces you to handle or declare.

What does this program print?
Integer a = 127, b = 127;
Integer c = 128, d = 128;
System.out.println((a == b) + " " + (c == d) + " " + c.equals(d));

Autoboxing goes through Integer.valueOf(), which caches values from -128 to 127. a and b therefore get the same instance and a == b is true. 128 is outside the cache: c and d are two distinct objects, so the reference comparison c == d is false. equals compares values, so c.equals(d) is true. Always compare wrapper objects with equals.

A Point class overrides equals() to compare x and y but does not override hashCode(). Two distinct Point(1, 2) instances are added to a HashSet. Which statement is correct?

HashSet first calls hashCode() to pick a bucket and only calls equals() on elements in that same bucket. Without an override, hashCode() is Object's identity-based version: two distinct instances almost always have different hashes and are stored as two separate elements, even though equals says they are equal. This breaks the equals/hashCode contract, but the compiler cannot detect it and no exception is thrown.

What does this program print?
List<Integer> nums = List.of(1, 2, 3, 4, 5, 6);
int r = nums.stream()
    .filter(x -> x % 2 == 0)
    .map(x -> x * x)
    .reduce(0, Integer::sum);
System.out.println(r);

filter keeps the even numbers: 2, 4, 6. map squares them: 4, 16, 36. reduce(0, Integer::sum) adds them starting from 0: 4 + 16 + 36 = 56. Option 91 (1+4+9+16+25+36) forgets the filter, 21 (1+2+3+4+5+6) forgets both filter and map, and 35 (1+9+25) applies the filter the wrong way round.

What does this program print?
class A {
    static String who() { return "A"; }
    String name() { return "A"; }
}
class B extends A {
    static String who() { return "B"; }
    @Override String name() { return "B"; }
}
// ...
A obj = new B();
System.out.println(obj.who() + obj.name());

Static methods are not polymorphic: who() is resolved at compile time from the declared type of the variable, A, so obj.who() returns "A" (this is called hiding, not overriding). name() is an overridden instance method, resolved at runtime from the actual object type, B, so obj.name() returns "B". The output is "AB".

What happens at runtime?
List<String> l = new ArrayList<>(List.of("a", "b", "c"));
for (String s : l) {
    if (s.equals("a")) {
        l.remove(s);
    }
}
System.out.println(l);

The for-each loop uses an internal Iterator that remembers the list's modCount. Calling l.remove() directly on the list changes that counter; on the next call to next(), the iterator detects the mismatch and throws a ConcurrentModificationException. To remove while iterating, use iterator.remove() or removeIf(). The code compiles without issue: the check only happens at runtime.

What value does this method return?
static int f() {
    int x = 1;
    try {
        x = 2;
        return x;
    } finally {
        x = 3;
    }
}

When return x is reached, the value of x (2) is evaluated and saved as the return value before finally runs. The finally block then sets the local variable x to 3, but that has no effect on the value already set aside. The method returns 2. If finally itself contained return 3, that would override the result and 3 would be returned, but that is not the case here.

Which of these declarations compiles without error?

Generics are invariant: even though Integer extends Number, a List<Integer> is not a List<Number>, so A and D do not compile. With ? extends Number you can read Numbers but cannot add anything (the compiler does not know which exact subtype the list holds), so the l.add(1) in B fails. With ? super Integer the list accepts any Integer for writing, so C compiles. This is the PECS rule: Producer Extends, Consumer Super.

What does this program print?
Long l = 127L;
System.out.println(l.equals(127) + " " + l.equals(127L) + " " + (l == 127));

The literal 127 is an int, so l.equals(127) autoboxes an Integer; Long.equals() requires a Long and returns false even though the values match. 127L is a Long, so l.equals(127L) is true. In l == 127, the left operand is a wrapper and the right one a primitive: Java unboxes l to a long and compares 127 == 127, which is true. The trap is the first term: equals never converts types.

On test day

  1. When reading a snippet, first note the declared type of every variable: most traps (static methods, generics, equals on wrappers) are decided by the compile-time type, not the runtime type.
  2. On any String or Integer question using ==, check for the string pool or the -128 to 127 cache before answering.
  3. With try/catch/finally, trace the execution order line by line and ask whether the return value has already been evaluated.
  4. For a stream, work out each stage on paper (after filter, after map, after reduce) instead of guessing the final result.

Practise with Coding Solutions

On a practice MCQ shown on screen, the Coding Solutions extension explains the right answer and the rule behind it, in 29 languages. For coding exercises, see also our explained CodinGame solutions and the platform guides.

Frequently asked questions

Which Java version should I know for a recruitment test?

Most tests target Java 8 to 17: lambdas, the Stream API, Optional, var (Java 10), records and switch expressions (Java 14-17). The fundamentals (Strings, collections, exceptions) do not change between versions and make up the bulk of the questions.

How long is a Java MCQ on SkillValue or CodinGame?

Expect 20 to 40 questions in 30 to 60 minutes, roughly 90 seconds per question. Tests usually mix code-reading MCQs with one or two small implementation exercises.

Do the questions cover Spring or only the core language?

A core Java test stays on the language and the standard library. If the job posting mentions Spring or Hibernate, expect a dedicated block: dependency injection, common annotations, bean lifecycle, JPA.

How can I improve quickly on language traps?

Run every snippet you hesitate on yourself in jshell or an IDE: seeing the real output fixes the rule better than rereading it. Then redo this MCQ without looking at the answers and note the topics you still get wrong.

Other languages

★ 5,0 · 16 avis · 928 utilisateurs Add to Chrome