OCPJP6 exam question – serialization

I was reading the SCJP6 book from Kathy Sierra and Bert Bates (one of the most prestigious books if you are pursuing the Oracle Certification for Java SE 6 Programmer) and I found a question that will blow your mind. Well, at least my reaction was something like “wtf! there must be an errata somewhere…”.

Which are true? (Choose all that apply.)

  • A. Compilation fails
  • B. The output is 10 0 9
  • C. The output is 10 0 10
  • D. The output is 10 7 9
  • E. The output is 10 7 10
  • F. In order to alter the standard deserialization process you would implement the readObject() method in SpecialSerial
  • G. In order to alter the standard deserialization process you would implement the defaultReadObject() method in SpecialSerial

My answer was B but the correct answer is “C and F”. Ok, I missed the “F” one and the entire question but why “C” ?!?!?!

What the heck the guys behind exams and certifications think when they are creating evil questions like this one.  Probably you know that static variables aren’t serialized, right? So, why SpecialSerial.z has 10 instead of 9 ? Because “z” is static and any SpecialSerial instance created or unserialized during the execution of this Java application will always have z = 10. 😉