Java Programming Quiz
Test your knowledge of Java programming concepts including serialization, exception handling, threading, collections, and core language features.
Questions
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.)
- declare reset() using the synchronized keyword
- declare getName() using the synchronized keyword
- declare getCount() using the synchronized keyword
- declare the constructor using the synchronized keyword
- declare increment() using the synchronized keyword
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?
- This code may throw an InterruptedException
- This code may throw an IllegalStateException
- This code may throw a TimeoutException after ten minutes
- This code will not compile unless "obj.wait()" is replaced with "((Thread) obj).wait()".
- Reversing the order of obj.wait() and obj.notify() may cause this method to complete normally
- A call to notify() or notifyAll() from another thread may cause this method to complete normally
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?
- Compilation fails.
- An exception is thrown at runtime.
- The code executes and prints "StartedComplete"
- The code executes and prints "StartedComplete0123".
- The code executes and prints "Started0123Complete".
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?
- harrier
- shepherd
- retriever
- Compilation fails
- retriever harrier
- An exception is thrown at runtime
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.)
- java test
- java -ea test
- java test file1
- java -ea test file1
- java -ea test file1 file2
- java -ea:test test file1
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?
- Alpha a = x;
- Foo f = (Delta)x;
- Foo f = (Alpha)x;
- Beta b = (Beta)(Alpha)x;
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?
- A, B, C,
- B, C, A,
- Compilation fails.
- The code runs with no output
- An exception is thrown at runtime.
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?
- Line 92 will not execute.
- The connection will not be retrieved in line 85.
- The resource connection will not be closed on line 88.
- The enclosing method will throw an exception to its caller
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.)
- The instance gets garbage collected.
- The code on line 33 throws an exception
- The code on line 35 throws an exception.
- The code on line 31 throws an exception
- The code on line 33 executes successfully.
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?
- Exception
- A,B,Exception
- Compilation fails because of an error in line 20.
- Compilation fails because of an error in line 14.
- A NullPointerException is thrown at runtime
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?
- assert value == null;
- assert value != null, "value is null";
- if (value == null) {throw new AssertionException("value is null");}
- if (value == null) {throw new IllegalArgumentException("value is null");}
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?
- end
- compilation fails
- exception end
- exception test end
- A Throwable is thrown by main.
- An Exception is thrown by main.
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?
- compilation fails
- pi is bigger than 3
- An Exception occurs at runtime
- pi is bigger than 3. Have a nice day
- pi is not bigger than 3. Have a nice day
Given: 11. String test = "This is a test"; 12. String[] tokens = test.split("\s"); 13. System.out.println(tokens.length); What is the result?
- 0
- 1
- 4
- Compilation fails
- An Exception thrown at runtime
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?
- int Long
- Short Long
- Compilation fails
- An Exception thrown at runtime
Given: 12. System.out.format("Pi is approximately %d.", Math.PI); What is the result?
- Compilation fails.
- Pi is approximately 3.
- Pi is approximately 3.141593.
- An exception is thrown at runtime.
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.)
- The value of b is 2.
- The value of a is 3.14.
- The value of b is 2.00.
- The value of a is 3.141
- The value of a is 3.1416
- The value of b is 2.0000
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?
- Compilation fails
- An exception is thrown at runtime.
- An instance of Forest is serialized.
- An instance of Forest and an instance of Tree are both serialized
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?
- restore 400
- restore 403
- restore 453
- Compilation fails.
- An exception is thrown at runtime
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?
- This code is NOT thread-safe
- The programmer can replace StringBuffer with StringBuilder with no other changes
- This code will perform poorly. For better performance, the code should be rewritten:return "<" + this.name + ">";
- This code will perform well and converting the code to use StringBuilder will not enhance the performance