SCJP 6 Mock exam for I/O


Free mock exam for Input/Output, Files and Serialization.

It covers File, FileReader, FileWriter, BufferedReader, BufferedWriter, FileInputStream, FileOutputStream, PrintWriter, ObjectInputStream, ObjectOutputStream.

Questions 1-9 are about File, 10-26 for Input/Output and 27-50 specific to Serialization.

Here is a simple overview of some interesting classes in the java.io package. The arrows denote decoration.

decoration in some IO classes

decoration in some IO classes

  1. What happens when this code is compiled and executed? (1 correct answer)
    String name = null;
    File file = new File(name);
    System.out.println(file.getName());
    
    1. It prints “null”.
    2. NullPointerException at second line.
    3. NullPointerException at third line.
    4. Compilation fails.
  2. What happens when this code is compiled and executed? (1 correct answer)
    String name = null;
    File file = new File("/folder", name);
    System.out.print(file.exists());
    
    1. It prints “true”.
    2. It prints “false”.
    3. NullPointerException at second line.
    4. NullPointerException at third line.
  3. What happens when this code is compiled and executed? (1 correct answer)
    void test() throws IOException {
    String parent = null;
    File file = new File(parent, "myfile.txt");
    file.createNewFile();
    }
    
    1. An empty file myfile.txt is created at the current directory.
    2. An empty file myfile.txt is created at the root directory.
    3. NullPointerException at second line.
    4. NullPointerException at third line.
  4. This program is executed in Windows. In Windows systems root is C:\ and the separator char is \.

    File file = new File("/file.txt");
    

    What is the output of the following statements?

    1. System.out.println(“path → ” + file.getPath());
    2. System.out.println(“absolute → ”
                + file.getAbsolutePath());
    3. System.out.println(“canonical → ”
                + file.getCanonicalPath());
  5. This program is executed in folder C:\documents of Windows. In Windows systems root is C:\ and the separator char is \.

    File file = new File("file.txt");
    

    What is the output of the following statements?

    1. System.out.println(“path → ” + file.getPath());
    2. System.out.println(“absolute → “
                + file.getAbsolutePath());
    3. System.out.println(“canonical → “
                + file.getCanonicalPath());
  6. This program is executed in folder C:\parent\child of Windows. In Windows systems root is C:\ and the separator char is \.

    File file = new File("../file.txt");
    

    What is the output of the following statements?

    1. System.out.println(“path → ” + file.getPath());
    2. System.out.println(“absolute → “
                + file.getAbsolutePath());
    3. System.out.println(“canonical → “
                + file.getCanonicalPath());
  7. This program is executed in OpenSolaris. In Unix systems root is / and separator char is /.

    File file = new File("/file.txt");
    

    What is the output of the following statements?

    1. System.out.println(“path → ” + file.getPath());
    2. System.out.println(“absolute → “
                + file.getAbsolutePath());
    3. System.out.println(“canonical → “
                + file.getCanonicalPath());
  8. This program is executed in directory /usr/prog of OpenSolaris. In Unix systems root is / and separator char is /.

    File file = new File("file.txt");
    

    What is the output of the following statements?

    1. System.out.println(“path → ” + file.getPath());
    2. System.out.println(“absolute → “
                + file.getAbsolutePath());
    3. System.out.println(“canonical → “
                + file.getCanonicalPath());
  9. This program is executed in directory /test of OpenSolaris. In Unix systems root is / and separator char is /.

    File file = new File("../file.txt");
    

    What is the output of the following statements?

    1. System.out.println(“path → ” + file.getPath());
    2. System.out.println(“absolute → “
                + file.getAbsolutePath());
    3. System.out.println(“canonical → “
                + file.getCanonicalPath());
  10. Which class is used to read streams of characters from a file? (1 correct answer)

    1. FileReader
    2. FileWriter
    3. FileInputStream
    4. FileOutputStream
  11. Which class is used to write streams of raw bytes to a file? (1 correct answer)

    1. FileReader
    2. FileWriter
    3. FileInputStream
    4. FileOutputStream
  12. What is the name of the abstract parent class of FilerWriter?
  13. What is the name of the abstract parent class of FileInputStream?
  14. Is this a valid statement?

    new BufferedWriter(new BufferedWriter(
    new BufferedWriter(new FileWriter("myfile.txt"))));
    
  15. Consider an empty file C:/empty.txt. What happens when this code is compiled and executed? (1 correct answer)
    void test() throws IOException {
    BufferedReader reader = new BufferedReader(
              new FileReader("C:/empty.txt"));
    System.out.println(reader.getLine());
    }
    
    1. It prints an empty string “”.
    2. It prints null.
    3. A NullPointerException is thrown at runtime.
    4. Compilation fails.
  16. Consider an empty file C:/empty.txt. What happens when this code is compiled and executed? (1 correct answer)
    void test() throws IOException {
    BufferedReader reader = new BufferedReader(
              new FileReader("C:/empty.txt"));
    System.out.println(reader.readLine());
    }
    
    1. It prints an empty string “”.
    2. It prints null.
    3. A NullPointerException is thrown at runtime.
    4. Compilation fails.
  17. What happens when this code is compiled and executed? (1 correct answer)
    void test() {
    FileWriter writer = new FileWriter("/fun.log");
    writer.write("Hello!");
    writer.close();
    }
    
    1. A file fun.log is created with the content “Hello!”.
    2. A file fun.log is created but it’s empty, because flush() was not called.
    3. A runtime exception is thrown because flush() was not called.
    4. Compilation fails.
  18. What happens when this code is compiled and executed? (1 correct answer)
    void test() throws IOException {
    FileWriter writer = new FileWriter("/fun.log");
    writer.write("Hello!");
    writer.close();
    }
    
    1. A file fun.log is created with the content “Hello!”.
    2. A file fun.log is created but it’s empty, because flush() was not called.
    3. A runtime exception is thrown because flush() was not called.
    4. Compilation fails.
  19. Consider a file fun.log whose first line is “Hello!”. What happens when this code is compiled and executed? (1 correct answer)
    void test() throws IOException {
    File file = new File("C:/fun.log");
    BufferedReader reader = new BufferedReader(file);
    System.out.println(reader.readLine());
    }
    
    1. A runtime exception is thrown because the file already exists.
    2. It prints “Hello!”.
    3. It prints “null”.
    4. Compilation fails.
  20. What happens when this code is compiled and executed? (1 correct answer)
    void test() throws IOException {
    PrintWriter writer = new PrintWriter(new PrintWriter("yo.txt"));
    writer.print("yo");
    writer.flush();
    writer.close();
    }
    
    1. Compilation fails because a PrintWriter cannot accept a PrintWriter in its constructor.
    2. Compilation fails because there is no method print().
    3. Two files yo.txt with the same content are created.
    4. A file yo.txt with the content “yo” is created.
  21. What happens when this code is compiled and executed? (1 correct answer)
    void test() throws IOException {
    for (int index = 1; index <= 2; index++) {
    PrintWriter writer = new PrintWriter("/apa");
    writer.print("apa");
    writer.close();
    }
    }
    
    1. A file apa is created with content “apa”.
    2. A file apa is created with content “apaapa”.
    3. Two files are created.
    4. An exception is thrown at runtime.
  22. The file fun.log has already some content. We want to keep the content of the file and add a new line at the end. Which statement can achieve this? (1 correct answer)
    void test() throws IOException {
    PrintWriter writer = new PrintWriter("C:/fun.log");
    // insert statement here
    writer.flush();
    writer.close();
    }
    
    1. writer.println(“this is a new line”);
    2. writer.append(“this a new line”);
    3. writer.append(“\nthis is a new line”);
    4. None of the above.
  23. Is this a valid statement?

    OutputStream out = new OutputStream(new File("directory", "file"));
    
  24. Consider the file empty.txt really exists and is empty. What is the output of this method? (1 correct answer)
    void test() throws IOException {
    FileInputStream input = new FileInputStream("/empty.txt");
    int i = input.read();
    input.close();
    System.out.println(i);
    }
    
    1. It prints 0.
    2. It prints -1.
    3. Compilation fails.
    4. An exception is thrown at runtime when the read() method is invoked.
  25. What happens when this code is compiled and executed? (1 correct answer)
    void test() throws IOException {
    FileOutputStream out = new FileOutputStream("shakira.mp3");
    out.write(1000);
    out.flush();
    out.close();
    }
    
    1. Compilation fails because 1000 is not a byte.
    2. A runtime exception is thrown because 1000 is not a byte.
    3. A runtime exception is thrown because shakira.mp3 is a music file!
    4. The program compiles and runs fine.
  26. What is the output of this method? (1 correct answer)
    void test() throws IOException {
    // write
    OutputStream out = new FileOutputStream("/my");
    out.write(22);
    out.close();
    // read
    InputStream input = new FileInputStream("/my");
    System.out.println(input.read() + " " + input.read());
    input.close();
    }
    
    1. 22 -1
    2. 22 0
    3. 22 null
    4. A runtime exception is thrown.
  27. What happens when this code is compiled and executed? (1 correct answer)
    import java.io.*;
    
    public class Person {
    
    String name;
    int age;
    
    public Person(String name, int age) {
    this.age = age;
    this.name = name;
    }
    
    public String toString() {
    return "Hello it's " + name + "! I am " + age + " years old.";
    }
    
    public static void main(String[] args)
    throws ClassNotFoundException, IOException {
    Person before = new Person("Shakira", 30);
    ObjectOutputStream out = new ObjectOutputStream(
    new FileOutputStream("/person"));
    out.writeObject(before);
    
    ObjectInputStream input = new ObjectInputStream(
    new FileInputStream("/person"));
    Person after = (Person)input.readObject();
    System.out.println(after);
    }
    
    }
    
    1. It prints “Hello it’s Shakira! I am 30 years old”.
    2. A ClassNotFoundException is thrown because Person does not implement Serializable.
    3. A NotSerializableException is thrown because Person does not implement Serializable.
    4. Compilation fails because NotSerializableException is not handled properly.
    5. Compilation fails for another reason.
  28. What happens when this code is compiled and executed? (1 correct answer)
    import java.io.*;
    
    public class Person implements Serializable {
    
    final String name;
    int age;
    
    public Person(String name, int age) {
    this.age = age;
    this.name = name;
    }
    
    public String toString() {
    return "Hello it's " + name + "! I am " + age + " years old.";
    }
    
    public static void main(String[] args) throws Exception {
    Person before = new Person("Shakira", 30);
    ObjectOutputStream out = new ObjectOutputStream(
    new FileOutputStream("/person"));
    out.writeObject(before);
    
    ObjectInputStream input = new ObjectInputStream(
    new FileInputStream("/person"));
    Person after = (Person)input.readObject();
    System.out.println(after);
    }
    
    }
    
    1. It prints “Hello it’s Shakira! I am 30 years old”.
    2. It prints “Hello it’s null! I am 30 years old”.
    3. It prints “Hello it’s ! I am 30 years old”.
    4. Compilation fails.
  29. What happens when this code is compiled and executed? (1 correct answer)
    import java.io.*;
    
    public class Person implements Serializable {
    
    transient String name;
    transient int age;
    
    public Person(String name, int age) {
    this.age = age;
    this.name = name;
    }
    
    public String toString() {
    return "Hello it's " + name + "! I am " + age + " years old.";
    }
    
    public static void main(String[] args) throws Exception {
    Person before = new Person("Shakira", 30);
    ObjectOutputStream out = new ObjectOutputStream(
    new FileOutputStream("/person"));
    out.writeObject(before);
    
    ObjectInputStream input = new ObjectInputStream(
    new FileInputStream("/person"));
    Person after = (Person)input.readObject();
    System.out.println(after);
    }
    
    }
    
    1. It prints “Hello it’s Shakira! I am 30 years old”.
    2. It prints “Hello it’s null! I am 0 years old”.
    3. It prints “Hello it’s null! I am -1 years old”.
    4. It prints “Hello it’s ! I am 0 years old”.
    5. It prints “Hello it’s ! I am -1 years old”.
    6. A NotSerializableException is thrown because all the state of Person is transient.
    7. Compilation fails.
  30. Consider the two classes.
    public class Parent implements Serializable {
    protected String hello = "Hello!";
    }
    
    public class Child extends Parent {
    int age = 11;
    public String toString() {
    return "age=" + age + " hello=" + hello;
    }
    }
    

    What happens when the following statements get executed? (1 correct answer)

    ObjectOutputStream out = new ObjectOutputStream(
    new FileOutputStream("/object"));
    out.writeObject(new Child());
    
    ObjectInputStream in = new ObjectInputStream(
    new FileInputStream("/object"));
    System.out.print(in.readObject());
    
    1. It prints “age=11 hello=Hello!”.
    2. It prints “age=11 hello=null”.
    3. It prints “age=0 hello=Hello!”.
    4. It prints “age=0 hello=null”.
    5. An exception is thrown at runtime, because Child does not implement Serializable directly.
  31. Consider the two classes.
    public class Parent {
    protected String hello = "Hello!";
    }
    
    public class Child extends Parent implements Serializable {
    int age = 11;
    public String toString() {
    return "age=" + age + " hello=" + hello;
    }
    }
    

    What happens when the following statements get executed? (1 correct answer)

    ObjectOutputStream out = new ObjectOutputStream(
    new FileOutputStream("/object"));
    out.writeObject(new Child());
    
    ObjectInputStream in = new ObjectInputStream(
    new FileInputStream("/object"));
    System.out.print(in.readObject());
    
    1. It prints “age=11 hello=Hello!”.
    2. It prints “age=11 hello=null”.
    3. It prints “age=0 hello=Hello!”.
    4. It prints “age=0 hello=null”.
    5. An exception is thrown at runtime, because Parent does not implement Serializable.
  32. Consider the two classes.
    public class Parent {
    protected String hello = "Hello!";
    }
    
    public class Child extends Parent implements Serializable {
    transient int age = 11;
    public String toString() {
    return "age=" + age + " hello=" + hello;
    }
    }
    

    What happens when the following statements get executed? (1 correct answer)

    ObjectOutputStream out = new ObjectOutputStream(
    new FileOutputStream("/object"));
    out.writeObject(new Child());
    
    ObjectInputStream in = new ObjectInputStream(
    new FileInputStream("/object"));
    System.out.print(in.readObject());
    
    1. It prints “age=11 hello=Hello!”.
    2. It prints “age=11 hello=null”.
    3. It prints “age=0 hello=Hello!”.
    4. It prints “age=0 hello=null”.
    5. An exception is thrown at runtime, because the field age of Child is transient.
  33. Consider the two classes.
    public class Parent {
    protected transient String hello = "Hello!";
    }
    
    public class Child extends Parent implements Serializable {
    int age = 11;
    public String toString() {
    return "age=" + age + " hello=" + hello;
    }
    }
    

    What happens when the following statements get executed? (1 correct answer)

    ObjectOutputStream out = new ObjectOutputStream(
    new FileOutputStream("/object"));
    out.writeObject(new Child());
    
    ObjectInputStream in = new ObjectInputStream(
    new FileInputStream("/object"));
    System.out.print(in.readObject());
    
    1. It prints “age=11 hello=Hello!”.
    2. It prints “age=11 hello=null”.
    3. It prints “age=0 hello=Hello!”.
    4. It prints “age=0 hello=null”.
    5. Compilation fails because Parent declares a transient field but does not implement Serializable.
  34. Consider the two classes.
    public class Parent {
    protected String hello = "Hello!";
    }
    
    public class Child extends Parent implements Serializable {
    int age = 11;
    public Child(int age) {
    this.age = age;
    }
    public String toString() {
    return "age=" + age + " hello=" + hello;
    }
    }
    

    What happens when the following statements get executed? (1 correct answer)

    ObjectOutputStream out = new ObjectOutputStream(
    new FileOutputStream("/object"));
    out.writeObject(new Child(7));
    
    ObjectInputStream in = new ObjectInputStream(
    new FileInputStream("/object"));
    System.out.print(in.readObject());
    
    1. It prints “age=7 hello=Hello!”.
    2. It prints “age=11 hello=Hello!”.
    3. Compilation fails because Child does not have a no-argument constructor.
  35. Consider the two classes.
    public class Parent implements Serializable {
    protected String hello = "Hello!";
    }
    
    public class Child extends Parent {
    int age = 11;
    public Child(int age) {
    this.age = age;
    }
    public String toString() {
    return "age=" + age + " hello=" + hello;
    }
    }
    

    What happens when the following statements get executed? (1 correct answer)

    ObjectOutputStream out = new ObjectOutputStream(
    new FileOutputStream("/object"));
    out.writeObject(new Child(7));
    
    ObjectInputStream in = new ObjectInputStream(
    new FileInputStream("/object"));
    System.out.print(in.readObject());
    
    1. It prints “age=7 hello=Hello!”.
    2. It prints “age=11 hello=Hello!”.
    3. Compilation fails because Child does not have a no-argument constructor.
  36. Consider the two classes.
    public class Parent implements Serializable {
    protected String hello = "Hello!";
    }
    
    public class Child extends Parent {
    int age = 11;
    public Child(int age, String hello) {
    this.age = age;
    this.hello = hello;
    }
    public String toString() {
    return "age=" + age + " hello=" + hello;
    }
    }
    

    What happens when the following statements get executed? (1 correct answer)

    ObjectOutputStream out = new ObjectOutputStream(
    new FileOutputStream("/object"));
    out.writeObject(new Child(7, "Zorro"));
    
    ObjectInputStream in = new ObjectInputStream(
    new FileInputStream("/object"));
    System.out.print(in.readObject());
    
    1. It prints “age=7 hello=Hello!”.
    2. It prints “age=7 hello=Zorro!”.
    3. It prints “age=11 hello=Hello!”.
    4. It prints “age=11 hello=Zorro!”.
  37. Consider the two classes.
    public class Parent {
    protected String hello = "Hello!";
    }
    
    public class Child extends Parent implements Serializable {
    int age = 11;
    public Child(int age, String hello) {
    this.age = age;
    this.hello = hello;
    }
    public String toString() {
    return "age=" + age + " hello=" + hello;
    }
    }
    

    What happens when the following statements get executed? (1 correct answer)

    ObjectOutputStream out = new ObjectOutputStream(
    new FileOutputStream("/object"));
    out.writeObject(new Child(7, "Zorro"));
    
    ObjectInputStream in = new ObjectInputStream(
    new FileInputStream("/object"));
    System.out.print(in.readObject());
    
    1. It prints “age=7 hello=Hello!”.
    2. It prints “age=7 hello=Zorro!”.
    3. It prints “age=11 hello=Hello!”.
    4. It prints “age=11 hello=Zorro!”.
  38. Consider the following classes.
    public class Team implements Serializable {
    Coach coach = null;
    }
    
    public class Coach {
    
    }
    

    What happens when an object of Team gets serialized? (1 correct answer)

    1. Compilation fails because Team has a non-serializable field.
    2. A NotSerializableException is thrown at runtime, because Coach does not implement Serializable.
    3. A NullPointerException is thrown at runtime, because the field coach is null.
    4. Serialization succeeds.
  39. Consider the following classes.
    public class Team implements Serializable {
    Coach coach = new Coach();
    }
    
    public class Coach {
    
    }
    

    What happens when an object of Team gets serialized? (1 correct answer)

    1. Compilation fails because Team has a non-serializable field.
    2. A NotSerializableException is thrown at runtime, because Coach does not implement Serializable.
    3. Serialization succeeds, but the coach object does not get serialized.
    4. Serialization succeeds, including the coach object.
  40. Consider the following classes.
    public class Team implements Serializable {
    Coach coach = new Coach();
    Player[] players = new Player[5];
    }
    
    public class Coach implements Serializable {
    
    }
    
    public class Player {
    
    }
    

    What happens when an object of Team gets serialized? (1 correct answer)

    1. A NotSerializableException is thrown at runtime, because Player does not implement Serializable.
    2. A NotSerializableException is thrown at runtime, because arrays cannot get serialized.
    3. Serialization succeeds, but the array of players does not get serialized.
    4. Serialization succeeds, including the array of players.
  41. Consider the following classes.
    public class Team implements Serializable {
    Coach coach = new Coach();
    Player[] players = new Player[5];
    
    public Team() {
    players[0] = new Player();
    players[1] = new Player();
    players[2] = new Player();
    players[3] = new Player();
    players[4] = new Player();
    }
    }
    
    public class Coach implements Serializable {
    
    }
    
    public class Player {
    
    }
    

    What happens when an object of Team gets serialized? (1 correct answer)

    1. A NotSerializableException is thrown at runtime, because Player does not implement Serializable.
    2. A NotSerializableException is thrown at runtime, because arrays cannot get serialized.
    3. Serialization succeeds, but the array of players does not get serialized.
    4. Serialization succeeds, including the array of players.
  42. Consider the following classes.
    public class A implements Serializable {
    private final B field = new B();
    }
    public class B implements Serializable {
    private final A field = new A();
    }
    

    What happens when an object of A gets serialized? (1 correct answer)

    1. A StackOverflowError is thrown, not related to serialization.
    2. A StackOverflowError is thrown, because serialization gets into a cyclic loop.
    3. Compilation fails because serializable fields cannot be final.
    4. Serialization succeeds.
  43. Consider this class.
    public class Collection implements Serializable {
    private volatile NavigableSet<Integer> numbers;
    
    public Collection() {
    numbers = new TreeSet<Integer>();
    numbers.add(1);
    numbers.add(1453);
    numbers.add(007);
    }
    }
    

    What happens when an object of this class gets serialized? (1 correct answer)

    1. Compilation fails because Collection is a reserved word in Java.
    2. A NotSerializableException is thrown at runtime, because a TreeSet cannot be serialized.
    3. A NotSerializableException is thrown at runtime, because primitive numbers (1, 1453 etc) cannot get serialized.
    4. An InvalidClassException is thrown at runtime due to the volatile modifier.
    5. Serialization succeeds.
  44. Consider these classes.
    public class Grandparent implements Serializable {
    public Grandparent(int number) {
    }
    }
    
    public class Parent extends Grandparent {
    public Parent(int number) {
    super(number);
    }
    }
    public class Child extends Parent {
    public Child(int number) {
    super(number);
    }
    }
    

    What happens when an object of class Child is serialized? (1 correct answer)

    1. An exception is thrown at runtime, because Grandparent does not provide a no-arguments constructor.
    2. An exception is thrown at runtime, because Parent does not provide a no-arguments constructor.
    3. An exception is thrown at runtime, because Child does not provide a no-arguments constructor.
    4. Serialization succeeds.
  45. Consider these classes.
    public class Grandparent {
    public Grandparent(int number) {
    }
    }
    
    public class Parent extends Grandparent implements Serializable {
    public Parent(int number) {
    super(number);
    }
    }
    public class Child extends Parent {
    public Child(int number) {
    super(number);
    }
    }
    

    What happens when an object of class Child is serialized? (1 correct answer)

    1. An exception is thrown at runtime, because Grandparent does not provide a no-argument constructor.
    2. An exception is thrown at runtime, because Parent does not provide a no-argument constructor.
    3. An exception is thrown at runtime, because Child does not provide a no-argument constructor.
    4. Serialization succeeds.
  46. Consider these classes.
    public class Grandparent implements Serializable {
    public Grandparent() {
    System.out.println("A");
    }
    }
    
    public class Parent extends Grandparent {
    public Parent() {
    System.out.println("B");
    }
    }
    public class Child extends Parent {
    public Child() {
    System.out.println("C");
    }
    }
    

    What gets printed after the following code is executed? (1 correct answer)

    ObjectOutputStream out = new ObjectOutputStream(
    new FileOutputStream("child"));
    out.writeObject(new Child());
    
    ObjectInputStream in = new ObjectInputStream(
    new FileInputStream("child"));
    System.out.print(in.readObject());
    
    1. A B C
    2. C B A
    3. A B C A
  47. Consider these classes.
    public class Grandparent {
    public Grandparent() {
    System.out.println("A");
    }
    }
    
    public class Parent extends Grandparent implements Serializable {
    public Parent() {
    System.out.println("B");
    }
    }
    public class Child extends Parent {
    public Child() {
    System.out.println("C");
    }
    }
    

    What gets printed after the following code is executed? (1 correct answer)

    ObjectOutputStream out = new ObjectOutputStream(
    new FileOutputStream("child"));
    out.writeObject(new Child());
    
    ObjectInputStream in = new ObjectInputStream(
    new FileInputStream("child"));
    System.out.print(in.readObject());
    
    1. A B C
    2. C B A
    3. A B C A
  48. Consider these classes.
    public class Grandparent {
    public Grandparent() {
    System.out.println("A");
    }
    }
    
    public class Parent extends Grandparent {
    public Parent() {
    System.out.println("B");
    }
    }
    public class Child extends Parent implements Serializable {
    public Child() {
    System.out.println("C");
    }
    }
    

    What gets printed after the following code is executed? (1 correct answer)

    ObjectOutputStream out = new ObjectOutputStream(
    new FileOutputStream("child"));
    out.writeObject(new Child());
    
    ObjectInputStream in = new ObjectInputStream(
    new FileInputStream("child"));
    System.out.print(in.readObject());
    
    1. A B C
    2. C B A
    3. A B C A B
    4. A B C B A
  49. Consider these classes.
    public class Grandparent {
    public Grandparent() {
    System.out.println("A");
    }
    public Grandparent(int number) {
    System.out.println("1");
    }
    }
    
    public class Parent extends Grandparent {
    public Parent() {
    System.out.println("B");
    }
    public Parent(int number) {
    super(number);
    System.out.println("2");
    }
    }
    public class Child extends Parent implements Serializable {
    public Child() {
    System.out.println("C");
    }
    public Child(int number) {
    super(number);
    System.out.println("3");
    }
    }
    

    What gets printed after the following code is executed? (1 correct answer)

    ObjectOutputStream out = new ObjectOutputStream(
    new FileOutputStream("child"));
    out.writeObject(new Child(123));
    
    ObjectInputStream in = new ObjectInputStream(
    new FileInputStream("child"));
    System.out.print(in.readObject());
    
    1. 1 2 3
    2. 1 2 3 A
    3. 1 2 3 A B
    4. 1 2 3 A B C
  50. Consider this class.
    public class Last implements Serializable{
    String name;
    int age;
    
    public Last(String name, int age) {
    this.name = name;
    this.age = age;
    }
    private void writeObject(ObjectOutputStream stream) 
                        throws IOException {
    stream.writeUTF(name);
    stream.writeInt(age);
    }
    private void readObject(ObjectInputStream stream) 
                        throws IOException {
    name = stream.readUTF();
    age = stream.readInt();
    }
    public String toString() {
    return name + " " + age;
    }
    }
    

    What gets printed after the following code is executed? (1 correct answer)

    ObjectOutputStream out = new ObjectOutputStream(
    new FileOutputStream("/object"));
    out.writeObject(new Last("Old", 150));
    
    ObjectInputStream in = new ObjectInputStream(
    new FileInputStream("/object"));
    System.out.print(in.readObject());
    
    1. Old 150
    2. null 0
    3. A runtime exception is thrown because writeObject() and readObject() are private.
    4. A runtime exception is thrown in readObject() because the age should be read first.
    5. A runtime exception is thrown because defaultWriteObject() and defaultReadObject() are never called.
    6. Compiler error, because readObject() does not throw a ClassNotFoundException.
    7. Compiler error, because defaultWriteObject() and defaultReadObject() must be called first.
    8. Compiler error, because the visibility of writeObject() and readObject() cannot be reduced.
    9. Compiler error, because writeObject() and readObject() should be the last methods of this class.

