· java,mashup

inspired by Java Champions Rafael Chinelato Del Nero & Paul Webber

originally from Duke’s Star Trek with Java 15 Code Challenges

Useful and fun information

Mission summary

  • JDK6 - Deque
  • JDK7 - AutoCloseable
  • JDK8 - List Stream
  • JDK8 - Lambda for Each
  • JDK9 - Modules
  • JDK11 - Type Inference (intro JDK10)
  • JDK14 - Switch (intro JDK12)
  • JDK14 - Records
  • JDK14 - InstanceOf Pattern Matching
  • JDK15 - Text Blocks (intro JDK13)
  • JDK15 - Sealed Classes

👉 JDK6 - Deque - The confused crew

 What does it produces?

  •  A) Scott, Worf, Worf, Odo
  •  B) Scott, Odo, Worf, null
  •  C) Worf, Odo, Scott, null
  •  D) Odo, Worf, Worf, Scott 

 Push add the element on the top.

 Remove takes the top value, removes it from the list and returns it.

 Peek returns the element from the top without removing it.

 Pop returns the element and removes it (it throw an exception if the list is empty).

 Poll is the same as pop except it does not throw exception if the list is empty.

 ·ꓷ sᴉ ɹǝʍsuɐ ǝɥꓕ

 👉 JDK7 - AutoCloseable - The door malfunction

What does it prints?

A)

B)

C)

D)

At the end of the try block, the resource is closed.

Then the catch block prints.

Then the finally block prints.

·ꓛ sᴉ ɹǝʍsuɐ ǝɥꓕ

👉 JDK8 - List Stream - The unknown order

What does it prints?

 A)

 B)

 C)

 D)

 There are two core concepts here with Streams.

 We have intermediate operations methods which are methods that program the Stream, they're doing none unless we have a terminial operation method.

 The terminal operation method being the trigger of the Stream.

 Here it is forEach.

 Remember Streams are lazy, meaning if we program the stream to limit only for two elements, the JVM won't go through all the elements.

 ·Ɐ sᴉ ɹǝʍsuɐ ǝɥꓕ

 👉 JDK8 - Lambda for Each - The Doors

 What does it prints?

 A)

 B)

 C)

 D)

 Outside variables inside lambda functions must be final or effectively final, meaning they can't be changed.

 So when doing doorNumber++, it is not right, the compiler fails.

 If you wanna change a variable in a lambda, make it static.

 ·ꓛ sᴉ ɹǝʍsuɐ ǝɥꓕ

 👉 JDK9 - Modules - Modules conundrum

 Given this module infos:

 And this classes:

  •  From risa package:
  • From vulcan package:

A)

B)

C)

D)

We are trying to break encapsulation here: "method.setAccessible(true);" on the "private void attack(String shipName)" method.

Unless we open the package, we can't break encapsulation.

To make it works, you would have to add "opens com.defiant to risa;" in the vulcan module info.

This show the powerful encapsulation provided by the JPMS (Java Platform Module System).

Note aside, JPMS allows you to choose what you import in your application (no useless packages), making app interestingly light.

 ·ꓛ sᴉ ɹǝʍsuɐ ǝɥꓕ

👉 JDK11 - Type Inference (intro JDK10) - Var turnabout intruder

What does it prints?

A)

B)

C) 

D)

 Var can be used as a name also - besides it is not recommended.

 Null must be cast with var.

 Var can be used as a lambda modifier.

 In the list remove method, we parse an Intger object, the remove will seek after it and if it finds it, will remove it.

 ·ꓭ sᴉ ɹǝʍsuɐ ǝɥꓕ

👉 JDK14 - Switch (intro JDK12) - Starship switch

What does it prints?

A)

B)

C)

D)

valueOf is implemented such that certain numbers are "pooled", and it returns the same instance for values smaller than 128.

It would have return '7' with 'Integer.valueOf(129) == Integer.valueOf(129) ? 10 : 7'.

Here it is a literal on second part so the comparison is done between literals ('Integer.valueOf(129) being unboxed) giving 10.

When there are more than one line, include "yield".

You can use lambda in a switch.

For the String comparison, as we're using new keyword, we create a new instance, so the references are different (which is what is compared with '=='), returning 4.

You don't use 'throw' clause in switch when throwing an exception.

·ꓭ sᴉ ɹǝʍsuɐ ǝɥꓕ

👉 JDK14 - Records 

What does it prints?

A)

B)

C)

D)

NewShip is not serializable (it should have use the clause 'implements Serilizable').

 ·ꓛ sᴉ ɹǝʍsuɐ ǝɥꓕ

👉 JDK14 - InstanceOf Pattern Matching

What does it print?

A)

B)

C)

D)

In line 3, there is no typo. It's one and 'L' therefore Object is a String.

A String is not a buffer, it complies with second if, but the equals result is false.

In line 4, the boolean is auto-boxed to object Boolean.

It is a comparable and the equals renders false.

In line 5, we invoked a StringBuffer but it calls toString, then it is a String.

Its equals processing renders false.

In line 6, the RuntimeException is not a Comparable but it is a Serializable.

So it prints java.lang.RuntimeException: Disruptor:serializable.

Look at the last two instanceof, in the following block it does no more cast the object declared in the instanceof (That is the new magic of PatternMatching).

·ꓷ sᴉ ɹǝʍsuɐ ǝɥꓕ

👉 JDK15 - Text Blocks (intro JDK13) - Text blocks

What does it prints?

A)

B)

C)

D) 

Look at method preceddence on line 7 and concatenation competed subsitituation succeeds on line 14.

·ꓛ sᴉ ɹǝʍsuɐ ǝɥꓕ

👉 JDK15 - Sealed Classes

What does it prints?

A) Line #A and #B won't compile

B) Line #A and #C won't compile

C) 

D) Line #A, #B and #C won't compile

Captain interface permits Janeway but Janeway does not implements Captain so line #A fails to compile.

You cannot use anonymous classes with sealed classes/interface, so line #C fails to compile.

The purpose of sealed class is for your system design to have more control on your classes (preventing misuse of your classes).

·ꓭ sᴉ ɹǝʍsuɐ ǝɥꓕ