├── .idea ├── .name ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── vcs.xml ├── inspectionProfiles │ ├── Project_Default.xml │ ├── profiles_settings.xml │ └── JavaTrivia.xml ├── modules.xml ├── compiler.xml ├── runConfigurations │ ├── Maps.xml │ ├── Nullify.xml │ ├── NegEquals.xml │ ├── MutableString.xml │ ├── StringReference.xml │ └── PrivateCtorOverride.xml ├── misc.xml ├── projectCodeStyle.xml └── uiDesigner.xml ├── .gitignore ├── README.md ├── src ├── Bytes.java ├── Nullify.java ├── Hello.java ├── NegEquals.java ├── StringReference.java ├── Absolutely.java ├── Maps.java ├── EggsToast.java ├── PrivateCtorOverride.java ├── MutableString.java └── Constants.java └── java-trivia.iml /.idea/.name: -------------------------------------------------------------------------------- 1 | java-trivia -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # IDEA 2 | .idea/workspace.xml 3 | .idea/ant.xml 4 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Java Trivia 2 | =========== 3 | 4 | Thesis: zero people know java. 5 | 6 | J: "but not esoteric stuff like that!" 7 | Z: This esoteric stuff is in your codebase. You wrote it. 8 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /src/Bytes.java: -------------------------------------------------------------------------------- 1 | // Bytes 2 | // ===== 3 | 4 | // What does this program output? 5 | final class Bytes { 6 | public static void main(String[] args) { 7 | for (byte b = 0; b < 255; b++) { 8 | System.out.println(b); 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /src/Nullify.java: -------------------------------------------------------------------------------- 1 | // Nullify 2 | // ======= 3 | 4 | // What is the output of this program? 5 | public final class Nullify { 6 | private static String s; 7 | 8 | private Nullify() { 9 | 10 | } 11 | 12 | public static void main(String[] args) { 13 | Nullify n = null; 14 | n.s = "null!"; 15 | System.out.println(n.s); 16 | } 17 | } -------------------------------------------------------------------------------- /src/Hello.java: -------------------------------------------------------------------------------- 1 | // Hello Ice-cream 2 | // =============== 3 | 4 | // What is the output of this program? 5 | public final class Hello { 6 | // say Hello then go buy an 7 | // ()//()//; \u000A { icecream();} //;()//() 8 | 9 | 10 | public static void main(String[] args) { 11 | new Hello().toString(); 12 | System.out.println("hello"); 13 | } 14 | 15 | void icecream() { 16 | System.out.println("icecream"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/NegEquals.java: -------------------------------------------------------------------------------- 1 | // Negation is Equivalent 2 | // ====================== 3 | 4 | // What argument(s) when passed to this program produces the output: true? 5 | public final class NegEquals { 6 | private NegEquals() { 7 | 8 | } 9 | 10 | public static void printNegXEqualsX(int x) { 11 | System.out.println(-x == x); 12 | } 13 | 14 | public static void main(String[] args) { 15 | printNegXEqualsX(Integer.parseInt(args[0])); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/StringReference.java: -------------------------------------------------------------------------------- 1 | // String References 2 | // ================= 3 | 4 | // What is the output of this program? 5 | // a) s1 == s2: true 6 | // b) s1 == s2: false 7 | // c) compile-error 8 | // d) runtime exception 9 | // e) something else _____ 10 | public final class StringReference { 11 | public static void main(String[] args) { 12 | String s1 = "hello"; 13 | String s2 = "hello"; 14 | System.out.println("s1 == s2: " + s1 == s2); 15 | } 16 | } -------------------------------------------------------------------------------- /java-trivia.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/Absolutely.java: -------------------------------------------------------------------------------- 1 | // Which argument(s), when passed to this program, 2 | // produce the output: true? 3 | public final class Absolutely { 4 | private Absolutely() throws UnsupportedOperationException { 5 | throw new UnsupportedOperationException(); 6 | } 7 | 8 | public static void main(final String[] args) { 9 | absolutely(Integer.parseInt(args[0])); 10 | } 11 | 12 | static void absolutely(int n) { 13 | System.out.println(Math.abs(n) == n && n < 0); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/Maps.java: -------------------------------------------------------------------------------- 1 | // Maps and Equality 2 | // ================= 3 | 4 | // What is the output of this program? 5 | import java.util.Map; 6 | import java.util.IdentityHashMap; 7 | 8 | public final class Maps { 9 | private Maps() { 10 | 11 | } 12 | 13 | public static void main(String... args) { 14 | Map m = new IdentityHashMap(); 15 | m.put(7, 8); 16 | m.put(777, 888); 17 | System.out.println(m.containsKey(7)); 18 | System.out.println(m.containsKey(new Integer(7))); 19 | System.out.println(m.containsKey(777)); 20 | System.out.println(m.containsKey(new Integer(777))); 21 | } 22 | } -------------------------------------------------------------------------------- /src/EggsToast.java: -------------------------------------------------------------------------------- 1 | // Calling Overridable Methods From Constructors 2 | // ============================================= 3 | 4 | // What is the output of this program? 5 | // a) x = 2 6 | // b) x = 3 7 | // c) compile-error 8 | // d) runtime exception 9 | // e) something else _____ 10 | abstract class Eggs { 11 | abstract void m(); 12 | 13 | Eggs() { 14 | m(); 15 | } 16 | } 17 | 18 | class Toast extends Eggs { 19 | private Integer x = 2; 20 | 21 | Toast() { 22 | x = 3; 23 | } 24 | 25 | public void m() { 26 | System.out.println("x = " + x); 27 | } 28 | 29 | public static void main(String[] args) { 30 | new Toast(); 31 | } 32 | } -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /src/PrivateCtorOverride.java: -------------------------------------------------------------------------------- 1 | // Are Private Constructors Overridden? 2 | // ==================================== 3 | 4 | // What is the output of this program? (1) 5 | // a) m 6 | // b) main 7 | // c) compile-error 8 | // d) runtime exception 9 | // e) something else ______ 10 | // 11 | // Now change the access modifier on the printS method to anything but private. 12 | // What is the output of the program now? (2) 13 | // a) m 14 | // b) main 15 | // c) compile-error 16 | // d) runtime exception 17 | // e) something else ______ 18 | class PrivateCtorOverride { 19 | private final String s; 20 | 21 | private void printS() { 22 | System.out.println(s); 23 | } 24 | 25 | PrivateCtorOverride(String s) { 26 | this.s = s; 27 | } 28 | 29 | public static void main(String[] args) { 30 | new PrivateCtorOverride("main").m(); 31 | } 32 | 33 | private void m() { 34 | new PrivateCtorOverride("m") { 35 | void method() { 36 | printS(); 37 | } 38 | }.method(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /.idea/runConfigurations/Maps.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 23 | -------------------------------------------------------------------------------- /.idea/runConfigurations/Nullify.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 23 | -------------------------------------------------------------------------------- /.idea/runConfigurations/NegEquals.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 23 | -------------------------------------------------------------------------------- /.idea/runConfigurations/MutableString.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 23 | -------------------------------------------------------------------------------- /.idea/runConfigurations/StringReference.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 23 | -------------------------------------------------------------------------------- /.idea/runConfigurations/PrivateCtorOverride.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 23 | -------------------------------------------------------------------------------- /src/MutableString.java: -------------------------------------------------------------------------------- 1 | // Strings are Immutable, Right? 2 | // ============================= 3 | 4 | // Looking only at the main method below, can you guarantee the output of the program? 5 | import java.lang.reflect.Field; 6 | 7 | public final class MutableString { 8 | private MutableString() { 9 | throw new UnsupportedOperationException(); 10 | } 11 | 12 | public static void main(String[] args) { 13 | String s = "hello"; 14 | System.out.println(s); 15 | method(s); 16 | System.out.println(s); 17 | } 18 | 19 | private static void method(String s) { 20 | try { 21 | Field f = String.class.getDeclaredField("value"); 22 | mute(f, s); 23 | return; 24 | } catch (NoSuchFieldException ex) { 25 | Field[] all = String.class.getDeclaredFields(); 26 | for (Field a : all) { 27 | if (a.getType().equals(char[].class)) { 28 | mute(a, s); 29 | return; 30 | } 31 | } 32 | } 33 | 34 | // can't do it 35 | } 36 | 37 | private static void mute(Field f, String s) { 38 | f.setAccessible(true); 39 | StringBuffer sb = new StringBuffer(s); 40 | sb.reverse(); 41 | try { 42 | f.set(s, sb.toString().toCharArray()); 43 | } catch (IllegalAccessException iae) { 44 | System.err.println(iae); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/Constants.java: -------------------------------------------------------------------------------- 1 | import static java.lang.System.out; 2 | 3 | // Constants 4 | // ========= 5 | 6 | // What does this program output? 7 | // You will need to understand Java constants to answer this question. 8 | class S { 9 | static final int I = 7; 10 | 11 | static { 12 | out.println("S"); 13 | } 14 | } 15 | 16 | class T { 17 | static final Object S = null; 18 | 19 | static { 20 | out.println("T"); 21 | } 22 | } 23 | 24 | class U { 25 | static final Object S = "s"; 26 | 27 | static { 28 | out.println("U"); 29 | } 30 | } 31 | 32 | class V { 33 | static final String S = "s"; 34 | 35 | static { 36 | out.println("V"); 37 | } 38 | } 39 | 40 | class W { 41 | static final String S = new String("s"); 42 | 43 | static { 44 | out.println("W"); 45 | } 46 | } 47 | 48 | class X { 49 | static final int I = 7; 50 | 51 | static { 52 | out.println("X"); 53 | } 54 | } 55 | 56 | class Y { 57 | static final int I; 58 | 59 | static { 60 | I = 8; 61 | out.println("Y"); 62 | } 63 | } 64 | 65 | class Z { 66 | static final Integer I = new Integer(7); 67 | 68 | static { 69 | out.println("Z"); 70 | } 71 | } 72 | 73 | class Constants { 74 | public static void main(String[] args) { 75 | int s = new S().I; 76 | Object t = T.S; 77 | Object u = U.S; 78 | String v = V.S; 79 | String w = W.S; 80 | int x = X.I; 81 | int y = Y.I; 82 | Integer z = Z.I; 83 | } 84 | } -------------------------------------------------------------------------------- /.idea/inspectionProfiles/JavaTrivia.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 25 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16 | 17 | 19 | 20 | 21 | 22 | 23 | 24 | 40 | 41 | 43 | 44 | http://www.w3.org/1999/xhtml 45 | 46 | 47 | 48 | 49 | 50 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /.idea/projectCodeStyle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 139 | 141 | 142 | 143 | -------------------------------------------------------------------------------- /.idea/uiDesigner.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | --------------------------------------------------------------------------------