Java Questions & Answers – Java.io Character Streams

This section of our 1000+ Java MCQs focuses on character streams of Java Programming Language.

1. Which of these stream contains the classes which can work on character stream?
a) InputStream
b) OutputStream
c) Character Stream
d) All of the mentioned
View Answer

Answer: c
Explanation: InputStream & OutputStream classes under byte stream they are not streams. Character Stream contains all the classes which can work with Unicode.

2. Which of these class is used to read characters in a file?
a) FileReader
b) FileWriter
c) FileInputStream
d) InputStreamReader
View Answer

Answer: a
Explanation: None.

3. Which of these method of FileReader class is used to read characters from a file?
a) read()
b) scanf()
c) get()
d) getInteger()
View Answer

Answer: a
Explanation: None.
advertisement
advertisement

4. Which of these class can be used to implement the input stream that uses a character array as the source?
a) BufferedReader
b) FileReader
c) CharArrayReader
d) FileArrayReader
View Answer

Answer: c
Explanation: CharArrayReader is an implementation of an input stream that uses character array as a source. Here array is the input source.

5. Which of these classes can return more than one character to be returned to input stream?
a) BufferedReader
b) Bufferedwriter
c) PushbachReader
d) CharArrayReader
View Answer

Answer: c
Explanation: 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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

6. What will be the output of the following Java program?

  1.     import java.io.*;
  2.     class Chararrayinput 
  3.     {
  4.         public static void main(String[] args) 
  5.         {
  6. 	    String obj  = "abcdef";
  7.             int length = obj.length();
  8.             char c[] = new char[length];
  9.             obj.getChars(0,length,c,0);
  10.             CharArrayReader input1 = new CharArrayReader(c);
  11.             CharArrayReader input2 = new CharArrayReader(c, 0, 3);
  12.             int i;
  13.             try 
  14.             {
  15. 		while ((i = input1.read()) != -1) 
  16.                 {
  17.                     System.out.print((char)i);
  18.                 }
  19.        	    } 
  20.             catch (IOException e) 
  21.             {
  22. 	        // TODO Auto-generated catch block
  23.                 e.printStackTrace();
  24. 	    }
  25. 	}
  26.     }

a) abc
b) abcd
c) abcde
d) abcdef
View Answer

Answer: d
Explanation: None.
Output:

advertisement
$ javac Chararrayinput.java
$ java Chararrayinput
abcdef

7. What will be the output of the following Java program?

advertisement
  1.     import java.io.*;
  2.     class Chararrayinput 
  3.     {
  4.         public static void main(String[] args) 
  5.         {
  6. 	    String obj  = "abcdef";
  7.             int length = obj.length();
  8.             char c[] = new char[length];
  9.             obj.getChars(0, length, c, 0);
  10.             CharArrayReader input1 = new CharArrayReader(c);
  11.             CharArrayReader input2 = new CharArrayReader(c, 0, 3);
  12.             int i;
  13.             try 
  14.             {
  15. 		while ((i = input2.read()) != -1) 
  16.                 {
  17.                     System.out.print((char)i);
  18.                 }
  19.        	    } 
  20.             catch (IOException e) 
  21.             {
  22. 	        // TODO Auto-generated catch block
  23.                 e.printStackTrace();
  24. 	    }
  25. 	}
  26.     }

a) abc
b) abcd
c) abcde
d) abcdef
View Answer

Answer: a
Explanation: None.
Output:

$ javac Chararrayinput.java
$ java Chararrayinput
abc

8. What will be the output of the following Java program?

  1.     import java.io.*;
  2.     class Chararrayinput 
  3.     {
  4.         public static void main(String[] args) 
  5.         {
  6. 	    String obj  = "abcdefgh";
  7.             int length = obj.length();
  8.             char c[] = new char[length];
  9.             obj.getChars(0, length, c, 0);
  10.             CharArrayReader input1 = new CharArrayReader(c);
  11.             CharArrayReader input2 = new CharArrayReader(c, 1, 4);
  12.             int i;
  13.             int j;
  14.             try 
  15.             {
  16. 		while ((i = input1.read()) == (j = input2.read())) 
  17.                 {
  18.                     System.out.print((char)i);
  19.                 }
  20.        	    } 
  21.             catch (IOException e) 
  22.             {
  23. 	        // TODO Auto-generated catch block
  24.                 e.printStackTrace();
  25. 	    }
  26. 	}
  27.     }

a) abc
b) abcd
c) abcde
d) none of the mentioned
View Answer

Answer: 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

Sanfoundry Global Education & Learning Series – Java Programming Language.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.