Java MCQs on reading & writing files in Java Programming Language.
1. Which of these class contains the methods used to write in a file? Answer: b 2. Which of these exception is thrown in cases when the file specified for writing is not found? Answer: c 3. Which of these methods are used to read in from file? Answer: b 4. Which of these values is returned by read() method is end of file (EOF) is encountered? Answer: c 5. Which of these exception is thrown by close() and read() methods? Answer: a 6. Which of these methods is used to write() into a file? Answer: c 7. What will be the output of the following Java program? Note: inputoutput.java is stored in the disk. (Output will be different in your case) 8. What will be the output of the following Java program? a) AaBaCa 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) abc
a) FileStream
b) FileInputStream
c) BUfferedOutputStream
d) FileBufferStream
Clarification: None.
a) IOException
b) FileException
c) FileNotFoundException
d) FileInputException
Clarification: In cases when the file specified is not found, then FileNotFoundException is thrown by java run-time system, earlier versions of java used to throw IOException but after Java 2.0 they throw FileNotFoundException.
a) get()
b) read()
c) scan()
d) readFileInput()
Clarification: Each time read() is called, it reads a single byte from the file and returns the byte as an integer value. read() returns -1 when the end of the file is encountered.
a) 0
b) 1
c) -1
d) Null
Clarification: Each time read() is called, it reads a single byte from the file and returns the byte as an integer value. read() returns -1 when the end of the file is encountered.
a) IOException
b) FileException
c) FileNotFoundException
d) FileInputOutputException
Clarification: Both close() and read() method throw IOException.
a) put()
b) putFile()
c) write()
d) writeFile()
Clarification: None.
import java.io.*;
class filesinputoutput
{
public static void main(String args[])
{
InputStream obj = new FileInputStream("inputoutput.java");
System.out.print(obj.available());
}
}
a) true
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(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
import java.io.*;
class Chararrayinput
{
public static void main(String[] args)
{
String obj = "abcdef";
int length = obj.length();
char c[] = new char[length];
obj.getChars(0, length, c, 0);
CharArrayReader input1 = new CharArrayReader(c);
CharArrayReader input2 = new CharArrayReader(c, 0, 3);
int i;
try
{
while((i = input2.read()) != -1)
{
System.out.print((char)i);
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
b) abcd
c) abcde
d) abcdef
Clarification: None.
Output:
$ javac Chararrayinput.java
$ java Chararrayinput
abc
import java.io.*;
class Chararrayinput
{
public static void main(String[] args)
{
String obj = "abcdefgh";
int length = obj.length();
char c[] = new char[length];
obj.getChars(0, length, c, 0);
CharArrayReader input1 = new CharArrayReader(c);
CharArrayReader input2 = new CharArrayReader(c, 1, 4);
int i;
int j;
try
{
while((i = input1.read()) == (j = input2.read()))
{
System.out.print((char)i);
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
b) abcd
c) abcde
d) none of the mentioned
Clarification: No output is printed. CharArrayReader object input1 contains string “abcdefgh” whereas object input2 contains string “bcde”, when while((i=input1.read())==(j=input2.read())) is executed the starting character of each object is compared since they are unequal control comes out of loop and nothing is printed on the screen.
Output:
$ javac Chararrayinput.java
$ java Chararrayinput
