Sail E0 Webinar
Question


What is the output of this 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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Options:
A .  abc
B .  abcd
C .  abcde
D .  None of the mentioned
Answer: Option D

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.



Was this answer helpful ?
Next Question

Submit Solution

Your email address will not be published. Required fields are marked *

Latest Videos

Latest Test Papers