Answers

  1. b
  2. b
  3. a
    1. path -> \file.txt
    2. absolute -> C:\file.txt
    3. canonical -> C:\file.txt
    1. path -> file.txt
    2. absolute -> C:\documents\file.txt
    3. canonical -> C:\documents\file.txt
    1. path -> ..\file.txt
    2. absolute -> C:\parent\child\..\file.txt
    3. canonical -> C:\parent\file.txt
    1. path -> /file.txt
    2. absolute -> /file.txt
    3. canonical -> /file.txt
    1. path -> file.txt
    2. absolute -> /usr/prog/file.txt
    3. canonical -> /usr/prog/file.txt
    1. path -> ../file.txt
    2. absolute -> /test/../file.txt
    3. canonical -> /file.txt
  4. a
  5. c
  6. Writer
  7. InputStream
  8. Yes
  9. d
  10. b
  11. d
  12. a
  13. d
  14. d
  15. a
  16. d
  17. No
  18. b
  19. d
  20. a
  21. c
  22. a
  23. b
  24. a
  25. a
  26. c
  27. a
  28. a
  29. a
  30. b
  31. a
  32. d
  33. b
  34. d
  35. a
  36. a
  37. e
  38. d
  39. a
  40. a
  41. c
  42. c
  43. c
  44. a

References

A friendly community of people preparing for SCJP can be found at the JavaRanch.

Please feel free to communicate for any suggestion!

Thanks, Nikos.

About these ads

18 Responses to SCJP 6 Mock exam for I/O

  1. [...] Free SCJP Mock exam for I/O [...]

  2. Tommy Zhu says:

    Thank you, very useful content.

  3. LuCisFero says:

    Question 28:
    The answer should be “d” Compilation fails.

    Because this line “final String name;”.

    And thanks for mock exam it very useful

  4. M.Khan says:

    Thanks a lot for these question set its very useful. However
    Question 2: answer should be c, NullPointerException at second line . when name is null. I tested it, it throws a null pointer exception. Also according to API .

    Question 11, answer should be d, FileOutputStream is used to write stream to a file.

    Please Let me know if I am wrong.
    Thanks,

  5. MisiekB says:

    Question nr 28 is correct. The final variable is initialized in constructor so all is good. You have 3 places where can You initialize final variable:
    1) in declaration
    2) init block
    3) constructor

  6. Ashok says:

    Good one !! Hats off to you Niko !!

  7. S says:

    Thanx for the questions, keep up good work ;)

  8. Leandro says:

    There’s an error in question 46. The output is: A B C and the return of toString() method, eg: Child@a62fc3

  9. Leandro says:

    47 has the same problem as 46

  10. Tauseef Raza says:

    Answer of question # 1 is NullPointerException at third line…

  11. me says:

    I was shocked when I got through first couple questions – shocked that I did not know such simple/basic things. Later, was much better!

    Thanks. Good one.

  12. Azat says:

    Hello,

    first of all thank you very much for a nicely prepared tests.
    I have a proposal on some questions (may be it is a just typo error).

    at Free SCJP Mock exam for I/O section:

    Question 2:
    Your answer is “b” – false,
    but I tested it should be “c” – NullPointerException at second line.

    Question 11:
    Your answer is “c” – FileInputStream,
    but it should be “d” – FileOutputStream

    Thank you again for your answer in advance.
    S. Azat

  13. Neha says:

    ques 17 answer should be 1 it creates a file named fun.log on my computer which contains hello!

  14. Nicole L. says:

    the answer of Question nr 17 is correct, because IOException should be thrown

  15. sureshvenkat says:

    Answer for 41 is wrong, it throws NotSerializableException as player is not serializable.

    so answer should be ‘a’;

  16. Please review I/O section:
    – formatting with code is bad in Firefox 4
    – answers list is using letters instead numbers:
    50 a -> 50 1
    – review 2, 11, 14, 17, 20, 23

    Thank you for you great work!

  17. leena says:

    i have problem in ques 38,39
    i can’t understand that if coach doesn’t implement serializable how can team class is serializable ???????please somebody help
    thanx in advance………….

  18. rkb says:

    Errata maybe? After testing the examples in the I/O section, I found the following to be correct:

    Q2 (b), Q11(d), Q33(b), Q37(b)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 76 other followers

%d bloggers like this: