The more we work with Java 8, the more we go into the rabbit hole. Did they add all those streams, lambdas, monads, Optionals and CompletableFutures only to confuse us? It surely looks so! And Java 9 that heads our way brings even more of what we like the most, more puzzlers, of course! In this season we as usual have a great batch of the best Java WTF, great jokes to present them and great prizes for the winners!
36. public class PerfectRobbery {
private Semaphore bankAccount = new Semaphore(-42);
public static void main(String[] args) {
PerfectRobbery perfectRobbery = new PerfectRobbery();
perfectRobbery.takeAllMoney();
perfectRobbery.checkBalance();
}
public void takeAllMoney(){
bankAccount.drainPermits();
}
public void checkBalance(){
System.out.println(bankAccount.availablePermits());
}
}
37. public class PerfectRobbery {
private Semaphore bankAccount = new Semaphore(-42);
public static void main(String[] args) {
PerfectRobbery perfectRobbery = new PerfectRobbery();
perfectRobbery.takeAllMoney();
perfectRobbery.checkBalance();
}
public void takeAllMoney(){
bankAccount.drainPermits();
}
public void checkBalance(){
System.out.println(bankAccount.availablePermits());
}
}
A. IllegalArgumentException – can’t create semaphore with negative
38. public class PerfectRobbery {
private Semaphore bankAccount = new Semaphore(-42);
public static void main(String[] args) {
PerfectRobbery perfectRobbery = new PerfectRobbery();
perfectRobbery.takeAllMoney();
perfectRobbery.checkBalance();
}
public void takeAllMoney(){
bankAccount.drainPermits();
}
public void checkBalance(){
System.out.println(bankAccount.availablePermits());
}
}
A. IllegalArgumentException – can’t create semaphore with negative
B. UnsupportedOperationException – can’t drain when negative
39. public class PerfectRobbery {
private Semaphore bankAccount = new Semaphore(-42);
public static void main(String[] args) {
PerfectRobbery perfectRobbery = new PerfectRobbery();
perfectRobbery.takeAllMoney();
perfectRobbery.checkBalance();
}
public void takeAllMoney(){
bankAccount.drainPermits();
}
public void checkBalance(){
System.out.println(bankAccount.availablePermits());
}
}
A. IllegalArgumentException – can’t create semaphore with negative
B. UnsupportedOperationException – can’t drain when negative
C. 0
40. public class PerfectRobbery {
private Semaphore bankAccount = new Semaphore(-42);
public static void main(String[] args) {
PerfectRobbery perfectRobbery = new PerfectRobbery();
perfectRobbery.takeAllMoney();
perfectRobbery.checkBalance();
}
public void takeAllMoney(){
bankAccount.drainPermits();
}
public void checkBalance(){
System.out.println(bankAccount.availablePermits());
}
}
A. IllegalArgumentException – can’t create semaphore with negative
B. UnsupportedOperationException – can’t drain when negative
C. 0
D. -42
41. public class PerfectRobbery {
private Semaphore bankAccount = new Semaphore(-42);
public static void main(String[] args) {
PerfectRobbery perfectRobbery = new PerfectRobbery();
perfectRobbery.takeAllMoney();
perfectRobbery.checkBalance();
}
public void takeAllMoney(){
bankAccount.drainPermits();
}
public void checkBalance(){
System.out.println(bankAccount.availablePermits());
}
}
A. IllegalArgumentException – can’t create semaphore with negative
B. UnsupportedOperationException – can’t drain when negative
C. 0
D. -42
45. A. IllegalArgumentException – can’t create semaphore with negative
B. UnsupportedOperationException – can’t drain when negative
C. 0
D. -42
public class PerfectRobbery {
private Semaphore bankAccount = new Semaphore(-42);
public static void main(String[] args) {
PerfectRobbery perfectRobbery = new PerfectRobbery();
perfectRobbery.takeAllMoney();
perfectRobbery.checkBalance();
}
public void takeAllMoney(){
bankAccount.drainPermits();
}
public void checkBalance(){
System.out.println(bankAccount.availablePermits());
}
}
55. A. true/true
B. true/false
C. false/true
Collections.emptyList() == Collections.emptyList();
Collections.emptyIterator() == Collections.emptyIterator();
@gamussa #PhillyETE
56. A. true/true
B. true/false
C. false/true
D. false/false
Collections.emptyList() == Collections.emptyList();
Collections.emptyIterator() == Collections.emptyIterator();
@gamussa #PhillyETE
57. A. true/true
B. true/false
C. false/true
D. false/false
Collections.emptyList() == Collections.emptyList();
Collections.emptyIterator() == Collections.emptyIterator();
@gamussa #PhillyETE
63. A. true/true
B. true/false
C. false/true
Spliterators.emptySpliterator() == Spliterators.emptySpliterator();
Stream.empty() == Stream.empty();
64. A. true/true
B. true/false
C. false/true
D. false/false
Spliterators.emptySpliterator() == Spliterators.emptySpliterator();
Stream.empty() == Stream.empty();
65. A. true/true
B. true/false
C. false/true
D. false/false
Spliterators.emptySpliterator() == Spliterators.emptySpliterator();
Stream.empty() == Stream.empty();
68. A. true/true
B. true/false
C. false/true
D. false/false
Spliterators.emptySpliterator() == Spliterators.emptySpliterator();
Stream.empty() == Stream.empty();
@gamussa #PhillyETE
73. ”Identical”
1. Has the same state
2. Not related to “equals and hashcode” contract
3. Not related to references to objects in
memory
@gamussa #PhillyETE
74. List[] twins = new List[2];
Arrays.setAll(twins, ArrayList::new);
@gamussa #PhillyETE
75. List[] twins = new List[2];
Arrays.setAll(twins, ArrayList::new);
A. Absolutely identical empty lists
@gamussa #PhillyETE
76. List[] twins = new List[2];
Arrays.setAll(twins, ArrayList::new);
A. Absolutely identical empty lists
B. Absolutely identical non-empty lists
@gamussa #PhillyETE
77. List[] twins = new List[2];
Arrays.setAll(twins, ArrayList::new);
A. Absolutely identical empty lists
B. Absolutely identical non-empty lists
C. Non-identical empty lists
@gamussa #PhillyETE
78. List[] twins = new List[2];
Arrays.setAll(twins, ArrayList::new);
A. Absolutely identical empty lists
B. Absolutely identical non-empty lists
C. Non-identical empty lists
D. Non-identical non-empty lists
@gamussa #PhillyETE
79. List[] twins = new List[2];
Arrays.setAll(twins, ArrayList::new);
A. Absolutely identical empty lists
B. Absolutely identical non-empty lists
C. Non-identical empty lists
D. Non-identical non-empty lists
@gamussa #PhillyETE
88. A. WTF?! ’Single’ means one, not three!
public interface Single<T> {
default void partyHard(String songName) { System.out.println(songName); }
void partyHard(T songName);
void drinkIn(T drinkName);
void drinkIn(String dringName);
}
@FunctionalInterface
public interface SingleAndHappy extends Single<String> { }
@gamussa #PhillyETE
89. A. WTF?! ’Single’ means one, not three!
B. Problem is with partyHard(T), remove it and it will work
public interface Single<T> {
default void partyHard(String songName) { System.out.println(songName); }
void partyHard(T songName);
void drinkIn(T drinkName);
void drinkIn(String dringName);
}
@FunctionalInterface
public interface SingleAndHappy extends Single<String> { }
@gamussa #PhillyETE
90. A. WTF?! ’Single’ means one, not three!
B. Problem is with partyHard(T), remove it and it will work
C. Problem is the drinkIn methods, removing one of them and it will
work
public interface Single<T> {
default void partyHard(String songName) { System.out.println(songName); }
void partyHard(T songName);
void drinkIn(T drinkName);
void drinkIn(String dringName);
}
@FunctionalInterface
public interface SingleAndHappy extends Single<String> { }
@gamussa #PhillyETE
91. A. WTF?! ’Single’ means one, not three!
B. Problem is with partyHard(T), remove it and it will work
C. Problem is the drinkIn methods, removing one of them and it will
work
D. It will work fine! Both partyHard() and drinkIn() are merged in
SingleAndHappy, leaving one abstract method
public interface Single<T> {
default void partyHard(String songName) { System.out.println(songName); }
void partyHard(T songName);
void drinkIn(T drinkName);
void drinkIn(String dringName);
}
@FunctionalInterface
public interface SingleAndHappy extends Single<String> { }
@gamussa #PhillyETE
92. A. WTF?! ’Single’ means one, not three!
B. Problem is with partyHard(T), remove it and it will work
C. Problem is the drinkIn methods, removing one of them and it will
work
D. It will work fine! Both partyHard() and drinkIn() are merged in
SingleAndHappy, leaving one abstract method
public interface Single<T> {
default void partyHard(String songName) { System.out.println(songName); }
void partyHard(T songName);
void drinkIn(T drinkName);
void drinkIn(String dringName);
}
@FunctionalInterface
public interface SingleAndHappy extends Single<String> { }
@gamussa #PhillyETE
93.
94.
95. A. WTF?! ’Single’ means one, not three!
B. Problem is with partyHard(T), remove it and it will work
C. Problem are the drinkIn methods, removing it will leave one
abstract method
D. Yes! Both partyHard() and drinkIn() are merged in SingleAndHappy,
leaving one abstract method
public interface Single<T> {
default void partyHard(String songName) { System.out.println(songName); }
void partyHard(T songName);
void drinkIn(T drinkName);
void drinkIn(String dringName);
}
@FunctionalInterface
public interface SingleAndHappy extends Single<String> { }
@gamussa #PhillyETE
98. Hacking the bank
☑Bank software written in Java
☑Hack into it
☑Analyze the accounts
@gamussa #PhillyETE
99. Given the code above, which statement is wrong:
Set<String> accounts= new HashSet<>(Arrays.asList("Gates", "Buffett", "Bezos", "Zuckerberg"));
System.out.println(”accounts= " + accounts);
@gamussa #PhillyETE
100. Given the code above, which statement is wrong:
A. The Set is ordered by hashcode
Set<String> accounts= new HashSet<>(Arrays.asList("Gates", "Buffett", "Bezos", "Zuckerberg"));
System.out.println(”accounts= " + accounts);
@gamussa #PhillyETE
101. Given the code above, which statement is wrong:
A. The Set is ordered by hashcode
B. The order is predictable across multiple runs of the JVM on the same machine
Set<String> accounts= new HashSet<>(Arrays.asList("Gates", "Buffett", "Bezos", "Zuckerberg"));
System.out.println(”accounts= " + accounts);
@gamussa #PhillyETE
102. Given the code above, which statement is wrong:
A. The Set is ordered by hashcode
B. The order is predictable across multiple runs of the JVM on the same machine
C. The order of elements in Set is not predictable
Set<String> accounts= new HashSet<>(Arrays.asList("Gates", "Buffett", "Bezos", "Zuckerberg"));
System.out.println(”accounts= " + accounts);
@gamussa #PhillyETE
103. Given the code above, which statement is wrong:
A. The Set is ordered by hashcode
B. The order is predictable across multiple runs of the JVM on the same machine
C. The order of elements in Set is not predictable
D. Statements A & B are correct
Set<String> accounts= new HashSet<>(Arrays.asList("Gates", "Buffett", "Bezos", "Zuckerberg"));
System.out.println(”accounts= " + accounts);
@gamussa #PhillyETE
104. Given the code above, which statement is wrong:
A. The Set is ordered by hashcode
B. The order is predictable across multiple runs of the JVM on the same machine
C. The order of elements in Set is not predictable
D. Statements A & B are correct
Set<String> accounts= new HashSet<>(Arrays.asList("Gates", "Buffett", "Bezos", "Zuckerberg"));
System.out.println(”accounts= " + accounts);
@gamussa #PhillyETE
105.
106.
107. Given the code above, which statement is wrong:
A. The Set is ordered
B. The order is predictable across multiple runs of the JVM on the same machine
C. The order of elements in Set is not predictable
D. Statements A & B are correct
Set<String> accounts= new HashSet<>(Arrays.asList("Gates", "Buffett", "Bezos", "Zuckerberg"));
System.out.println(”accounts= " + accounts);
@gamussa #PhillyETE
111. Given the code above, which statement is wrong:
Set<String> accounts = Set.of("Gates", "Buffett", "Bezos", "Zuckerberg");
System.out.println(”accounts= " + accounts);
@gamussa #PhillyETE
112. Given the code above, which statement is wrong:
A. The Set is ordered
Set<String> accounts = Set.of("Gates", "Buffett", "Bezos", "Zuckerberg");
System.out.println(”accounts= " + accounts);
@gamussa #PhillyETE
113. Given the code above, which statement is wrong:
A. The Set is ordered
B. The order is predictable across multiple runs of the JVM on the same machine
Set<String> accounts = Set.of("Gates", "Buffett", "Bezos", "Zuckerberg");
System.out.println(”accounts= " + accounts);
@gamussa #PhillyETE
114. Given the code above, which statement is wrong:
A. The Set is ordered
B. The order is predictable across multiple runs of the JVM on the same machine
C. The order of elements in Set is not predictable
Set<String> accounts = Set.of("Gates", "Buffett", "Bezos", "Zuckerberg");
System.out.println(”accounts= " + accounts);
@gamussa #PhillyETE
115. Given the code above, which statement is wrong:
A. The Set is ordered
B. The order is predictable across multiple runs of the JVM on the same machine
C. The order of elements in Set is not predictable
D. Statements A & B are correct
Set<String> accounts = Set.of("Gates", "Buffett", "Bezos", "Zuckerberg");
System.out.println(”accounts= " + accounts);
@gamussa #PhillyETE
116. Given the code above, which statement is wrong:
A. The Set is ordered
B. The order is predictable across multiple runs of the JVM on the same machine
C. The order of elements in Set is not predictable
D. Statements A & B are correct
Set<String> accounts = Set.of("Gates", "Buffett", "Bezos", "Zuckerberg");
System.out.println(”accounts= " + accounts);
@gamussa #PhillyETE
120. Given the code above, which statement is wrong:
A. The Set is ordered
B. The order is predictable across multiple runs of the JVM on the same machine
C. The order of elements in Set is not predictable
D. Statements A & B are correct
Set<String> accounts = Set.of("Gates", "Buffett", "Bezos", "Zuckerberg");
System.out.println(”accounts= " + accounts);
@gamussa #PhillyETE
121. private int probe(Object pe) {
int idx = Math.floorMod(pe.hashCode() ^ SALT,
elements.length);
while (true) {
E ee = elements[idx];
if (ee == null) {
return -idx - 1;
} else if (pe.equals(ee)) {
return idx;
} else if (++idx == elements.length) {
idx = 0;
}
}
}
@gamussa #PhillyETE
125. What’s correct?
A. If you convert your application to module, classpath
dependencies will still be resolved correctly
@gamussa #PhillyETE
126. What’s correct?
A. If you convert your application to module, classpath
dependencies will still be resolved correctly
B. If one of the dependencies was converted to a module,
you have to declare it in module-info in order to use
@gamussa #PhillyETE
127. What’s correct?
A. If you convert your application to module, classpath
dependencies will still be resolved correctly
B. If one of the dependencies was converted to a module,
you have to declare it in module-info in order to use
C. Once you added the module-info to your project you
have to declare the dependencies twice, in classpath
and in module-info
@gamussa #PhillyETE
128. What’s correct?
A. If you convert your application to module, classpath
dependencies will still be resolved correctly
B. If one of the dependencies was converted to a module,
you have to declare it in module-info in order to use
C. Once you added the module-info to your project you
have to declare the dependencies twice, in classpath
and in module-info
D. None of the above
@gamussa #PhillyETE
129. What’s correct?
A. If you convert your application to module, classpath
dependencies will still be resolved correctly
B. If one of the dependencies was converted to a module,
you have to declare it in module-info in order to use
C. Once you added the module-info to your project you
have to declare the dependencies twice, in classpath
and in module-info
D. None of the above
@gamussa #PhillyETE
130.
131.
132. What’s correct?
A. If you convert your application to module, classpath
dependencies will still be resolved correctly
B. If one of the dependencies was converted to a module,
you have to declare it in module-info in order to use
C. Once you added the module-info to your project you
have to declare the dependencies twice, in classpath
and in module-info
D. None of the above
@gamussa #PhillyETE
137. A. You killed them all
static void killThemAll(Collection<Hero> expendables) {
Iterator<Hero> heroes = expendables.iterator();
heroes.forEachRemaining(e -> {
if (heroes.hasNext()) {
heroes.next();
heroes.remove();
}
});
System.out.println(expendables);
}
138. A. You killed them all
B. You killed only even ones
static void killThemAll(Collection<Hero> expendables) {
Iterator<Hero> heroes = expendables.iterator();
heroes.forEachRemaining(e -> {
if (heroes.hasNext()) {
heroes.next();
heroes.remove();
}
});
System.out.println(expendables);
}
139. A. You killed them all
B. You killed only even ones
C. They all survived
static void killThemAll(Collection<Hero> expendables) {
Iterator<Hero> heroes = expendables.iterator();
heroes.forEachRemaining(e -> {
if (heroes.hasNext()) {
heroes.next();
heroes.remove();
}
});
System.out.println(expendables);
}
140. A. You killed them all
B. You killed only even ones
C. They all survived
D. You killed only odd ones
static void killThemAll(Collection<Hero> expendables) {
Iterator<Hero> heroes = expendables.iterator();
heroes.forEachRemaining(e -> {
if (heroes.hasNext()) {
heroes.next();
heroes.remove();
}
});
System.out.println(expendables);
}
141. A. You killed them all
B. You killed only even ones
C. They all survived
D. You killed only odd ones
E. All answers are correct
static void killThemAll(Collection<Hero> expendables) {
Iterator<Hero> heroes = expendables.iterator();
heroes.forEachRemaining(e -> {
if (heroes.hasNext()) {
heroes.next();
heroes.remove();
}
});
System.out.println(expendables);
}
142. A. You killed them all
B. You killed only even ones
C. They all survived
D. You killed only odd ones
E. All answers are correct
static void killThemAll(Collection<Hero> expendables) {
Iterator<Hero> heroes = expendables.iterator();
heroes.forEachRemaining(e -> {
if (heroes.hasNext()) {
heroes.next();
heroes.remove();
}
});
System.out.println(expendables);
}
145. A. You killed them all
B. You killed only even ones
C. They all survived
D. You killed only odd ones
E. All answers are correct
static void killThemAll(Collection<Hero> expendables) {
Iterator<Hero> heroes = expendables.iterator();
heroes.forEachRemaining(e -> {
if (heroes.hasNext()) {
heroes.next();
heroes.remove();
}
});
System.out.println(expendables);
}
156. A. Both work just fine
@FunctionalInterface
public interface OriginalPredicate<T> {
boolean test(T t);
}
OriginalPredicate<Object> lambda = (Object obj) -> "adidas".equals(obj);
OriginalPredicate<Object> methodRef = "adidas"::equals;
157. A. Both work just fine
B. Lambda works, method ref fails
@FunctionalInterface
public interface OriginalPredicate<T> {
boolean test(T t);
}
OriginalPredicate<Object> lambda = (Object obj) -> "adidas".equals(obj);
OriginalPredicate<Object> methodRef = "adidas"::equals;
158. A. Both work just fine
B. Lambda works, method ref fails
C. Method ref works, lambda fails
@FunctionalInterface
public interface OriginalPredicate<T> {
boolean test(T t);
}
OriginalPredicate<Object> lambda = (Object obj) -> "adidas".equals(obj);
OriginalPredicate<Object> methodRef = "adidas"::equals;
159. A. Both work just fine
B. Lambda works, method ref fails
C. Method ref works, lambda fails
D. Won’t compile
@FunctionalInterface
public interface OriginalPredicate<T> {
boolean test(T t);
}
OriginalPredicate<Object> lambda = (Object obj) -> "adidas".equals(obj);
OriginalPredicate<Object> methodRef = "adidas"::equals;
160. A. Both work just fine
B. Lambda works, method ref fails
C. Method ref works, lambda fails
D. Won’t compile
@FunctionalInterface
public interface OriginalPredicate<T> {
boolean test(T t);
}
OriginalPredicate<Object> lambda = (Object obj) -> "adidas".equals(obj);
OriginalPredicate<Object> methodRef = "adidas"::equals;
161.
162.
163. A. Both work just fine
B. Lambda works, method ref fails
C. Method ref works, lambda fails
D. Not a functional interface, will fail on annotation processing
@FunctionalInterface
public interface CopyCatPredicate {
<T> boolean test(T t);
}
CopyCatPredicate lambda = (Object obj) -> "adadas".equals(obj);
CopyCatPredicate methodRef = "adadas"::equals;
164. A. Both work just fine
B. Lambda works, method ref fails
C. Method ref works, lambda fails
D. Not a functional interface, will fail on annotation processing
@FunctionalInterface
public interface CopyCatPredicate {
<T> boolean test(T t);
}
CopyCatPredicate lambda = (Object obj) -> "adadas".equals(obj);
CopyCatPredicate methodRef = "adadas"::equals;
165.
166.
167.
168. A. Both work just fine
B. Lambda works, method ref fails
C. Method ref works, lambda fails
D. Not a functional interface, will fail on annotation processing
@FunctionalInterface
public interface CopyCatPredicate {
<T> boolean test(T t);
}
CopyCatPredicate lambda = (Object obj) -> "adadas".equals(obj);
CopyCatPredicate methodRef = "adadas"::equals;
190. filter(new TreeSet<>()::add) filter(i -> new TreeSet<>().add(i))!=
New instance is
created every time!
Instance method is
created once!
@gamussa #PhillyETE
199. A. obvious / obvious
B. obvious / NullPointerException
C. NullPointerException / obvious
D. NullPointerException / NullPointerException
Optional.of("obvious").orElseGet(null);
Optional.empty().map(null).orElse("obvious");
@gamussa #PhillyETE
200. A. obvious / obvious
B. obvious / NullPointerException
C. NullPointerException / obvious
D. NullPointerException / NullPointerException
Optional.of("obvious").orElseGet(null);
Optional.empty().map(null).orElse("obvious");
Will never happen
@gamussa #PhillyETE
201. A. obvious / obvious
B. obvious / NullPointerException
C. NullPointerException / obvious
D. NullPointerException / NullPointerException
Optional.of("obvious").orElseGet(null);
Optional.empty().map(null).orElse("obvious");
Will never happen
Will never happen
@gamussa #PhillyETE
202. A. obvious / obvious
B. obvious / NullPointerException
C. NullPointerException / obvious
D. NullPointerException / NullPointerException
Optional.of("obvious").orElseGet(null);
Optional.empty().map(null).orElse("obvious");
Will never happen
Will never happen
@gamussa #PhillyETE
224. Reuse is only possible for pure functions
@gamussa #PhillyETE
225. Reuse is only possible for pure functions
Consumers accept parameters ==
have state
@gamussa #PhillyETE
226. Reuse is only possible for pure functions
Consumers accept parameters ==
have state
Supplier in 4 has state – the
resolved method reference
@gamussa #PhillyETE