• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Doubt in Serialization

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers
This is the question from SCJP(310-055) exam guide by kathy Siera and Bert Bates, page no:501 Question no:6

import java.io.*;
Class Keyboard
{
}
public class Computer implements Serializable
{
private Keyboard k=new Keyboard();
public static void main(String[]args)
{
Computer c=new Computer();
c.storeIt(c);
}
void storeIt(Computer c)
{
try{
ObjectOutputStream os=new ObjectOutputStream(new FileOutputStrea("myfile"));
os.writeObject(c);
os.close();
System.out.println("done");
}
catch(Exception e)
{
System.out.println("exe");
}
}
}
Answers(choose all the apply)
a)exe
b)done
c)Compilation fails
d)Exactly one object is serialized
e)exactly two objects are serialized

answer is given as A, my doubt why cant answer be A,B and D, because here only one object is Serialized that is c.
[ February 09, 2008: Message edited by: gobburi saikrishna ]
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags and QuoteYourSources.
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason the answer is A is that the Computer class has a private instance variable of type Keyboard.

When you attempt to serialize the Computer class it fails due to the Keyboard class not implementing Serializable. To overcome this problem either implement Serializable on the Keyboard class or add the transient modifier to the reference variable.
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

my doubt why cant answer be A,B and D, because here only one object is Serialized that is c.



Here, as an exception occurs at runtime while trying to serialize a Keyboard, the exception is being caught in catch block - so ya, A is fine.

But after the exception is caught the execution starts from the statement that follows the catch block


So b and d gotto be wrong..

Regards,
Vishwa
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as class Keyboard is not implementing Serializable or the object reference variable (instance of class Computer) is not transient
so while serialization it will throw Runtime Exception -
java.io.NotSerializableException: Keyboard
so in catch block it will print "exe" instead of "done" of try block
so A is right answer
 
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic