Java Programming Quiz

Test your knowledge of Java programming concepts including serialization, exception handling, threading, collections, and core language features.

20 Questions Published

Questions

Question 1 Multiple Choice (Multiple Answers)

Given: public class NamedCounter { private final String name; private int count; public NamedCounter(String name) { this.name = name; } public String getName() { return name; } public void increment() { count++; } public int getCount() { return count; } public void reset() { count = 0; } } Which three changes should be made to adapt this class to be used safely by multiple threads? (Choose three.)

  1. declare reset() using the synchronized keyword
  2. declare getName() using the synchronized keyword
  3. declare getCount() using the synchronized keyword
  4. declare the constructor using the synchronized keyword
  5. declare increment() using the synchronized keyword
Question 2 Multiple Choice (Single Answer)

Given: 7. void waitForSignal() { 8. Object obj = new Object(); 9. synchronized (Thread.currentThread()) { 10. obj.wait(); 11. obj.notify(); 12. } 13. } Which statement is true?

  1. This code may throw an InterruptedException
  2. This code may throw an IllegalStateException
  3. This code may throw a TimeoutException after ten minutes
  4. This code will not compile unless "obj.wait()" is replaced with "((Thread) obj).wait()".
  5. Reversing the order of obj.wait() and obj.notify() may cause this method to complete normally
  6. A call to notify() or notifyAll() from another thread may cause this method to complete normally
Question 3 Multiple Choice (Single Answer)

Given: 1. public class TestOne implements Runnable { 2. public static void main (String[] args) throws Exception { 3. Thread t = new Thread(new TestOne()); 4. t.start(); 5. System.out.print("Started"); 6. t.join(); 7. System.out.print("Complete"); 8. } 9. public void run() { 10. for (int i = 0; i < 4; i++) { 11. System.out.print(i); 12. } 13. } 14. } What can be a result?

  1. Compilation fails.
  2. An exception is thrown at runtime.
  3. The code executes and prints "StartedComplete"
  4. The code executes and prints "StartedComplete0123".
  5. The code executes and prints "Started0123Complete".
Question 4 Multiple Choice (Single Answer)

Given: 11. public class Test { 12. public enum Dogs {collie, harrier, shepherd}; 13. public static void main(String [] args) { 14. Dogs myDog = Dogs.shepherd; 15. switch (myDog) { 16. case collie: 17. System.out.print("collie "); 18. case default: 19. System.out.print("retriever "); 20. case harrier: 21. System.out.print("harrier "); 22. } 23. } 24. } What is the result?

  1. harrier
  2. shepherd
  3. retriever
  4. Compilation fails
  5. retriever harrier
  6. An exception is thrown at runtime
Question 5 Multiple Choice (Multiple Answers)

Given: 8. public class test { 9. public static void main(String [] a) { 10. assert a.length == 1; 11. } 12. } Which two will produce an AssertionError? (Choose two.)

  1. java test
  2. java -ea test
  3. java test file1
  4. java -ea test file1
  5. java -ea test file1 file2
  6. java -ea:test test file1
Question 6 Multiple Choice (Single Answer)

