Page: 4
11. java.io package and Serialization
13. Given:
import java.io.*;
class Player {
Player() { System.out.print("p"); }
}
class CardPlayer extends Player implements Serializable {
CardPlayer() { System.out.print("c"); }
public static void main(String[] args) {
CardPlayer c1 = new CardPlayer();
try {
FileOutputStream fos = new FileOutputStream("play.txt");
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(c1);
os.close();
FileInputStream fis = new FileInputStream("play.txt");
ObjectInputStream is = new ObjectInputStream(fis);
CardPlayer c2 = (CardPlayer) is.readObject();
is.close();
} catch (Exception x ) { }
}
}
What is the result?
A. pc B. pcc
C. pcp D. pcpc
E. Compilation fails. F. An exception is thrown at runtime.
Answer:
-> C is correct. It's okay for a class to implement Serializable even if its superclass doesn't. However, when you deserialize such an object, the non-serializable superclass must run its constructor. Remember, constructors don't run on deserialized classes that implement Serializable.
-> A, B, D, E, and F are incorrect based on the above.
14. Given:
bw is a reference to a valid BufferedWriter And the snippet:
15. BufferedWriter b1 = new BufferedWriter(new File("f"));
16. BufferedWriter b2 = new BufferedWriter(new FileWriter("f1"));
17. BufferedWriter b3 = new BufferedWriter(new PrintWriter("f2"));
18. BufferedWriter b4 = new BufferedWriter(new BufferedWriter(bw));
What is the result?
A. Compilation succeeds.
B. Compilation fails due only to an error on line 15.
C. Compilation fails due only to an error on line 16.
D. Compilation fails due only to an error on line 17.
E. Compilation fails due only to an error on line 18.
F. Compilation fails due to errors on multiple lines.
Answer:
-> B is correct. BufferedWriters can be constructed only by wrapping a Writer. Lines 16, 17, and 18 are correct because BufferedWriter, FileWriter, and PrintWriter all extend Writer. (Note: BufferedWriter is a decorator class. Decorator classes are used extensively in the java.io package to allow you to extend the functionality of other classes.)
-> A, C, D, E, and F are incorrect based on the above. (Objective 3.2)
15. Given:
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 FileOutputStream("myFile"));
os.writeObject(c);
os.close();
System.out.println("done");
} catch (Exception x) {System.out.println("exc"); }
}
}
What is the result? (Choose all that apply.)
A. exc
B. done
C. Compilation fails.
D. Exactly one object is serialized.
E. Exactly two objects are serialized.
Answer:
-> A is correct. An instance of type Computer Has-a Keyboard. Because Keyboard doesn't implement Serializable, any attempt to serialize an instance of Computer will cause an exception to be thrown.
-> B, C, D, and E are incorrect based on the above. If Keyboard did implement Serializable then two objects would have been serialized. (Objective 3.3)
16. Given:
import java.io.*;
class Directories {
static String [] dirs = {"dir1", "dir2"};
public static void main(String [] args) {
for (String d : dirs) {
// insert code 1 here
File file = new File(path, args[0]);
// insert code 2 here
}
}
}
and that the invocation
java Directories file2.txt
is issued from a directory that has two subdirectories, "dir1" and "dir1", and that "dir1" has a
file "file1.txt" and "dir2" has a file "file2.txt", and the output is "false true", which
set(s) of code fragments must be inserted? (Choose all that apply.)
A. String path = d;
System.out.print(file.exists() + " ");
B. String path = d;
System.out.print(file.isFile() + " ");
C. String path = File.separator + d;
System.out.print(file.exists() + " ");
D. String path = File.separator + d;
System.out.print(file.isFile() + " ");
Answer:
-> A and B are correct. Because you are invoking the program from the directory whose direct subdirectories are to be searched, you don't start your path with a File.separator character. The exists() method tests for either files or directories; the isFile() method tests only for files. Since we're looking for a file, both methods work.
-> C and D are incorrect based on the above (Objective 3.2)
17. Given:
import java.io.*;
public class TestSer {
public static void main(String[] args) {
SpecialSerial s = new SpecialSerial();
try {
ObjectOutputStream os = new ObjectOutputStream(
new FileOutputStream("myFile"));
os.writeObject(s); os.close();
System.out.print(++s.z + " ");
ObjectInputStream is = new ObjectInputStream(
new FileInputStream("myFile"));
SpecialSerial s2 = (SpecialSerial)is.readObject();
is.close();
System.out.println(s2.y + " " + s2.z);
} catch (Exception x) {System.out.println("exc"); }
}
}
class SpecialSerial implements Serializable {
transient int y = 7;
static int z = 9;
}
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 override the readObject() method in SpecialSerial.
G. In order to alter the standard deserialization process you would override the
defaultReadObject() method in SpecialSerial.
Answer:
-> C and F are correct. C is correct because static and transient variables are not serialized when an object is serialized. F is a valid statement.
-> A, B, D, and E are incorrect based on the above. G is incorrect because you don't
override the defaultReadObject() method, you call it from within the overridden
readObject()method, along with any custom read operations your class needs.
Page: 4
1
2
3
4