Java Challenge #9: Tokenizer

It is so important to know that Tokenizer exists and to know what tools you have available to make your code powerful!

Try to solve this challenge before seeing the answer below.


Answer: This is an easy one. We created the object Scanner passing the String to be tokenized, separated by a pattern. At first, you’ve got to know what the symbols on the tokens mean.

This symbol is called Regex. Many languages use the same concept:

In simple words, this symbol “[^\\w*]” means “not a word” so it will tokenize by the comma “,“.

The result is:

Java Challenge #9: Tokenizer

Java Challenge #8: Enums Implementing Interfaces

Enums, do you really master them? Can you tell me if it’s going to compile? If not, explain why. If so, write the result.

Try to solve this challenge before seeing the answer below.


Answer: The first thing to observe is the implementation of the interface Invoker on the Enum. It’s not very common to do this, but it’s possible. When we access the Enum reference of the reference it does not make a difference. The invokeShenLong() method will be invoked in the same way bringing all the Enums declared on DragonBalls. When we enter in the loop, we are not using brackets ({}) on the “if” command. This means that only one line will be executed.

Conclusion: Until the Enums are equaled, the looping will continue. In the end, the iterated Enums will be FIFTH and equal to one another.

The output will be:
Same dragon ball

Java Challenge #8: Enums Implementing Interfaces

JAVA CHALLENGE #7: Interfaces and Abstract classes

Who is stronger? Master Yoda or Darth Vader? What will the output be? Solve this challenge and find out!

Before seeing the answer below, try it out!


Answer: At first we create an anonymous inner class of Darth Vader, that means any class that implements DarthVader interface. We have only one getNumber() method.

With MasterYoda, things are different. We created an abstract class with the getNumber() method and another one in the inner class of MasterYoda. This is the same as DarthVader, it is an anonymous inner class. This challenge is pretty tricky because you can see that there is a method overloading both methods. Which one will be invoked? See that we are passing a new Integer(FORCE) for the getNumber() method. For this reason, the method from the abstract class will be invoked and MasterYoda will have the force of 20.

The answer is:
The stronger Jedi is: Master Yoda

May the force be with you!

JAVA CHALLENGE #7: Interfaces and Abstract classes

Java Challenge #6: Thread Start

Threads! We commonly don’t deal with them directly on day-to-day work. What will the wolverineAdrenaline be?

Try to solve this challenge before seeing the answer below.


Answer: Remember, Threads execution is always uncertain and depends on the JVM. The order of execution is always random.

Conclusion: There is no exact result. The Threads will be executed randomly. wolverineAdrenaline could be 10, 11, 12, 13 or 14.

Java Challenge #6: Thread Start

Java Challenge #5: Logical and Bitwise Operators

Have you mastered logical operators?

Try to sum these numbers before seeing the answer below.


Answer: There are some important concepts here. The first concept is the bitwise operators. They will be checked wherever the result is true or false. For example, when we use logical operators like:

/* The second condition won't be executed,
because it's not necessary, once you are
using "&&", both conditions must be "true"
when one of them is false, JVM just ignores the rest. */
if (false && true) {


When we use bitwise operators:

/* The second condition will be checked,
even if the first condition is false,
 this can be useful when you want to
execute a command in every situation on your conditions. */
if (false & true) {


When we use the command “||” the same rule is applied here. See the example:

/* The second condition won't be
executed because once we are using "||",
if one of the conditions is "true" the
whole condition will be true, so JVM won't
check the next condition. */
if (true || false) {


/* When using the bitwise operator,
both conditions will be check,
even if the first condition is "true" */
if (true | false) {


We must know the incremental operator. When you use it before the variable it will increment in the line you used it. When you use the post-incremental operator it will increment the next time you use the variable. For example:

/* In the first condition, the variable
"i" will be 5 and on the other will be 6,
so both conditions will be true */
int i = 5;
if (i++ == 5 || i++ == 6) {}

/* In the first condition the variable
will be 6 and in the next will be 7,
so both conditions will be false */
int i = 5;
if (++i == 5 && ++i == 6) {}

Knowing all these concepts, now I can give the answer, it is 74! Try this challenge, debug it, and see the concepts in a practical way. This is how you master a programming language!

Java Challenge #5: Logical and Bitwise Operators

Java Challenge #4: Switch Case

Switch case challenge – what will the result be? Don’t test the code. Answer the question by reading it!

Try to solve this challenge before seeing the answer below.


Answer: “MnNeH”. Pretty easy, huh? But the “switch case” command can be tricky. The formula number of heisenberg is 50, so the second case will be executed until the break of the “case 5”, even if the condition is not true.

Java Challenge #4: Switch Case

Java Challenge #3: Garbage Collector

Good challenge! How many objects will be eligible for the garbage collector after the executeAction() invocation?

Try to solve this challenge before seeing the answer below.

public class GarbageCollectorTest {

    public static void main(String[] args) {
	HowManyObjectsWillBeCleanedByTheGarbageCollector gc = new GarbageCollectorTest()
		.new HowManyObjectsWillBeCleanedByTheGarbageCollector();


	System.out.println("How many objects are eligible be collected by "
		+ "the Garbage Collector after "
		+ "the executeAction() method invokation?" + gc);

    private class HowManyObjectsWillBeCleanedByTheGarbageCollector {
	private Character homer = new Character("Dooh");
	private Character misterBurns = new Character("you are fired");
	private Character bart = new Character("eat my shorts");
	private Character elBarto;

	public void executeAction() {;;
	    makeTwoCharactersTalk(misterBurns, homer);

	    misterBurns = null;

	    elBarto = bart;
	    bart = null;;

	private void makeTwoCharactersTalk(Character misterBurns, Character homer) {;;

	    misterBurns = null;
	    homer = null;

    // class Character omitted

class Character {
    private String talk;

    public Character(String talk) { = talk;

    public void talk() {

Answer: Only one object, misterBurns! When we invoke the makeTwoCharactersTalk we pass the objects by reference and not by value. This means the object is still in the Heap. We also pass the reference of bart to elBarto. When we pass null to bart, we still have the object of Bart into elBarto.

Java Challenge #3: Garbage Collector