Page: 3
11. java.io package and Serialization
Q: 09 Click the Task button. 
Solution: 1. String path=” “; 2. path=path+File.separator+dir; 3. File file=new File(path,filename); 4. return file.exists(); Q:10 Click the Exhibit button. Which code, inserted at line 14, will allow this class to correctly serialize and deserialize?  A. s.defaultReadObject(); B. this = s.defaultReadObject(); C. y = s.readInt(); x = s.readInt(); D. x = s.readInt(); y = s.readInt(); Answer: D Question: 11 Given: 10. public class Foo implements java.io.Serializable { 11. private int x; 12. public int getX() { return x; } 12.publicFoo(int x){this.x=x; } 13. private void writeObject( ObjectOutputStream s) 14. throws IOException { 15. // insert code here 16. } 17. } Which code fragment, inserted at line 15, will allow Foo objects to be correctly serialized and deserialized? A. s.writeInt(x); B. s.serialize(x); C. s.writeObject(x); D. s.defaultWriteObject(); Answer: D 12 Click the Task button.
 Solution: import java.io.*; public class ReadFile{ public static void main(String s[ ]){ try { File x1=new File("MyText.txt"); FileReader x2=new FileReader(x1); BufferedReader x4=new BufferedReader(x2); String s3=null; while((x3 = x4.readLine( )) != null ) { System.out.println(x3); }x4.close( ); }catch(Exception e){ e.printStackTrace(); } } }
Page: 3
1
2
3
4
|