Java Questions and Answers - Java.io Byte Streams

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

  Discussion

Answer: b
Explanation: flush()

2. 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()

  Discussion

Answer: b
Explanation: print() and write()

3. What will be the output of the following Java 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

  Discussion

Answer: a
Explanation:
Output:
abc

4. What will be the output of the following Java 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

  Discussion

Answer: b
Explanation:
Output:
ABC

5. What will be the output of the following Java 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

  Discussion

Answer: d
Explanation:
Output:
AaBaaCaaa

6. Which of the following is not a segment of memory in java?
a) Stack Segment
b) Heap Segment
c) Code Segment
d) Register Segment

  Discussion

Answer: d
Explanation: There are only 3 types of memory segment. Stack Segment, Heap Segment and Code Segment.

7. Does code Segment loads the java code?
a) true
b) false

  Discussion

Answer: a
Explanation: Code Segment loads compiled java bytecode. Bytecode is platform independent.

8. What is JVM?
a) Bootstrap
b) Interpreter
c) Extension
d) Compiler

  Discussion

Answer: b
Explanation: JVM is Interpreter. It reads .class files which is the byte code generated by compiler line by line and converts it into native OS code.

9. Which one of the following is a class loader?
a) Bootstrap
b) Compiler
c) Heap
d) Interpreter

  Discussion

Answer: a
Explanation: Bootstrap is a class loader. It loads the classes into memory.

10. Which class loader loads jar files from JDK directory?
a) Bootstrap
b) Extension
c) System
d) Heap

  Discussion

Answer: b
Explanation: Extension loads jar files from lib/ext directory of the JRE. This gives the basic functionality available.