• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Possible errata in OCP 1Z0-815 Wiley Test Banks Bonus Exam 1 on Serialization

 
Ranch Hand
Posts: 229
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below is a question from the Sybex test bank on serialization.

import java.io.*;

public class AnimalCheckup {
  private String name;
  private int age;

  private static final ObjectStreamField[] ________________ =
     { new ObjectStreamField("name", String.class)};

  private void writeObject(ObjectOutputStream stream)
     throws Exception {
     
     ObjectOutputStream.PutField fields = stream.putFields();
     fields.put("name", name);
     stream.writeFields();
  }
  private void readObject(ObjectInputStream stream)
     throws Exception {
   
     ObjectInputStream.GetField fields = stream.readFields();
     name = (String) fields.get("name", null);
  }
}

A. Serializable
B. Serialized
C. serialFields
D. serialPersistentFields
E. transient
F. None of the above

This is the answer:

When implementing serialization, there are two main ways for omitting an instance variable. Option E is tricky because transient is one of those ways. However, it is incorrect because it must be used on the instance variable to be omitted. Option D is correct because it is the other way. The serialPersistentFields array lists the fields available to writeObject() and readObject().

However since the class does not implement Serializable, how would adding a serialPersistentFields make any difference? An exception would be thrown when an object of that class is being serialized.
 
reply
    Bookmark Topic Watch Topic
  • New Topic