Java OCA OCP Practice Question 2050

Question

Which code, when inserted at (1), will result in the program compiling and running without errors?.

import java.io.*;

public class Main {
  public static void main(String[] args) {
    try {/* w  ww.  java 2s.c  o m*/
      String fileName = "greetings.txt";
      // (1) INSERT CODE HERE ...
      writeGreetings(stream);
      stream.close();
    } catch (IOException ioe) {
      System.out.println("I/O error");
    }
  }

  private static void writeGreetings(Writer writer) {
    try {
      BufferedWriter bw = new BufferedWriter(writer);
      bw.write("Hello");
      bw.newLine();
      bw.write("Howdy");
      bw.newLine();
      bw.flush();
    } catch (IOException ioe) {
      System.out.println("I/O error");
    }
  }
}

Select the three correct answers.

(a)  FileOutputStream fos = new FileOutputStream(fileName);
(b)  OutputStreamWriter stream = new OutputStreamWriter(fos);
(c)  FileOutputStream fos = new FileOutputStream(fileName);
(d)  InputStreamWriter stream = new InputStreamWriter(fos);
(e)  FileOutputStream stream = new FileOutputStream(fileName);
(f)  PrintWriter stream = new PrintWriter(fileName);
(g)  FileWriter stream = new FileWriter(fileName);


(a), (d), and (e)

Note

  • (b) There is no stream called InputStreamWriter.
  • (c) The class FileOutputStream is not a Writer.



PreviousNext

Related