java.io package and Serialization
Q: 01 Click the Task button.
Solution:
reader = new BufferedReader(new FileReader("in");
writer = new PrintWriter (new BufferedWriter (new FileWriter("out")));
Q: 02 Given:
12. import java.io.*;
13. public class Forest implements Serializable {
14. private Tree tree = new Tree();
15. public static void main(String [] args) {
16. Forest f = new Forest();
17. try {
18. FileOutputStream fs = new FileOutputStream("Forest.ser");
19. ObjectOutputStream os = new ObjectOutputStream(fs);
20. os.writeObject(f); os.close();
21. } catch (Exception ex) { ex.printStackTrace(); }
22. } }
23.
24. class Tree { }
What is the result ?
- Compilation fails.
- An exception is thrown at runtime.
- An instance of Forest is serialized.
- An instance of Forest and an instance of Tree are both serialized.
Answer: B
Q: 03 Click the Task button.
Solution:
1. (temp = buffReader.readLine())
2. != null
3. (IOException e){
Q: 04
Assuming that the serializeBanana() and the deserializeBanana() methods will
correctly use Java serialization and given:
13. import java.io.*;
14. class Food implements Serializable {int good = 3;}
15. class Fruit extends Food {int juice = 5;}
16. public class Banana extends Fruit {
17. int yellow = 4;
18. public static void main(String [] args) {
19. Banana b = new Banana(); Banana b2 = new Banana();
20. b.serializeBanana(b); // assume correct serialization
21. b2 = b.deserializeBanana(); // assume correct
22. System.out.println("restore "+b2.yellow+ b2.juice+b2.good);
24. }
25. // more Banana methods go here 50. }
What is the result ?
- restore 400
- restore 403
- restore 453 D. Compilation fails.
- An exception is thrown at runtime.
Answer: C
Q: 05 Which three statements concerning the use of the java.io.Serializable interface are true ? (Choose three.)
- Objects from classes that use aggregation cannot be serialized.
- An object serialized on one JVM can be successfully deserialized on a different JVM.
- The values in fields with the volatile modifier will NOT survive serialization and deserialization.
- The values in fields with the transient modifier will NOT survive serialization and deserialization.
- It is legal to serialize an object of a type that has a supertype that does NOT implement java.io.Serializable.
Answer: B, D, E
Q: 06 Assuming that the serializeBanana2() and the deserializeBanana2() methods will correctly use Java serialization and given:
13. import java.io.*;
14. class Food {Food() { System.out.print("1"); } }
15. class Fruit extends Food implements Serializable {
16. Fruit() { System.out.print("2"); } }
17. public class Banana2 extends Fruit { int size = 42;
18. public static void main(String [] args) {
19. Banana2 b = new Banana2();
20. b.serializeBanana2(b); // assume correct serialization
21. b = b.deserializeBanana2(b); // assume correct
22. System.out.println(" restored " + b.size + " "); }
23. // more Banana2 methods
24. }
What is the result ?
- Compilation fails.
- 1 restored 42
- 12 restored 42
- 121 restored 42
- 1212 restored 42
- An exception is thrown at runtime.
Answer: D
Q: 7 When comparing java.io.BufferedWriter to java.io.FileWriter, which capability exists as a method in only one of the two ?
- closing the stream
- flushing the stream
- writing to the stream
- marking a location in the stream
- writing a line separator to the stream
Answer: E
Q : 8 Given:
10. class MakeFile {
11. public static void main(String[] args) {
12. try {
13. File directory = new File("d");
14. File file = new File(directory,"f");
15. if(!file.exists()) {
16. file.createNewFile();
17. }
18. } catch (IOException e) {
19. e.printStackTrace
20. }
21. }
22. }
The current directory does NOT contain a directory named "d."
Which three are true? (Choose three.)
- Line 16 is never executed.
- An exception is thrown at runtime.
- Line 13 creates a File object named ―d.‖
- Line 14 creates a File object named ―f.
- Line 13 creates a directory named ―d‖ in the file system.
- Line 16 creates a directory named ―d‖ and a file ‗f‘ within it in the file system.
- Line 14 creates a file named ‗f‘ inside of the directory named ―d‖ in the file system.
Answer: B, C, D
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 ?
- s.defaultReadObject();
- this = s.defaultReadObject();
- y = s.readInt(); x = s.readInt();
- 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 ?
- s.writeInt(x);
- s.serialize(x);
- s.writeObject(x);
- 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();
}
}
}
Question: 13
Which capability exists only in java.io.FileWriter ?
- Closing an open stream.
- Flushing an open stream.
- Writing to an open stream.
- Writing a line separator to an open stream.
Answer: D
Question: 14 Given that the current directory is empty, and that the user has read and write permissions, and the following:
11. import java.io.*;
12. public class DOS {
13. public static void main(String[] args) {
14. File dir = new File("dir");
15. dir.mkdir();
16. File f1 = new File(dir, "f1.txt");
17. try {
18. f1.createNewFile();
19. } catch (IOException e) { ; }
20. File newDir = new File("newDir");
21. dir.renameTo(newDir);
22. }
23. }
Which statement is true ?
- Compilation fails.
- The file system has a new empty directory named dir.
- The file system has a new empty directory named newDir.
- The file system has a directory named dir, containing a file f1.txt.
- The file system has a directory named newDir, containing a file f1.txt.
Answer: E
Question: 15 Given:
1. public class LineUp {
2. public static void main(String[] args) {
3. double d = 12.345;
4. // insert code here
5. }
6. }
Which code fragment, inserted at line 4, produces the output | 12.345| ?
- System.out.printf("|%7d| \n", d);
- System.out.printf("|%7f| \n", d);
- System.out.printf("|%3.7d| \n", d);
- System.out.printf("|%3.7f| \n", d);
- System.out.printf("|%7.3d| \n", d);
- System.out.printf("|%7.3f| \n", d);
Answer: F
Question: 16 Given:
5. import java.io.*;
6. public class Talk {
7. public static void main(String[] args) {
8. Console c = new Console();
9. String pw;
10. System.out.print("password: ");
11. pw = c.readLine();
12. System.out.println("got " + pw);
13. }
14. }
If the user types the password aiko when prompted, what is the result ?
- password: got
- password: got aiko
- password: aiko got aiko
- An exception is thrown at runtime.
- Compilation fails due to an error on line 8.
Answer: E
Question: 17
Given that the current directory is empty, and that the user has read and write privileges to the current directory, and the following:
1. import java.io.*;
2. public class Maker {
3. public static void main(String[] args) {
4. File dir = new File("dir");
5. File f = new File(dir, "f");
6. }
7. }
Which statement is true?
- Compilation fails.
- Nothing is added to the file system.
- Only a new file is created on the file system.
- Only a new directory is created on the file system.
- Both a new file and a new directory are created on the file system.
Answer: B
Question: 18 Given:
12. String csv = "Sue,5,true,3";
13. Scanner scanner = new Scanner( csv );
14. scanner.useDelimiter(",");
15. int age = scanner.nextInt();
What is the result ?
- Compilation fails.
- After line 15, the value of age is 5.
- After line 15, the value of age is 3.
- An exception is thrown at runtime.
Answer: D
Question: 19
Given that c is a reference to a valid java.io.Console object, which two code fragments read a line of text from the console? (Choose two.)
- String s = c.readLine();
- char[] c = c.readLine();
- String s = c.readConsole();
- char[] c = c.readConsole();
- String s = c.readLine("%s", "name ");
- char[] c = c.readLine("%s", "name ");
Answer: A,E
Question: 20
Given that c is a reference to a valid java.io.Console object, and:
11. String pw = c.readPassword("%s", "pw: ");
12. System.out.println("got " + pw);
13. String name = c.readLine("%s", "name: ");
14. System.out.println(" got ", name);
If the user types fido when prompted for a password, and then responds bob when prompted for a name, what is the result ?
- pw: got fido name: bob got bob
- pw: fido got fido name: bob got bob
- pw: got fido name: bob got bob
- pw: fido got fido name: bob got bob E. Compilation fails.
- An exception is thrown at runtime.
Answer: E
Question: 21
Given the following six method names:
addListener
addMouseListener
setMouseListener
deleteMouseListener
removeMouseListener
registerMouseListener
How many of these method names follow JavaBean Listener naming rules?
- 1
- 2
- 3
- 4
- 5
Answer: B
Question: 22
Click the Task button.
Answer:
23. 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 ?
- pc
- pcc
- pcp
- pcpc
- Compilation fails.
- 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.
24. 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 ?
- Compilation succeeds.
- Compilation fails due only to an error on line 15.
- Compilation fails due only to an error on line 16.
- Compilation fails due only to an error on line 17.
- Compilation fails due only to an error on line 18.
- 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.
25. 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.)
- exc
- done
- Compilation fails.
- Exactly one object is serialized.
- 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.
26. 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.)
- String path = d; System.out.print(file.exists() + " ");
- String path = d; System.out.print(file.isFile() + " ");
- String path = File.separator + d; System.out.print(file.exists() + " ");
- 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
27. 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.)
- Compilation fails.
- The output is 10 0 9
- The output is 10 0 10
- The output is 10 7 9
- The output is 10 7 10
- In order to alter the standard deserialization process you would override the readObject() method in SpecialSerial.
- 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.
|