Skip to content

Instantly share code, notes, and snippets.

@riking
Last active December 13, 2015 18:18
Show Gist options
  • Save riking/4953940 to your computer and use it in GitHub Desktop.
Save riking/4953940 to your computer and use it in GitHub Desktop.
Java puzzler. From a Google Tech Talk.
import java.util.Set;
import java.util.HashSet;
public class ShortsPuzzle {
public static void main(String[] args) {
Set<Short> shortSet = new HashSet<Short>();
for (short i = 0; i < 100; i++)
{
shortSet.add(i);
shortSet.remove(i - 1);
}
System.out.println(shortSet.length());
}
}
/*
What will this program output?
a) 0
b) 1
c) 100
d) None of the above
"None of the above" could mean that it outputs something not in the answer
choices, throws an exception, or varies from run to run.
It does not include the possibility of the program failing to compile.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment