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? Answer: a 2. Which of these class is used to read and write bytes in a file? Answer: c 3. Which of these method of InputStream is used to read integer representation of next available byte input? Answer: a 4. Which of these data type is returned by every method of OutputStream? Answer: d 5. Which of these is a method to clear all the data present in output buffers? Answer: b 6. Which of these method(s) is/are used for writing bytes to an outputstream? Answer: b 7. What will be the output of the following Java program? (Note: inputoutput.java is stored in the disk.) a) true (Output will be different in your case) 8. What will be the output of the following Java program? a) abc 9. What will be the output of the following Java program? a) abc 10. What will be the output of the following Java program? a) AaBaCa
a) InputStream
b) Reader
c) Writer
d) All of the mentioned
Clarification: InputStream & OutputStream are designed for byte stream. Reader and writer are designed for character stream.
a) FileReader
b) FileWriter
c) FileInputStream
d) InputStreamReader
Clarification: None.
a) read()
b) scanf()
c) get()
d) getInteger()
Clarification: None.
a) int
b) float
c) byte
d) none of the mentioned
Clarification: Every method of OutputStream returns void and throws an IOExeption in case of errors.
a) clear()
b) flush()
c) fflush()
d) close()
Clarification: None.
a) put()
b) print() and write()
c) printf()
d) write() and read()
Clarification: write() and print() are the two methods of OutputStream that are used for printing the byte data.
import java.io.*;
class filesinputoutput
{
public static void main(String args[])
{
InputStream obj = new FileInputStream("inputoutput.java");
System.out.print(obj.available());
}
}
b) false
c) prints number of bytes in file
d) prints number of characters in the file
Clarification: obj.available() returns the number of bytes.
Output:
$ javac filesinputoutput.java
$ java filesinputoutput
1422
import java.io.*;
public class filesinputoutput
{
public static void main(String[] args)
{
String obj = "abc";
byte b[] = obj.getBytes();
ByteArrayInputStream obj1 = new ByteArrayInputStream(b);
for (int i = 0; i < 2; ++ i)
{
int c;
while ((c = obj1.read()) != -1)
{
if(i == 0)
{
System.out.print((char)c);
}
}
}
}
}
b) ABC
c) ab
d) AB
Clarification: None.
Output:
$ javac filesinputoutput.java
$ java filesinputoutput
abc
import java.io.*;
public class filesinputoutput
{
public static void main(String[] args)
{
String obj = "abc";
byte b[] = obj.getBytes();
ByteArrayInputStream obj1 = new ByteArrayInputStream(b);
for (int i = 0; i < 2; ++ i)
{
int c;
while ((c = obj1.read()) != -1)
{
if (i == 0)
{
System.out.print(Character.toUpperCase((char)c));
}
}
}
}
}
b) ABC
c) ab
d) AB
Clarification: None.
Output:
$ javac filesinputoutput.java
$ java filesinputoutput
ABC
import java.io.*;
public class filesinputoutput
{
public static void main(String[] args)
{
String obj = "abc";
byte b[] = obj.getBytes();
ByteArrayInputStream obj1 = new ByteArrayInputStream(b);
for (int i = 0; i < 2; ++ i)
{
int c;
while ((c = obj1.read()) != -1)
{
if (i == 0)
{
System.out.print(Character.toUpperCase((char)c));
obj2.write(1);
}
}
System.out.print(obj2);
}
}
}
b) ABCaaa
c) AaaBaaCaa
d) AaBaaCaaa
Clarification: None.
Output:
$ javac filesinputoutput.java
$ java filesinputoutput
AaBaaCaaa