Site Search:

OCPJP Simulation Test 14

<Back to OCPJP





Question: What will the following code output?


  
  1 import java.io.*;
  2 class Pet implements Serializable {String name = "betty";}
  3 public class PetStore implements Serializable {
  4   private String name = "peeco";
  5   private int year = 2007;
  6   private transient Pet pet = new Pet();
  7   public Pet getPet() { return pet;}
  8   public void setPet(Pet pet) {this.pet = pet;}
  9 
 10   public static void main(String[] args) throws IOException, FileNotFoundException, ClassNotFoundException  {
 11     try(ObjectOutputStream out = new ObjectOutputStream(
 12       new FileOutputStream("pet.dat"));
 13       ObjectInputStream in = new ObjectInputStream(
 14       new FileInputStream("pet.dat"))) {
 15       PetStore pet = new PetStore();
 16       out.writeObject(pet);
 17       PetStore pet22 = (PetStore)in.readObject();
 18       System.out.println(pet22.name+pet22.year+pet22.getPet().name);
 19     }}}


Choice:
A. peeco2007null
B. peeco2007betty
C. null0null
D. null0betty
E. 0
F. The code does not compile
G. The code compiles but throw Exception at runtime