Java File Input and Output Questions and Answers

Related Topics:

Java Programming : Java File Input Output

Here are the Java File Input and Output Interview Questions which can be helpful for cracking the Java interview in various companies.
1. Which of these stream contains the classes which can work on character stream?
[A] InputStream
[B] OutputStream
[C] Character Stream
[D] All of the mentioned

Answer: C. Character Stream

Explanation:

InputStream & OutputStream classes under byte stream they are not streams. Character Stream contains all the classes which can work with Unicode
2. Which of these class is used to read characters in a file?
[A] FileReader
[B] FileWriter
[C] FileInputStream
[D] InputStreamReader

Answer: A. FileReader

Explanation:

We can read characters in a file using FileReader class of Java.
3. Which of these method of FileReader class is used to read characters from a file?
[A] read()
[B] scanf()
[C] get()
[D] getInteger()

Answer: A. read()

4. Which of these class can be used to implement input stream that uses a character array as the source?
[A] BufferedReader
[B] FileReader
[C] CharArrayReader
[D] FileArrayReader

Answer: C. CharArrayReader

Explanation:

CharArrayReader is an implementation of an input stream that uses character array as a source. Here array is the input source.
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. flush()

Explanation:

none
6. Which of these classes can return more than one character to be returned to input stream?
[A] BufferedReader
[B] Bufferedwriter
[C] PushbachReader
[D] CharArrayReader

Answer: C. PushbachReader

Explanation:

PushbackReader class allows one or more characters to be returned to the input stream. This allows looking ahead in input stream and performing action accordingly.
7. 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

Answer: C. prints number of bytes in file

Explanation:

obj.available() returns the number of bytes.
8. What is the output of this program?

    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 = input1.read()) != -1) {
                    System.out.print((char)i);
                }
       	    } 
            catch (IOException e) {
	        // TODO Auto-generated catch block
                e.printStackTrace();
	    }
	}
    }
[A] abc
[B] abcd
[C] abcde
[D] abcdef

Answer: D. abcdef

9. What is the output of this program?
  
  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) {
	        // TODO Auto-generated catch block
                e.printStackTrace();
	    }
	}
    }
[A] abc
[B] abcd
[C] abcde
[D] abcdef

Answer: A. abc

10. What is the output of this program?
 
    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) {
	        // TODO Auto-generated catch block
                e.printStackTrace();
	    }
	}
    }
[A] abc
[B] abcd
[C] abcde
[D] None of the mentioned

Answer: D. None of the mentioned

Explanation:

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.

Comments: (Your feedback is valuable to us)
;