Multiple choice technology

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

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Forest implements Serializable, but its field 'tree' is of type Tree, which does NOT implement Serializable. During serialization, Java attempts to serialize all non-transient object fields. When it tries to serialize the Tree object, it throws a NotSerializableException at runtime because Tree is not serializable.