Java Questions & Answers – Java.io Byte Streams

This section of our 1000+ Java MCQs focuses on java.io byte streams of Java Programming Language.

1. Which of these classes is used for input and output operation when working with bytes?
a) InputStream
b) Reader
c) Writer
d) All of the mentioned
View Answer

Answer: a
Explanation: InputStream & OutputStream are designed for byte stream. Reader and writer are designed for character stream.

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

Answer: c
Explanation: None.

3. Which of these method of InputStream is used to read integer representation of next available byte input?
a) read()
b) scanf()
c) get()
d) getInteger()
View Answer

Answer: a
Explanation: None.
advertisement
advertisement

4. Which of these data type is returned by every method of OutputStream?
a) int
b) float
c) byte
d) none of the mentioned
View Answer

Answer: d
Explanation: Every method of OutputStream returns void and throws an IOExeption in case of errors.

5. Which of these is a method to clear all the data present in output buffers?
a) clear()
b) flush()
c) fflush()
d) close()
View Answer

Answer: b
Explanation: None.
Note: Join free Sanfoundry classes at Telegram or Youtube

6. Which of these method(s) is/are used for writing bytes to an outputstream?
a) put()
b) write()
c) printf()
d) write() and read()
View Answer

Answer: b
Explanation: write() method is specifically designed for writing bytes to an OutputStream where as printf() method is primarily used for formatted data to a stream. put() is not there.

7. What will be the output of the following Java program? (Note: inputoutput.java is stored in the disk.)

advertisement
  1.     import java.io.*;
  2.     class filesinputoutput 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             InputStream obj = new FileInputStream("inputoutput.java");
  7.             System.out.print(obj.available());
  8.         }
  9.     }

a) true
b) false
c) prints number of bytes in file
d) prints number of characters in the file
View Answer

Answer: c
Explanation: obj.available() returns the number of bytes.
Output:

advertisement
$ javac filesinputoutput.java
$ java filesinputoutput
1422

(Output will be different in your case)

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

  1.     import java.io.*;
  2.     public class filesinputoutput 
  3.     {
  4.     	public static void main(String[] args) 
  5.         {
  6.  	   String obj  = "abc";
  7.            byte b[] = obj.getBytes();
  8.            ByteArrayInputStream obj1 = new ByteArrayInputStream(b);
  9.            for (int i = 0; i < 2; ++ i) 
  10.            {
  11.                int c;
  12.                while ((c = obj1.read()) != -1) 
  13.                {
  14.             	   if(i == 0) 
  15.                    {
  16.             	       System.out.print((char)c); 
  17.             	   }
  18.                }
  19.            }
  20.         }
  21.     }

a) abc
b) ABC
c) ab
d) AB
View Answer

Answer: a
Explanation: None.
Output:

$ javac filesinputoutput.java
$ java filesinputoutput
abc

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

  1.     import java.io.*;
  2.     public class filesinputoutput 
  3.     {
  4.     	public static void main(String[] args) 
  5.         {
  6.  	   String obj  = "abc";
  7.            byte b[] = obj.getBytes();
  8.            ByteArrayInputStream obj1 = new ByteArrayInputStream(b);
  9.            for (int i = 0; i < 2; ++ i) 
  10.            {
  11.                int c;
  12.                while ((c = obj1.read()) != -1) 
  13.                {
  14.             	   if (i == 0) 
  15.                    {
  16.                        System.out.print(Character.toUpperCase((char)c)); 
  17.             	   }
  18.                }
  19.            }
  20.         }
  21.     }

a) abc
b) ABC
c) ab
d) AB
View Answer

Answer: b
Explanation: None.
Output:

$ javac filesinputoutput.java
$ java filesinputoutput
ABC

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

  1.     import java.io.*;
  2.     public class filesinputoutput 
  3.     {
  4.     	public static void main(String[] args) 
  5.         {
  6.  	   String obj  = "abc";
  7.            byte b[] = obj.getBytes();
  8.            ByteArrayInputStream obj1 = new ByteArrayInputStream(b);
  9.            for (int i = 0; i < 2; ++ i) 
  10.            {
  11.                int c;
  12.                while ((c = obj1.read()) != -1) 
  13.                {
  14.             	   if (i == 0) 
  15.                    {
  16.                        System.out.print(Character.toUpperCase((char)c));
  17.                        obj2.write(1); 
  18.             	   }
  19.                }
  20.                System.out.print(obj2);
  21.            }
  22.         }
  23.     }

a) AaBaCa
b) ABCaaa
c) AaaBaaCaa
d) AaBaaCaaa
View Answer

Answer: d
Explanation: None.
Output:

$ javac filesinputoutput.java
$ java filesinputoutput
AaBaaCaaa

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.