Java Level 3 Test
A new cook in a busy kitchen doesn't need to invent recipes. What they need is to know where every tool is and which one fits each job: the sharp knife, not the bread knife, for onions. This level of the course was about Java's kitchen drawer: strings, exceptions, generics, collections, streams, dates and files. This test checks that you reach for the right tool, and that you know how the most used ones, like HashMap, work inside.
How the Test Works
- What's in it: 20 multiple-choice questions, then 3 output puzzles where you work out what a short program prints.
- 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 "Strings in Java" to serialization.
Part 1: 20 Questions
Part 2: Output Puzzles
Work out what each program prints, on paper, before you open the solution. The solution shows the full program, its real output and a Try it button. Give yourself one point for each puzzle you get exactly right.
Puzzle 1: Built at Runtime
What is printed?
String s1 = "Java";
String s2 = "Ja";
String s3 = s2 + "va";
System.out.println(s1 == s3);
System.out.println(s1 == s3.intern());Show solution
public class InternPuzzle {
public static void main(String[] args) {
String s1 = "Java";
String s2 = "Ja";
String s3 = s2 + "va";
System.out.println(s1 == s3);
System.out.println(s1 == s3.intern());
}
}false
trueWhy this output?
s2 is a variable, not a constant, so s2 + "va" builds a new String object at runtime, and == compares references. intern() returns the pooled "Java", which is the same object as s1. Compare this with question 1, where both sides were constants. Revise: String Intern Method in Java.
Puzzle 2: Return and Finally
What is printed?
static int test() {
try {
return 1;
} finally {
System.out.print("finally ");
}
}
// in main:
System.out.println(test());Show solution
public class FinallyPuzzle {
static int test() {
try {
return 1;
} finally {
System.out.print("finally ");
}
}
public static void main(String[] args) {
System.out.println(test());
}
}finally 1Why this output?
return 1 decides the value, but before the method actually returns, finally runs and prints "finally ". Only then does main receive 1 and print it. Revise: Finally Block in Java.
Puzzle 3: A Short Stream
What is printed?
List<String> names = List.of("Navika", "Srihitha", "Advitha");
System.out.println(names.stream()
.filter(n -> n.length() > 6)
.map(String::toUpperCase)
.sorted()
.toList());Show solution
import java.util.List;
public class StreamPuzzle {
public static void main(String[] args) {
List<String> names = List.of("Navika", "Srihitha", "Advitha");
System.out.println(names.stream()
.filter(n -> n.length() > 6)
.map(String::toUpperCase)
.sorted()
.toList());
}
}[ADVITHA, SRIHITHA]Why this output?
"Navika" has 6 letters, so the filter drops it. "Srihitha" (8) and "Advitha" (7) pass, are changed to upper case, and are sorted alphabetically. toList() prints like any list, in square brackets. Revise: Stream Filter, Map and Reduce.
What Your Score Means
| Score (out of 23) | What it means | What to do next |
|---|---|---|
| 20 to 23 | You know the core library well | Move on to Multithreading in Java |
| 16 to 19 | A pass, with a few gaps | Read the lessons linked from the questions you missed, then move on |
| Under 16 | Not yet | Reread the lessons behind your wrong answers, rewrite a few loops as streams, and take the test again |
Was this tutorial helpful?