Java Level 3 Test

Naveen AnamaneniUpdated · 8 min read

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

Level 3 test: 20 questions

20 questions
1What does System.out.println("a" + "b" == "ab"); print?
2Which is thread-safe?
3What does "a1b22c333".replaceAll("\\d+", "#") return?
4Which call reads a whole small text file into one String?
5When does a finally block not run?
6Which class turns a byte stream (InputStream) into a character stream, using a charset?
7Which multi-catch fails to compile?
8What happens with List<Object> list = new ArrayList<String>();?
9A method that adds Integer values to a list should take which parameter type?
10What does type erasure mean?
11What does HashMap allow?
12A new HashMap() has which default capacity and load factor?
13When does a HashMap bucket turn from a linked list into a red-black tree?
14Which is the right way to measure how long a piece of code takes?
15Removing elements from an ArrayList inside a for-each loop over it usually:
16What happens to a transient field during serialization?
17Which is a terminal stream operation?
18What happens if you use a stream again after its terminal operation?
19What does Optional.of(null) do?
20Which class represents a date with no time and no time zone?

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?

Java
String s1 = "Java";
String s2 = "Ja";
String s3 = s2 + "va";
System.out.println(s1 == s3);
System.out.println(s1 == s3.intern());
Show solution
InternPuzzle.java▶ Try it
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());
    }
}
Output
false
true
Why 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?

Java
static int test() {
    try {
        return 1;
    } finally {
        System.out.print("finally ");
    }
}
 
// in main:
System.out.println(test());
Show solution
FinallyPuzzle.java▶ Try it
public class FinallyPuzzle {
    static int test() {
        try {
            return 1;
        } finally {
            System.out.print("finally ");
        }
    }
 
    public static void main(String[] args) {
        System.out.println(test());
    }
}
Output
finally 1
Why 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?

Java
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
StreamPuzzle.java▶ Try it
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());
    }
}
Output
[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 meansWhat to do next
20 to 23You know the core library wellMove on to Multithreading in Java
16 to 19A pass, with a few gapsRead the lessons linked from the questions you missed, then move on
Under 16Not yetReread the lessons behind your wrong answers, rewrite a few loops as streams, and take the test again

Was this tutorial helpful?