What will be the output of the following Java program?

import java.io.*;
   class Chararrayinput
   {
       public static void main(String[] args) 
       {
       String obj  = "abcdefgh";
           int length = obj.length();
           char c[] = new char[length];
           obj.getChars(0, length, c, 0);
           CharArrayReader input1 = new CharArrayReader(c);
           CharArrayReader input2 = new CharArrayReader(c, 1, 4);
           int i;
           int j;
           try 
           {
       while ((i = input1.read()) == (j = input2.read()))
               {
                   System.out.print((char)i);
               }
      	    } 
           catch (IOException e) 
           {
               e.printStackTrace();
       }
   }
   }

a) abc
b) abcd
c) abcde
d) None of the mentioned

1 thought on “What will be the output of the following Java program?”

  1. d
    Explanation: No output is printed. CharArrayReader object input1 contains string “abcdefgh” whereas object input2 contains string “bcde”, when while((i=input1.read())==(j=input2.read())) is executed the starting character of each object is compared since they are unequal control comes out of loop and nothing is printed on the screen.
    Output:
    $ javac Chararrayinput.java
    $ java Chararrayinput

Leave a Comment