Nine Core Java Questions

My previous post with Eight Core Java Questions was popular so I created nine more.

Please join the discussion of the answers for these questions on Twitter

1.

Comment on this question
How many elements does this set have?

Set set = new HashSet(Arrays.asList(
        -0, +0, -0L, +0L, -0F, 0F, -0D, 0D, 0xF, 0xD, 0x0.DP+0));
System.out.println(set.size());
  1. 12
  2. 1
  3. 6
  4. 9

2.

Comment on this question
This compiles in Java 17 due to

static int[] a(int[]... a)[] { return a; }
static final int one = a(new int[0]).length; // 1
  1. It confuses the compiler
  2. It’s a static method
  3. Backward Compatibility
  4. JEP 747

3.

Comment on this question
The code below prints "23 & 52" the following due to

System.out.println(
        new BigDecimal(2.008f).toString().length() + " & " +
                new BigDecimal(2.008).toString().length());
  1. A bug in Java 23
  2. The randomness of floating-point
  3. Length of the mantissa
  4. Magic numbers

4.

Comment on this question
This code prints "hello world" due to

public static void main(String[] args) {
    System.out.println(r(604568751) + " " + r(401174283));
}

public static String r(int num) {
    return Integer.toUnsignedString(new Random(num).nextInt(), 33);
}
  1. Secret code in Java
  2. Using Magic numbers
  3. A bug in Java 13
  4. (char) 33 is '!'

5.

Comment on this question
This is class is

public class StackTrace extends Throwable { }
  1. An unchecked exception
  2. A checked exception
  3. Neither
  4. A compilation error

6.

Comment on this question

This code prints the following in Java 17

   public static void main(String[] args) {
        test(new IdentityHashMap<>());
        test(new WeakHashMap<>());
        test(new ConcurrentSkipListMap<>());
    }

    static void test(Map<String, Integer> map) {
        map.computeIfAbsent("key", (String k) -> {
            map.putIfAbsent(k, 2);
            return 1;
        });
        System.out.println(map.getClass().getSimpleName() + " " + map);
    }

IdentityHashMap {key=1}
WeakHashMap {key=1}
ConcurrentSkipListMap {key=2}
  1. It depends on whether the map is concurrent or not
  2. It’s a memory leak
  3. It's a thread safety issue
  4. The behaviour is undefined.

7.

Comment on this question
In Java 17, the stack trace StackTraceElement[] of a Throwable is created when

  1. It is thrown
  2. It is used for the first time
  3. Never really exists
  4. The Throwable is created

8.

Comment on this question
If -Dfalse=true is set on the command line, which of these is true

  1. (Boolean) false
  2. Boolean.parseBoolean("false")
  3. Boolean.getBoolean("false")
  4. "true" == "false"

9.

Comment on this question
This expression is true for powers of 2 and one other value.

n & ( n - 1 ) == 0
  1. Integer.MAX_VALUE
  2. Character.MIN_VALUE
  3. Double.MIN_VALUE
  4. Float.NaN

Hint

In the questions above, there are a total of three answers for each for B, C,and D

Comments

Popular posts from this blog

Java is Very Fast, If You Don’t Create Many Objects

System wide unique nanosecond timestamps

Comparing Approaches to Durability in Low Latency Messaging Queues