2024-06-24
Most languages have a lot of funny semantics. Java is no exception.
I also threw in a few trick questions just to make it a bit harder.
Your current results (e.g. 5 correct out of 15) are tracked at the bottom of the page.
It's only 25 questions. How hard can it be?
1.
Consider the following code segment.
public static String calculate() {
try {
throw new RuntimeException();
} catch (RuntimeException e) {
return "Cats are the best!";
} finally {
return "Dogs are the best!";
}
}
What is the result of calling the calculate function?
2.
Consider the following code segment.
List<Integer> numbers = new ArrayList<>();
numbers.add(1);
List<String> strings = (List<String>) (Object) numbers;
strings.add("Hello, world!");
strings.forEach(System.out::println); This code does not function as intended. Which part of this code will cause an error or exception?
3.
Which of the following is not a keyword in the Java language?
4.
Consider the following code segment.
public class Main {
public class Inner {
}
}
What is the correct way to create an instance of the Inner class, that exists inside the top-level class Main, from an external, static function?
5.
How does Java determine whether or not a given number n of type double is NaN?
6.
Consider the following code segment.
public class Output {
public Output() {
output(null);
}
public void output(Long t) {
System.out.println("Output channel one: " + t);
}
public void output(Integer t) {
System.out.println("Output channel two: " + t);
}
} This code does not compile. What is a correct way to adjust the program so that it compiles?
7.
Which encoding standard is used by default for serializing the String type (e.g. to a DataOutputStream)?
8.
Which of the following code segments are examples of syntactically valid expressions?
- I.
var x = List.of(); - II.
List x = List.<String>of(); - III.
let x = List<String>.of();
9.
Consider the following code segment.
public static int determineSum() {
int sum = 0;
int counter = 0;
int reversed = 10;
while (true) {
if (counter > 5) {
return sum;
}
sum += counter;
counter++;
if (reversed > counter) {
reversed--;
sum += reversed;
}
}
return -1;
}
What is the result of calling the determineSum() function?
10.
What is the result of evaluating the following code segment?
Integer.valueOf(0) == Integer.valueOf(0) 11.
Which of the following are valid calls of a function with the signature void count(int... numbers)?
- I.
count({5}); - II.
count(5); - III.
count(new int{5});
12.
Consider the following code segment.
public static String handle(Number a) {
if (a instanceof Integer) {
return a == null ? "Null integer!" : "Integer!";
} else if (a == Long.valueOf(5L)) {
return "Long equal to five!";
} else if (a == null) {
return "Null object!";
} else if (a instanceof Number) {
return a == null ? "Null number!" : "Number!";
} else {
return "Unknown object!";
}
}
Which of the following results is not a possible return value from a call of the handle function?
13.
Consider the following code segment.
public static void main(String[] args) {
// placeholder line
do {
System.out.println("Hello, world!");
} while (Math.random() > 0.5);
}
The provided segment, in its current state, compiles successfully. Which of the following lines of code could replace // placeholder line without causing a compile error?
14.
Consider the following method.
public int mystery(int number) {
if (number == 0) {
return 0;
} else if (number % 10 == 0) {
return 10 + mystery(number / 10);
} else if (number % 5 == 0) {
return 5 + mystery(number / 5);
} else {
return number % 10 + mystery(number / 10);
}
}
What is the result of calling mystery(120500)?
15.
Consider the following code segment.
Which of the following lines of code could replace /* placeholder code */ without causing a compile error?
public void end() {
/* placeholder code */
} - I.
throw (RuntimeException) null; - II.
throw (Throwable) null; - III.
throw null;
16.
Consider the following code segment.
public class Wrapper<T> {
final T[] objects;
public Wrapper() {
/* placeholder code */
}
}
Which of the following code segments correctly initializes objects without an exception or error?
17.
An variable int x is declared, but left uninitialized. How could the variable be placed such that x could be read from without resulting in an error?
- I. Statically (e.g.
static int x;) - II. As an instance variable (e.g.
int x;) - III. A variable in a function (e.g.
int x;inside a function body)
18.
What is printed when executing the following code segment?
StringBuilder builder = new StringBuilder("😠");
builder.append(8 ^ 7);
builder.append(0.0);
System.out.println(builder.toString().length()); 19.
Which of the following expressions evaluates to true?
20.
What is the difference between errors and exceptions in Java?
21.
Consider the following code segment.
enum Animal {
CAT("Cat"),
DOG("Dog"),
HAMSTER("Hamster");
private String name;
public Animal(String name) {
this.name = name;
}
private String getName() {
return name;
}
} Why does the above code segment fail to compile?
22.
Which of the following code segments successfully initializes a 2-dimensional array of int?
23.
Consider the following code segment.
List<Integer> numbers = new ArrayList<>() {{
add(5);
}}; Which of the following is a correct statement made about the segment?
24.
Consider the following code segments. Which segment(s) print Hello, world! when run, without an exception or compile error?
-
I.
new Runnable() { @Override public void run(Consumer<String> print) { print.accept("Hello, world!"); } }.run(string -> System.out.println(string)); -
II.
new Object() { void foo(String s) { System.out.println(s); } }.foo("Hello, world!"); -
III.
((Consumer<String>) System.out::println).accept("Hello, world!");
25.
Consider the following code segment.
public class Main {
public static void main(String[] args) {
new Main();
new Main();
}
public Main() {
System.out.print("A");
}
static {
System.out.print("B");
}
{
System.out.print("C");
}
static {
System.out.print("D");
}
{
System.out.print("E");
}
}
What is the printed when calling the main function?
Results
You got 0 correct out of 0.
(if there weren't 25 total, go back and answer them all!)
The average score was 11 from a sample size of 13. Do note that this includes a fair number of super nerds. Did you beat the average Java user?
Found my content on a site or in a video? Any questions or corrections? Please let me know on Discord at @GoldenStack
Note: I'm aware #1 is common to most languages with try & catch, #5 is specified by IEEE, and #13 is common to all languages with the two specific features required for the correct answer. I added them because they make good questions here :)
Website design stolen from Cody @ codyq.dev
It's for a good cause, I promise: supporting quiz answers.