1.
Given: 11. public static void test(String str) { 12. if (str == null | str.length() == 0) { 13. System.out.println("String is empty"); 14. } else { 15. System.out.println("String is not empty"); 16. } 17. } And the invocation: 31. test(null); What is the result?
2.
Given code in separate source files: 10. public class Foo { 11. public int a; 12. public Foo() { a = 3; } 13. public void addFive() { a += 5;} 14. } and: 20. public class Bar extends Foo { 21. public int a; 22. public Bar() { a = 8; } 23. public void addFive() { this.a += 5; } 24. } invoked with: 30. Foo foo = new Bar(); 31. foo.addFive(); 32. System.out.println("Value: " + foo.a); What is the result?
3.
Given classes defined in two different files: 1. package util; 2. public class BitUtils { 3. private static void process(byte[] b) {} 4. } 1. package app; 2. public class SomeApp { 3. public static void main(String[] args) { 4. byte[] bytes = new byte[256]; 5. // insert code here 6. } 7. } What is required at line 5 in class SomeApp to use the process method of BitUtils?
4.
Given: 11. String test = "a1b2c3"; 12. String[] tokens = test.split("\\d"); 13. for(String s: tokens) System.out.print(s + " "); What is the result?
5.
Given: 1. interface DoStuff2 { 2. float getRange(int low, int high); } 3. 4. interface DoMore { 5. float getAvg(int a, int b, int c); } 6. 7. abstract class DoAbstract implements DoStuff2, DoMore { } 8. 9. class DoStuff implements DoStuff2 { 10. public float getRange(int x, int y) { return 3.14f; } } 11. 12. interface DoAll extends DoMore { 13. float getAvg(int a, int b, int c, int d); } What is the result?
6.
Given a class Repetition: 1. package utils; 2. 3. public class Repetition { 4. public static String twice(String s) { return s + s; } 5. } and given another class Demo: 1. // insert code here 2. 3. public class Demo { 4. public static void main(String[] args) { 5. System.out.println(twice("pizza")); 6. } 7. } Which code should be inserted at line 1 of Demo.java to compile and run Demo to print "pizzapizza"?
7.
Given: 11. public class Test { 12. public static void main(String [] args) { 13. int x = 5; 14. boolean b1 = true; 15. boolean b2 = false; 16. 17. if ((x == 4) && !b2 ) 18. System.out.print("1 "); 19. System.out.print("2 "); 20. if ((b2 = true) && b1 ) 21. System.out.print("3 "); 22. } 23. } What is the result?
8.
Given: 11. public static void main(String[] args) { 12. try { 13. args = null; 14. args[0] = "test"; 15. System.out.println(args[0]); 16. } catch (Exception ex) { 17. System.out.println("Exception"); 18. } catch (NullPointerException npe) { 19. System.out.println("NullPointerException"); 20. } 21. } What is the result?
9.
Given: 1. interface TestA { String toString(); } 2. public class Test { 3. public static void main(String[] args) { 4. System.out.println(new TestA() { 5. public String toString() { return "test"; } 6. }); 7. } 8. } What is the result?
10.
Given: 10. public class ClassA { 11. public void count(int i) { 12. count(++i); 13. } 14. } And: 20. ClassA a = new ClassA(); 21.