• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

static variables and serialization

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My source is Sun Certification Sample

I know serialization is not in the exam anymore but I want to understand whether the reported answer is right.


The reported answer is 0 7 0 but my understanding is that it should be 7 7 0 as the value of the static variable is going to remain unaffected by serialization/unserialization as it is not part of serialized object. Is it an error in the question or is there a way of interpreting the question so the given answer makes sense? Incidentally, I have included the serialization/deserialization code as the original just stated:

4) Given:
2. import java.io.*;
3. public class Network {
4. public static void main(String[] args) {
5. Traveler t = new Traveler();
6. t.x1 = 7; t.x2 = 7; t.x3 = 7;
7. // serialize t then deserialize t
8. System.out.println(t.x1 + " " + t.x2 + " " + t.x3);
9. }
10. }
11. class Traveler implements Serializable {
12. static int x1 = 0;
13. volatile int x2 = 0;
14. transient int x3 = 0;
15. }

If, on line 7, t is successfully serialized and then deserialized, what is the result?
 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, You are right output will be "7 7 0"
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I was pretty upset about the low quality of the sample questions and proposed "correct" answers - which aren't even free. The proposed answer and explanation from your example can only be created of by a total lack of understanding how a simple program runs in general, combined the laziness to not test it.

Maybe the serialization questions in the real exam were all wrong, too, and that's why they had to remove them?

Kai
 
Ranch Hand
Posts: 241
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not impressed either... well, at least it's been corrected here
 
reply
    Bookmark Topic Watch Topic
  • New Topic