Java Multiple Choice Questions & Answers on Java.io Byte Streams for Freshers

https://www.computersprofessor.com/2017/12/java-multiple-choice-questions-answers_16.html
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
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
Answer: c
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
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
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()
Answer: b
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
Explanation: write() and print() are the two methods of OutputStream that are used for printing the byte data.
7. What is the output of this program?
import java.io.*;
class filesinputoutput {
public static void main(String args[]) {
InputStream obj = new FileInputStream("inputoutput.java");
System.out.print(obj.available());
}
}
Note: inputoutput.java is stored in the disk.
a) true
b) false
c) prints number of bytes in file
d) prints number of characters in the file
a) true
b) false
c) prints number of bytes in file
d) prints number of characters in the file
Answer: c
Explanation: obj.available() returns the number of bytes.
8. What is the output of this program?
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);
}
}
}
}
}
a) abc
b) ABC
c) ab
d) AB
b) ABC
c) ab
d) AB
Answer: a
9. What is the output of this program?
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));
}
}
}
}
}
a) abc
b) ABC
c) ab
d) AB
b) ABC
c) ab
d) AB
Answer: b
10. What is the output of this program?
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);
}
}
}
a) AaBaCa
b) ABCaaa
c) AaaBaaCaa
d) AaBaaCaaa
b) ABCaaa
c) AaaBaaCaa
d) AaBaaCaaa
Answer: d