Java Questions and Answers
Trivia
Copyright © Tony Morris 2005, 2006
Category: Trivia
-x equals x
Under what
two circumstances (unique values for x) will the following print true?
01 public final class X
02 {
03 private X()
04 {
05
06 }
07
08 public static void printNegXEqualsX(int x)
09 {
10 System.out.println(-x == x);
11 }
12 }
|
Category: Trivia
Absolutely
Make the following code print true.
01 public final class Absolutely
02 {
03 private Absolutely() throws UnsupportedOperationException
04 {
05 throw new UnsupportedOperationException();
06 }
07
08 public static void main(final String[] args)
09 {
10 absolutely(Integer.parseInt(args[0]));
11 }
12
13 static void absolutely(int n)
14 {
15 System.out.println(Math.abs(n) == n && n < 0);
16 }
17 }
|
Category: Trivia
Assignment throws NullPointerException
Make the following line of code throw a java.lang.NullPointerException.
Category: Trivia
Byte Me
01 public final class Bytes
02 {
03 private Bytes()
04 {
05
06 }
07
08 public static void main(String[] args)
09 {
10 for(byte b = 0; b < 255; b++)
11 {
12 System.out.println(b);
13 }
14 }
15 }
|
What is the output and why?
a) no output
b) prints 0 to 254
c) compile-time error ________
d) runtime exception ________
e) something else ________
Category: Trivia
Calling an overridable method from a constructor
01 abstract class Eggs
02 {
03 abstract void m();
04
05 Eggs()
06 {
07 m();
08 }
09 }
10
11 class Toast extends Eggs
12 {
13 private int x = 2;
14
15 Toast()
16 {
17 x = 3;
18 }
19
20 public void m()
21 {
22 System.out.println("x = " + x);
23 }
24
25 public static void main(String[] args)
26 {
27 new Toast();
28 }
29 }
|
What is the output and why?
a) x = 3
b) x = 2
c) compile-time error ________
d) runtime exception ________
e) something else ________
Category: Trivia
Constants, chars, ints, Strings
01 public class ConstantsCharsStrings
02 {
03 private ConstantsCharsStrings()
04 {
05
06 }
07
08 public static void main(String[] args)
09 {
10 char c1 = 'x';
11 final char c2 = 'y';
12 int i1 = 7;
13 final int i2 = 8;
14 final int i3 = i1 >>> 3;
15 final int i4 = i2 >>> 4;
16
17 System.out.println("xxx" == (c1 + "xx"));
18 System.out.println("yxx" == (c2 + "xx"));
19 System.out.println("7xx" == (i1 + "xx"));
20 System.out.println("8xx" == (i2 + "xx"));
21 System.out.println("7xx" == (i3 + "xx"));
22 System.out.println("8xx" == (i4 + "xx"));
23 }
24 }
|
What is the output and why?
a) false false false false false false
b) true true true true true true
c) false true false true false true
d) false true false false true false
e) false true false true false false
f) compile-time error ________
g) runtime exception ________
h) something else ________
Category: Trivia
Go buy an ice-cream
01 public final class Hello
02 {
03 private Hello()
04 {
05
06 }
07
08 // say Hello \u000A then go buy an ice-cream
09 public static void main(String[] args)
10 {
11 System.out.println("hello");
12 }
13 }
|
What is the output and why?
a) hello
b) compile-time error ________
c) runtime exception ________
d) something else ________
Category: Trivia
hello Test
01 /**
02 * Test class.
03 * Source from Z:\Java\tests\undies\Test.java
04 */
05 public final class Test
06 {
07 private Test()
08 {
09
10 }
11
12 public static void main(String[] args)
13 {
14 System.out.println("hello");
15 }
16 }
|
What is the output and why?
a) hello
b) compile-time error ________
c) runtime exception ________
d) something else ________
Category: Trivia
Java.com
01 public final class Java
02 {
03 private Java() throws UnsupportedOperationException
04 {
05 throw new UnsupportedOperationException();
06 }
07
08 public static void main(final String[] args)
09 {
10 System.out.println("Hello");
11 http://www.java.com/
12 System.out.println("Goodbye");
13 }
14 }
|
What is the output and why?
a) no output
b) prints Hello http://www.java.com/ Goodbye
c) prints Hello Goodbye
d) compile-time error ________
e) runtime exception ________
f) something else ________
Category: Trivia
Maps and Integers
01 import java.util.Map;
02 import java.util.IdentityHashMap;
03
04 public final class X
05 {
06 private X()
07 {
08
09 }
10
11 public static void main(String... args)
12 {
13 Map<Integer, Integer> m = new IdentityHashMap<Integer, Integer>();
14 m.put(7, 8);
15 m.put(777, 888);
16 System.out.println(m.containsKey(7));
17 System.out.println(m.containsKey(new Integer(7)));
18 System.out.println(m.containsKey(777));
19 System.out.println(m.containsKey(new Integer(777)));
20 }
21 }
|
What is the output and why?
a) true true true true
b) true false true false
c) true true true false
d) true false true true
e) false true true true
f) true false false false
g) compile-time error ________
h) runtime exception ________
i) something else ________
Category: Trivia
Mutable String
Is it possible to change the value of a java.lang.String instance?
Category: Trivia
Nullify
01 public final class Nullify
02 {
03 private static String s;
04
05 private Nullify()
06 {
07
08 }
09
10 public static void main(String[] args)
11 {
12 Nullify n = null;
13 n.s = "null!";
14 System.out.println(n.s);
15 }
16 }
|
What is the output and why?
a) A java.lang.NullPointerException is thrown
b) null!
c) no output
d) compile-time error ________
e) Some other runtime exception besides a NullPointerException ________
f) something else ________
Category: Trivia
private Constructors
True or false.
A class with constructors that are all private cannot be subclassed.
Category: Trivia
private methods cannot be overridden
01 public class T
02 {
03 private final String s;
04
05 private void printS()
06 {
07 System.out.println(s);
08 }
09
10 T(String s)
11 {
12 this.s = s;
13 }
14
15 public static void main(String[] args)
16 {
17 new T("main").m();
18 }
19
20 private void m()
21 {
22 new T("m")
23 {
24 void method()
25 {
26 printS();
27 }
28 }.method();
29 }
30 }
|
What is the output and why?
a) main
b) m
c) no output
d) compile-time error ________
e) runtime exception ________
f) something else ________
Change the access modifer on the printS() method from 'private' to something else. What is the output?
a) main
b) m
c) no output
d) compile-time error ________
e) runtime exception ________
f) something else ________
Category: Trivia
Return seven
01 public final class Seven
02 {
03 private Seven()
04 {
05
06 }
07
08 int s()
09 {
10 return 7;;
11 }
12
13 public static void main(String[] args)
14 {
15 System.out.println(new Seven().s());
16 }
17 }
|
What is the output and why?
a) 7
b) Seven
c) compile-time error ________
d) runtime exception ________
e) something else ________
Category: Trivia
Strings and reference equality comparison
01 public final class StringReference
02 {
03 private StringReference()
04 {
05
06 }
07
08 public static void main(String[] args)
09 {
10 String s1 = "hello";
11 String s2 = "hello";
12
13 System.out.println("s1 == s2: " + s1 == s2);
14 }
15 }
|
What is the output and why?
a) s1 == s2: true
b) s1 == s2: false
c) compile-time error ________
d) runtime exception ________
e) something else ________
Category: Trivia
Stringurbation
01 public final class Stringurbation
02 {
03 private Stringurbation()
04 {
05
06 }
07
08 public static void main(String[] args)
09 {
10 System.out.println("Strings".trim() == "Strings".trim());
11 System.out.println("String ".trim() == "String ".trim());
12 System.out.println("String".replace('t','T') == "String".replace('t','T'));
13 System.out.println("String".replace('t','t') == "String".replace('t','t'));
14 }
15 }
|
What is the output?
a) true true true true
b) true false true false
c) true true false false
d) true false false true
e) true true false true
f) compile-time error ________
g) runtime exception ________
h) something else ________
Category: Trivia
The variable final that is not final
Which core API exposes field(s) that are declared final while at the same time, method(s) that permit altering the field value(s)?
Category: Trivia
This Person equals that Person
01 import java.util.Set;
02 import java.util.HashSet;
03
04 public final class Person
05 {
06 private String name;
07
08 public Person(String name)
09 {
10 this.name = name;
11 }
12
13 public boolean equals(Object o)
14 {
15 if(o == null || o.getClass() != Person.class)
16 {
17 return false;
18 }
19
20 Person p = (Person)o;
21
22 return name.equals(p.name);
23 }
24
25 public static void main(String[] args)
26 {
27 Set s = new HashSet();
28 s.add(new Person("Bob"));
29 System.out.println(s.contains(new Person("Bob")));
30 }
31 }
|
What is the output and why?
a) true
b) false
c) compile-time error ________
d) runtime exception ________
e) something else ________
Category: Trivia
Unequal to itself
Using the following piece of code unmodified, make it print "not equal".
1 System.out.println(x == x ? "equal" : "not equal");
|
Category: Trivia
When a final is not a constant
01 class S
02 {
03 static final int I = 7;
04
05 static
06 {
07 System.out.println("S");
08 }
09 }
10
11 class T
12 {
13 static final Object S = null;
14
15 static
16 {
17 System.out.println("T");
18 }
19 }
20
21 class U
22 {
23 static final Object S = "s";
24
25 static
26 {
27 System.out.println("U");
28 }
29 }
30
31 class V
32 {
33 static final String S = "s";
34
35 static
36 {
37 System.out.println("V");
38 }
39 }
40
41 class W
42 {
43 static final String S = new String("s");
44
45 static
46 {
47 System.out.println("W");
48 }
49 }
50
51 class X
52 {
53 static final int I = 7;
54
|