Java MCQs on character streams of Java Programming Language.
1. Which of these stream contains the classes which can work on character stream? Answer: c 2. Which of these class is used to read characters in a file? Answer: a 3. Which of these method of FileReader class is used to read characters from a file? Answer: a 4. Which of these class can be used to implement the input stream that uses a character array as the source? Answer: c 5. Which of these classes can return more than one character to be returned to input stream? Answer: c 6. What will be the output of the following Java program? a) abc 7. What will be the output of the following Java program? a) abc 8. What will be the output of the following Java program? a) abc
a) InputStream
b) OutputStream
c) Character Stream
d) All of the mentioned
Clarification: InputStream & OutputStream classes under byte stream they are not streams. Character Stream contains all the classes which can work with Unicode.
a) FileReader
b) FileWriter
c) FileInputStream
d) InputStreamReader
Clarification: None.
a) read()
b) scanf()
c) get()
d) getInteger()
Clarification: None.
a) BufferedReader
b) FileReader
c) CharArrayReader
d) FileArrayReader
Clarification: CharArrayReader is an implementation of an input stream that uses character array as a source. Here array is the input source.
a) BufferedReader
b) Bufferedwriter
c) PushbachReader
d) CharArrayReader
Clarification: PushbackReader class allows one or more characters to be returned to the input stream. This allows looking ahead in input stream and performing action accordingly.
import java.io.*;
class Chararrayinput
{
public static void main(String[] args)
{
String obj = "abcdef";
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, 0, 3);
int i;
try
{
while ((i = input1.read()) != -1)
{
System.out.print((char)i);
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
b) abcd
c) abcde
d) abcdef
Clarification: None.
Output:
$ javac Chararrayinput.java
$ java Chararrayinput
abcdef
import java.io.*;
class Chararrayinput
{
public static void main(String[] args)
{
String obj = "abcdef";
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, 0, 3);
int i;
try
{
while ((i = input2.read()) != -1)
{
System.out.print((char)i);
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
b) abcd
c) abcde
d) abcdef
Clarification: None.
Output:
$ javac Chararrayinput.java
$ java Chararrayinput
abc
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();
}
}
}
b) abcd
c) abcde
d) none of the mentioned
Clarification: 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
