Java Level 5 Test

Naveen AnamaneniUpdated · 9 min read

Knowing how to cook is not the same as running a restaurant kitchen. A restaurant also needs a supplier list, a way to taste every dish before it goes out, a notebook of what went wrong last night, and recipes that the next cook can follow. The last level of the course was about that side of Java: build tools, tests, mocks, logging, clean code, and the algorithms that interviewers like to ask about. This is the final test of the Learn track.

How the Test Works

  • What's in it: 20 multiple-choice questions, then 3 puzzles where you work out what a short program prints, or how fast it runs.
  • Time: 30 minutes, without notes, an IDE or JShell.
  • Pass mark: 16 out of 23. The quiz marks the 20 questions for you. Mark the puzzles yourself, one point each, and add them to your quiz score.
  • What it covers: the lessons from "Build Tools in Java: Maven and Gradle" to the interview preparation lessons.

Part 1: 20 Questions

Level 5 test: 20 questions

20 questions
1Which Maven lifecycle phase runs the unit tests?
2Where do test classes go in a standard Maven project?
3Which JUnit annotation runs a method before each test?
4What does when(repo.findById(1)).thenReturn(Optional.of(book)); do in Mockito?
5How do you check that code throws an exception in modern JUnit?
6Why use SLF4J?
7What is the time complexity of binary search, and what does it need?
8What is the worst-case time complexity of quicksort?
9Which is true of merge sort?
10Which data structure does breadth-first search (BFS) use?
11Which JDK class gives you an LRU cache almost for free?
12The sliding-window technique is best for:
13Which JUnit annotation runs the same test once for each of several inputs?
14Which Maven dependency scope keeps JUnit out of your production build?
15What is the average time complexity of HashMap.get?
16A method has 8 parameters. What is the usual fix?
17What should a method that returns a list return when there are no results?
18What is the better way to build a long string inside a loop?
19Why write new ArrayList<>(expectedSize)?
20Which of these is not an LTS release?

Part 2: Puzzles

Work out each answer on paper before you open the solution. The solution shows a full program, its real output and a Try it button. Give yourself one point for each puzzle you get right.

Puzzle 1: Count the Steps

What is the time complexity of this code?

Java
for (int i = 1; i < n; i *= 2)
    for (int j = 0; j < n; j++)
        count++;

The solution counts the steps for three sizes of n, so you can check your answer.

Show solution
LoopCount.java▶ Try it
public class LoopCount {
    static long count(int n) {
        long count = 0;
        for (int i = 1; i < n; i *= 2) {
            for (int j = 0; j < n; j++) {
                count++;
            }
        }
        return count;
    }
 
    public static void main(String[] args) {
        for (int n : new int[] {8, 1024, 1_048_576}) {
            System.out.println("n = " + n + ": " + count(n) + " steps");
        }
    }
}
Output
n = 8: 24 steps
n = 1024: 10240 steps
n = 1048576: 20971520 steps
Why this answer?

O(n log n). The outer loop doubles i each time, so it runs log₂ n times: 3 times for 8, 10 times for 1024, 20 times for 1,048,576. The inner loop runs n times for each of those. The output shows exactly n × log₂ n steps. Revise: Time and Space Complexity (Big-O) in Java.

Puzzle 2: Counting Words

What is printed?

Java
Map<String, Integer> m = new HashMap<>();
for (String w : "to be or not to be".split(" ")) {
    m.merge(w, 1, Integer::sum);
}
System.out.println(new TreeMap<>(m));
Show solution
WordCountPuzzle.java▶ Try it
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
 
public class WordCountPuzzle {
    public static void main(String[] args) {
        Map<String, Integer> m = new HashMap<>();
        for (String w : "to be or not to be".split(" ")) {
            m.merge(w, 1, Integer::sum);
        }
        System.out.println(new TreeMap<>(m));
    }
}
Output
{be=2, not=1, or=1, to=2}
Why this output?

merge puts 1 for a new word and adds 1 with Integer::sum for a word it has seen. Copying the map into a TreeMap sorts the keys alphabetically, so the order is fixed. Revise: Map Methods in Java.

Puzzle 3: StringBuilder and String in a Method

What is printed?

Java
public static void main(String[] args) {
    StringBuilder sb = new StringBuilder("abc");
    String s = "abc";
    change(sb, s);
    System.out.println(sb + " " + s);
}
 
static void change(StringBuilder sb, String s) {
    sb.append("d");
    s = s + "d";
}
Show solution
BuilderPuzzle.java▶ Try it
public class BuilderPuzzle {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("abc");
        String s = "abc";
        change(sb, s);
        System.out.println(sb + " " + s);
    }
 
    static void change(StringBuilder sb, String s) {
        sb.append("d");
        s = s + "d";
    }
}
Output
abcd abc
Why this output?

Both parameters are copies of references. sb.append("d") changes the one shared StringBuilder, so main sees it. s = s + "d" builds a new String and points only the method's own copy at it; strings are immutable, so the original "abc" is untouched. Revise: Call by Value and Call by Reference in Java.

What Your Score Means

Score (out of 23)What it meansWhat to do next
20 to 23You have finished the Learn trackGo on to Core Java Interview Questions and build a project of your own
16 to 19A pass, with a few gapsRead the lessons linked from the questions you missed, then move on to interview practice
Under 16Not yetReread the lessons behind your wrong answers, write tests for one of your own programs, and take the test again

Was this tutorial helpful?