250+ TOP MCQs on Java.io Byte Streams and Answers

Java MCQs 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

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

Answer: c
Clarification: 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()

Answer: a
Clarification: None.

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

Answer: d
Clarification: 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()

Answer: b
Clarification: None.

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

Answer: b
Clarification: write() and print() are the two methods of OutputStream that are used for printing the byte data.

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

  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

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

$ 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

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

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

Answer: d
Clarification: None.
Output:

$ javac filesinputoutput.java
$ java filesinputoutput
AaBaaCaaa

Leave a Reply

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