250+ TOP MCQs on Reading & Writing Files and Answers

Java MCQs on reading & writing files in Java Programming Language.

1. Which of these class contains the methods used to write in a file?
a) FileStream
b) FileInputStream
c) BUfferedOutputStream
d) FileBufferStream

Answer: b
Clarification: None.

2. Which of these exception is thrown in cases when the file specified for writing is not found?
a) IOException
b) FileException
c) FileNotFoundException
d) FileInputException

Answer: c
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.

3. Which of these methods are used to read in from file?
a) get()
b) read()
c) scan()
d) readFileInput()

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

4. Which of these values is returned by read() method is end of file (EOF) is encountered?
a) 0
b) 1
c) -1
d) Null

Answer: c
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.

5. Which of these exception is thrown by close() and read() methods?
a) IOException
b) FileException
c) FileNotFoundException
d) FileInputOutputException

Answer: a
Clarification: Both close() and read() method throw IOException.

6. Which of these methods is used to write() into a file?
a) put()
b) putFile()
c) write()
d) writeFile()

  250+ TOP MCQs on Exceptions Types and Answers

Answer: c
Clarification: None.

7. What will be the output of the following Java program?

  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.     }

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

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(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

9. What will be the output of the following Java program?

  1.     import java.io.*;
  2.     class Chararrayinput
  3.     {
  4.         public static void main(String[] args)
  5.         {
  6. 	    String obj  = "abcdef";
  7.             int length = obj.length();
  8.             char c[] = new char[length];
  9.             obj.getChars(0, length, c, 0);
  10.             CharArrayReader input1 = new CharArrayReader(c);
  11.             CharArrayReader input2 = new CharArrayReader(c, 0, 3);
  12.             int i;
  13.             try
  14.             {
  15. 		while((i = input2.read()) != -1)
  16.                 {
  17.                     System.out.print((char)i);
  18.                 }
  19.        	    } 
  20.             catch (IOException e)
  21.             {
  22.                 e.printStackTrace();
  23. 	    }
  24. 	}
  25.     }

a) abc
b) abcd
c) abcde
d) abcdef

Answer: a
Clarification: None.
Output:

$ javac Chararrayinput.java
$ java Chararrayinput
abc

10. What will be the output of the following Java program?

  1.     import java.io.*;
  2.     class Chararrayinput
  3.     {
  4.         public static void main(String[] args)
  5.         {
  6. 	    String obj  = "abcdefgh";
  7.             int length = obj.length();
  8.             char c[] = new char[length];
  9.             obj.getChars(0, length, c, 0);
  10.             CharArrayReader input1 = new CharArrayReader(c);
  11.             CharArrayReader input2 = new CharArrayReader(c, 1, 4);
  12.             int i;
  13.             int j;
  14.             try 
  15.             {
  16. 		while((i = input1.read()) == (j = input2.read()))
  17.                 {
  18.                     System.out.print((char)i);
  19.                 }
  20.        	    } 
  21.             catch (IOException e)
  22.             {
  23.                 e.printStackTrace();
  24. 	    }
  25. 	}
  26.     }

a) abc
b) abcd
c) abcde
d) none of the mentioned

Answer: d
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

Leave a Comment

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

Scroll to Top