Given: 10. interface Foo {} 11. class Alpha implements Foo {} 12. class Beta extends Alpha {} 13. class Delta extends Beta { 14. public static void main( String[] args ) { 15. Beta x = new Beta(); 16. // insert code here 17. } 18. } Which code, inserted at line 16, will cause a java.lang.ClassCastException?

  1. Alpha a = x;
  2. Foo f = (Delta)x;
  3. Foo f = (Alpha)x;
  4. Beta b = (Beta)(Alpha)x;
Question 7 Multiple Choice (Single Answer)

Given: 11. public static Collection get() { 12. Collection sorted = new LinkedList(); 13. sorted.add("B"); sorted.add("C"); sorted.add("A"); 14. return sorted; 15. } 16. public static void main(String[] args) { 17. for (Object obj: get()) { 18. System.out.print(obj + ", "); 19. } 20. } What is the result?

  1. A, B, C,
  2. B, C, A,
  3. Compilation fails.
  4. The code runs with no output
  5. An exception is thrown at runtime.
Question 8 Multiple Choice (Single Answer)

Given: 84. try { 85. ResourceConnection con = resourceFactory.getConnection(); 86. Results r = con.query("GET INFO FROM CUSTOMER"); 87. info = r.getData(); 88. con.close(); 89. } catch (ResourceException re) { 90. errorLog.write(re.getMessage()); 91. } 92. return info; Which statement is true if a ResourceException is thrown on line 86?

  1. Line 92 will not execute.
  2. The connection will not be retrieved in line 85.
  3. The resource connection will not be closed on line 88.
  4. The enclosing method will throw an exception to its caller
Question 9 Multiple Choice (Multiple Answers)

Given: 31. // some code here 32. try { 33. // some code here 34. } catch (SomeException se) { 35. // some code here 36. } finally { 37. // some code here 38. } Under which three circumstances will the code on line 37 be executed? (Choose three.)

  1. The instance gets garbage collected.
  2. The code on line 33 throws an exception
  3. The code on line 35 throws an exception.
  4. The code on line 31 throws an exception
  5. The code on line 33 executes successfully.
Question 10 Multiple Choice (Single Answer)

Given: 11. class A { 12. public void process() { System.out.print("A,"); } 13. class B extends A { 14. public void process() throws IOException { 15. super.process(); 16. System.out.print("B,"); 17. throw new IOException(); 18. } 19. public static void main(String[] args) { 20. try { new B().process(); } 21. catch (IOException e) { System.out.println("Exception"); }} What is the result?

  1. Exception
  2. A,B,Exception
  3. Compilation fails because of an error in line 20.
  4. Compilation fails because of an error in line 14.
  5. A NullPointerException is thrown at runtime
Question 11 Multiple Choice (Single Answer)

Given a method that must ensure that its parameter is not null: 11. public void someMethod(Object value) { 12. // check for null value ... 20. System.out.println(value.getClass()); 21. } What, inserted at line 12, is the appropriate way to handle a null value?

  1. assert value == null;
  2. assert value != null, "value is null";
  3. if (value == null) {throw new AssertionException("value is null");}
  4. if (value == null) {throw new IllegalArgumentException("value is null");}
Question 12 Multiple Choice (Single Answer)

Given: 11. static void test() throws Error { 12. if (true) throw new AssertionError(); 13. System.out.print("test "); 14. } 15. public static void main(String[] args) { 16. try { test(); } 17. catch (Exception ex) { System.out.print("exception "); } 18. System.out.print("end "); 19. } What is the result?

  1. end
  2. compilation fails
  3. exception end
  4. exception test end
  5. A Throwable is thrown by main.
  6. An Exception is thrown by main.
Question 13 Multiple Choice (Single Answer)

Given: 11. Float pi = new Float(3.14f); 12. if (pi > 3) { 13. System.out.print("pi is bigger than 3. "); 14. } 15. else { 16. System.out.print("pi is not bigger than 3. "); 17. } 18. finally { 19. System.out.println("Have a nice day."); 20. } What is the result?

  1. compilation fails
  2. pi is bigger than 3
  3. An Exception occurs at runtime
  4. pi is bigger than 3. Have a nice day
  5. pi is not bigger than 3. Have a nice day
Question 14 Multiple Choice (Single Answer)

Given: 11. String test = "This is a test"; 12. String[] tokens = test.split("\s"); 13. System.out.println(tokens.length); What is the result?

  1. 0
  2. 1
  3. 4
  4. Compilation fails
  5. An Exception thrown at runtime
Question 15 Multiple Choice (Single Answer)

Given: 11. public class Yikes { 12. 13. public static void go(Long n) {System.out.println("Long ");} 14. public static void go(Short n) {System.out.println("Short ");} 15. public static void go(int n) {System.out.println("int ");} 16. public static void main(String [] args) { 17. short y = 6; 18. long z = 7; 19. go(y); 20. go(z); 21. } 22. } What is the result?

  1. int Long
  2. Short Long
  3. Compilation fails
  4. An Exception thrown at runtime
Question 16 Multiple Choice (Single Answer)

Given: 12. System.out.format("Pi is approximately %d.", Math.PI); What is the result?

  1. Compilation fails.
  2. Pi is approximately 3.
  3. Pi is approximately 3.141593.
  4. An exception is thrown at runtime.
Question 17 Multiple Choice (Multiple Answers)

Given: 12. NumberFormat nf = NumberFormat.getInstance(); 13. nf.setMaximumFractionDigits(4); 14. nf.setMinimumFractionDigits(2); 15. String a = nf.format(3.1415926); 16. String b = nf.format(2); Which two statements are true about the result if the default locale is Locale.US? (Choose two.)

  1. The value of b is 2.
  2. The value of a is 3.14.
  3. The value of b is 2.00.
  4. The value of a is 3.141
  5. The value of a is 3.1416
  6. The value of b is 2.0000
Question 18 Multiple Choice (Single Answer)

Given: 12. import java.io.*; 13. public class Forest implements Serializable { 14. private Tree tree = new Tree(); 15. public static void main(String [] args) { 16. Forest f = new Forest(); 17. try { 18. FileOutputStream fs = new FileOutputStream("Forest.ser"); 19. ObjectOutputStream os = new ObjectOutputStream(fs); 20. os.writeObject(f); os.close(); 21. } catch (Exception ex) { ex.printStackTrace(); } 22. } } 23. 24. class Tree { } What is the result?

  1. Compilation fails
  2. An exception is thrown at runtime.
  3. An instance of Forest is serialized.
  4. An instance of Forest and an instance of Tree are both serialized
Question 19 Multiple Choice (Single Answer)

Assuming that the serializeBanana() and the deserializeBanana() methods will correctly use Java serialization and given: 13. import java.io.*; 14. class Food implements Serializable {int good = 3;} 15. class Fruit extends Food {int juice = 5;} 16. public class Banana extends Fruit { 17. int yellow = 4; 18. public static void main(String [] args) { 19. Banana b = new Banana(); Banana b2 = new Banana(); 20. b.serializeBanana(b); // assume correct serialization 21. b2 = b.deserializeBanana(); // assume correct 22. System.out.println("restore "+b2.yellow+ b2.juice+b2.good); 24. } 25. // more Banana methods go here 50. } What is the result?

  1. restore 400
  2. restore 403
  3. restore 453
  4. Compilation fails.
  5. An exception is thrown at runtime
Question 20 Multiple Choice (Single Answer)

Given this method in a class: 21. public String toString() { 22. StringBuffer buffer = new StringBuffer(); 23. buffer.append('<'); 24. buffer.append(this.name); 25. buffer.append('>'); 26. return buffer.toString(); 27. } Which statement is true?

  1. This code is NOT thread-safe
  2. The programmer can replace StringBuffer with StringBuilder with no other changes
  3. This code will perform poorly. For better performance, the code should be rewritten:return "<" + this.name + ">";
  4. This code will perform well and converting the code to use StringBuilder will not enhance the performance