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?
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?
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.)
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. }
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. }
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?
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. }
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. }
Bug Report