Assessment Test for Experienced Programmer

Instructions: For each question, choose one single best answer. There is no time limit, so you can change your answers at any time. When you click Done button, the assessment report will appear in the next screen.


1.   Given:

 1. class Test {
 2.     static String mountain = "Everest";
 3.     static Test favorite(){
 4.         System.out.print("Mount ");
 5.         return null;
 6.     }
 7.     public static void main(String[] args) {
 8.         System.out.println(favorite().mountain);
 9.     }
10. }

What will happen when you attempt to compile and execute the code?

A.    NullPointerException is thrown because of code at line 5.
B.    NullPointerException is thrown because of code at line 8.
C.    It will not compile because of syntax error.
D.    It prints: Mount Everest.


2.   Given:

 1.  class Test {
 2.      public static void main(String[] args) {
 3.          int len = 0, oldlen = 0;
 4.          Object[] a = new Object[0];
 5.          try {
 6.              for (;;) {
 7.                  ++len;
 8.                  Object[] temp = new Object[oldlen = len];
 9.                  temp[0] = a;
10.                  a = temp;
11.              }
12.          } catch (Error e) {
13.              System.out.println(e + ", " + (oldlen==len));
14.          }
15.      }
16.  }

What will happen when you attempt to compile and run the code?

A.    It prints: java.lang.OutOfMemoryError, true.
B.    It prints: java.lang.OutOfMemoryError, false.
C.    It will not compile because of code at line 8.
D.    It will not compile because of code at line 10.
E.    It will not compile because of code at line 12.


3.   Here is a method that creates a number of String objects in the course of printing a countdown sequence.

1. public void countDown() {
2.    for (int i = 10; i >= 0; i --) {
3.        String tmp = Integer.toString(i);
4.        System.out.println(tmp);
5.    }
6.    System.out.println("Boom!");
7. }

When the program reaches line 6, how many of the String objects created in line 3.
will have been garbage collected?(Assume that the System.out object is not keeping a reference.)

A.    none
B.    There is no way to tell
C.    9
D.    10
E.    11


4.   Given the code below. What will be the result of attempting to run the following program?

 1. class Test {
 2.    public static void main(String[] args) {
 3.       String [][][] arr = {
 4.           { {}, null },
 5.           { {"1","2"}, {"1", null, "3" }},
 6.           { {}, {"1",null} }
 7.           };
 8.       System.out.println(arr[1][2].length);
 9.    }
10. }
A.    Compiler objects to line 5
B.    Compiler objects to line 8
C.    It compiles but throws an ArrayIndexOutOfBoundsException
D.    It compiles but throws a NullPointerException
E.    It prints 2

5.   What is the output of the following code when compiled and run?

1. public class Test {
2.    public static void main(String[] args) { 
3.        boolean b = false;
4.        String s = (b=!b)?(b=!b)?"Hello":"hello":(b=!b)?"world":"World";
5.        System.out.println(s);
6.    } 
7. }
A.    Prints: Hello
B.    Prints: hello
C.    Prints: Helloworld
D.    Prints: helloWorld
E.    Compilation error.

6.   Given:

 1.  interface I { 
 2.     int x = 0; 
 3.  }
 4.  class T1 implements I { 
 5.     int x = 1; 
 6.  }
 7.  class T2 extends T1 { 
 8.     int x = 2; 
 9.  }
10.  class T3 extends T2 {
11.     int x = 3;
12.     void test() {
13.         System.out.print(x);
14.         System.out.print(super.x);
15.         System.out.print(((T2)this).x);
16.         System.out.print(((T1)this).x);
17.         System.out.println(((I)this).x);
18.     }
19.  }
20.  class Test {
21.     public static void main(String[] args) {
22.         new T3().test();
23.     }
24.  }

What is the printout?
A.    31210
B.    32210
C.    32110
D.    32101
E.    32010

7.   What is the output of the following code when compiled and run?

 1. public class Test {
 2.    public static void main(String[] args) throws Exception{
 3.        Thread t1 = new Thread(getRunnable(3));
 4.        Thread t2 = new Thread(getRunnable(4));
 5.        t1.join();
 6.        System.out.println("End");
 7.    }
 8.
 9.    public static Runnable getRunnable(final int id){
10.        return new Runnable(){
11.            public void run(){
12.                for(int i = 0; i < id; i++){
13.                    System.out.print(" "+i);
14.                }
15.            }
16.        };
17.    }
18. }
A.    The output depends an underlying platform.
B.    Prints: End.
C.    Prints: 0 1 2 0 1 2 3 End.
D.    Compilation error at line 9.
E.    Compilation error at line 10.

8.   Given the code below, what is the result when you attempt to execute it?

1. import java.util.*;
2. class Key {
3.     int key;
4.     Key(int n) {
5.         this.key = n;
6.     }
7.     public int hashCode() {
8.         return this.key*181;
9.     }
10.    public boolean equals(Object other) {
11.         return this.key == ((Key)other).key;
12.     }
13. 
14.
15.  }
16.  class LuckyWord {
17.    boolean b = Math.random() > 0.5;
18.    public String toString() {
19.        if (b)
20.           return "You will pass!";
21.        else
22.           return "You will fail!";
23.    }
24. }
25. 
26. class Test {
27.     public static void main(String[] args) {
28.        Map hm = new HashMap();
29.        for (int i = 0; i < 5; i++)
30.            hm.put(new Key(i), new LuckyWord());
31.        System.out.println("Lookup key 3 ");
32.        Key k = new Key(3);
33.        if (hm.containsKey(k))
34.            System.out.println((LuckyWord)hm.get(k));
35.        else
36.            System.out.println("Key not found: " + k);
37.    }
38. }
A.    It compiles and the key looked for is not found because it has a different memory address.
B.    It compiles and the key looked for has been found.
C.    It compiles and the key looked for is not found.
D.    It fails to compile because the HashMap can only store homogeneous objects.
E.    It fails to compile because the identifier name conflicts.

Bug Report