Site Search:

OCPJP Simulation Test 15

<Back to OCPJP





Question: What is the output of the following program?

  1 import java.io.*;
  2 import java.util.*;
  3 class Item implements Serializable {
  4    int id;
  5    public Item(int id) {this.id = id;}}
  6 public class ItemStorage implements Serializable {
  7   private transient String name = "toolstore";
  8   private List items = new ArrayList<>();
  9   {items.add(new Item(1));items.add(new Item(2));}
 10 
 11   public static void main(String[] args) throws IOException, FileNotFoundException, ClassNotFoundException  {
 12     try(ObjectOutputStream out = new ObjectOutputStream(
 13       new FileOutputStream("pet.dat"));
 14       ObjectInputStream in = new ObjectInputStream(
 15       new FileInputStream("pet.dat"))) {
 16       ItemStorage storage = new ItemStorage();
 17       out.writeObject(storage);
 18       storage = (ItemStorage)in.readObject();
 19       System.out.println(storage.name+storage.items.size());
 20     }}}


Choice:
A. toolstore4
B. toolstore2
C. toolstore0
D. null0
E. 4
F. null2
G. The code does not compile
H. The throws Exception at runtime