Java Level 5 Test
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
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?
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
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");
}
}
}n = 8: 24 steps
n = 1024: 10240 steps
n = 1048576: 20971520 stepsWhy 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?
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
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));
}
}{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?
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
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";
}
}abcd abcWhy 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 means | What to do next |
|---|---|---|
| 20 to 23 | You have finished the Learn track | Go on to Core Java Interview Questions and build a project of your own |
| 16 to 19 | A pass, with a few gaps | Read the lessons linked from the questions you missed, then move on to interview practice |
| Under 16 | Not yet | Reread the lessons behind your wrong answers, write tests for one of your own programs, and take the test again |
Was this tutorial helpful?