Java OCA OCP Practice Question 2257

Question

Which one of the following options correctly reads a line of string from the console?

A.   BufferedReader br = new BufferedReader(System.in);

     String str = br.readLine();/*  w  ww.j a v  a 2 s.  c o  m*/

B.   BufferedReader br =

     new BufferedReader(new InputStreamReader(System.in));
     String str = br.readLine();

C.   InputStreamReader isr =

     new InputStreamReader (new BufferedReader(System.in));
     String str = isr.readLine();

d.   String str = System.in.readLine();

     String str;
     System.in.scanf(str);


B.

Note

this is the right way to read a line of a string from the console where you pass a system.in reference to Inputstreamreader and pass the returning reference to Bufferedreader.

From the Bufferedreader reference, you can call the readline() method to read the string from the console.




PreviousNext

Related