jobs4timesLogo jobs4timesLogo

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 ?
  1. Compilation fails.
  2. An exception is thrown at runtime.
  3. An instance of Forest is serialized.
  4. 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 ?
  1. restore 400
  2. restore 403
  3. restore 453 D. Compilation fails.
  4. 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.)

  1. Objects from classes that use aggregation cannot be serialized.
  2. An object serialized on one JVM can be successfully deserialized on a different JVM.
  3. The values in fields with the volatile modifier will NOT survive serialization and deserialization.
  4. The values in fields with the transient modifier will NOT survive serialization and deserialization.
  5. 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 ?
  1. Compilation fails.
  2. 1 restored 42
  3. 12 restored 42
  4. 121 restored 42
  5. 1212 restored 42
  6. 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 ?

  1. closing the stream
  2. flushing the stream
  3. writing to the stream
  4. marking a location in the stream
  5. 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.)
  1. Line 16 is never executed.
  2. An exception is thrown at runtime.
  3. Line 13 creates a File object named ―d.‖
  4. Line 14 creates a File object named ―f.
  5. Line 13 creates a directory named ―d‖ in the file system.
  6. Line 16 creates a directory named ―d‖ and a file ‗f‘ within it in the file system.
  7. 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 ?
  1. s.defaultReadObject();
  2. this = s.defaultReadObject();
  3. y = s.readInt(); x = s.readInt();
  4. 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 ?
  1. s.writeInt(x);
  2. s.serialize(x);
  3. s.writeObject(x);
  4. 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 ?
  1. Closing an open stream.
  2. Flushing an open stream.
  3. Writing to an open stream.
  4. 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 ?
  1. Compilation fails.
  2. The file system has a new empty directory named dir.
  3. The file system has a new empty directory named newDir.
  4. The file system has a directory named dir, containing a file f1.txt.
  5. 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| ?
  1. System.out.printf("|%7d| \n", d);
  2. System.out.printf("|%7f| \n", d);
  3. System.out.printf("|%3.7d| \n", d);
  4. System.out.printf("|%3.7f| \n", d);
  5. System.out.printf("|%7.3d| \n", d);
  6. 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 ?
  1. password: got
  2. password: got aiko
  3. password: aiko got aiko
  4. An exception is thrown at runtime.
  5. 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?
  1. Compilation fails.
  2. Nothing is added to the file system.
  3. Only a new file is created on the file system.
  4. Only a new directory is created on the file system.
  5. 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 ?
  1. Compilation fails.
  2. After line 15, the value of age is 5.
  3. After line 15, the value of age is 3.
  4. 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.)
  1. String s = c.readLine();
  2. char[] c = c.readLine();
  3. String s = c.readConsole();
  4. char[] c = c.readConsole();
  5. String s = c.readLine("%s", "name ");
  6. 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 ?
  1. pw: got fido name: bob got bob
  2. pw: fido got fido name: bob got bob
  3. pw: got fido name: bob got bob
  4. pw: fido got fido name: bob got bob E. Compilation fails.
  5. 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. 1
  2. 2
  3. 3
  4. 4
  5. 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 ?
  1. pc
  2. pcc
  3. pcp
  4. pcpc
  5. Compilation fails.
  6. 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 ?
  1. Compilation succeeds.
  2. Compilation fails due only to an error on line 15.
  3. Compilation fails due only to an error on line 16.
  4. Compilation fails due only to an error on line 17.
  5. Compilation fails due only to an error on line 18.
  6. 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.)
  1. exc
  2. done
  3. Compilation fails.
  4. Exactly one object is serialized.
  5. 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.)
  1. String path = d; System.out.print(file.exists() + " ");
  2. String path = d; System.out.print(file.isFile() + " ");
  3. String path = File.separator + d; System.out.print(file.exists() + " ");
  4. 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.)
  1. Compilation fails.
  2. The output is 10 0 9
  3. The output is 10 0 10
  4. The output is 10 7 9
  5. The output is 10 7 10
  6. In order to alter the standard deserialization process you would override the readObject() method in SpecialSerial.
  7. 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.


BACK