Java OCA OCP Practice Question 373

Question

Which of the following lines can be inserted at line 1 to make the program run?

//line 1 
public class Main{ 
  public static void main (String [] args){ 
      PrintWriter pw = new PrintWriter(System.out); 
      OutputStreamWriter osw  =  new OutputStreamWriter(System.out); 
      pw.print("hello"); 
   } 
} 

Assume that PrintWriter and OutputStreamWriter are valid classes in java.io package.

Select 1 option

  • A. import java.lang.*;
  • B. import java.io.*;
  • C. import java.io.OutputStreamWriter;
  • D. include java.io.*;
  • E. include java.lang.System;


Correct Option is  : B

Note

A. import java.lang.*;

Although you can import java.lang package explicitly, it is not required

because this package is always imported by the compiler.

B. import java.io.*;

This will make all the classes of java.io package available.

C. import java.io.OutputStreamWriter;

This will only make OutputStreamwriter available.

PrintWriter will still be unavailable.

D. is wrong since include is not valid keyword in Java.




PreviousNext

Related