Reading & Writing Files



Download 17.01 Kb.
Date09.01.2017
Size17.01 Kb.
#8173
Reading & Writing Files

This section of our 1000+ Java MCQs focuses 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
View Answer

2. Which of these exception is thrown in cases when the file specified for writing it not found?


a) IOException
b) FileException
c) FileNotFoundException
d) FileInputException
View Answer

Answer: c


Explanation: 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()
View Answer

Answer: b


Explanation: 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
View Answer

Answer: c


Explanation: 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
View Answer

Answer: a


Explanation: 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()
View Answer

Answer: c


Explanation: None.

7. What is the output of this program?



  1. import java.io.*;

  2. class filesinputoutput {

  3. public static void main(String args[]) {

  4. InputStream obj = new FileInputStream("inputoutput.java");

  5. System.out.print(obj.available());

  6. }

  7. }

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
View Answer

Answer: c


Explanation: obj.available() returns the number of bytes.
Output:
$ javac filesinputoutput.java
$ java filesinputoutput
1422
(Output will be different in your case)

8. What is the output of this program?



  1. import java.io.*;

  2. public class filesinputoutput {

  3. public static void main(String[] args) {

  4. String obj = "abc";

  5. byte b[] = obj.getBytes();

  6. ByteArrayInputStream obj1 = new ByteArrayInputStream(b);

  7. for (int i = 0; i < 2; ++ i) {

  8. int c;

  9. while((c = obj1.read()) != -1) {

  10. if(i == 0) {

  11. System.out.print(Character.toUpperCase((char)c));

  12. obj2.write(1);

  13. }

  14. }

  15. System.out.print(obj2);

  16. }

  17. }

  18. }

a) AaBaCa
b) ABCaaa
c) AaaBaaCaa
d) AaBaaCaaa
View Answer

Answer: d


Explanation: None.
Output:
$ javac filesinputoutput.java
$ java filesinputoutput
AaBaaCaaa

9. What is the output of this program?



  1. import java.io.*;

  2. class Chararrayinput {

  3. public static void main(String[] args) {

  4. String obj = "abcdef";

  5. int length = obj.length();

  6. char c[] = new char[length];

  7. obj.getChars(0, length, c, 0);

  8. CharArrayReader input1 = new CharArrayReader(c);

  9. CharArrayReader input2 = new CharArrayReader(c, 0, 3);

  10. int i;

  11. try {

  12. while((i = input2.read()) != -1) {

  13. System.out.print((char)i);

  14. }

  15. }

  16. catch (IOException e) {

  17. e.printStackTrace();

  18. }

  19. }

  20. }

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

Answer: a


Explanation: None.
Output:
$ javac Chararrayinput.java
$ java Chararrayinput
abc

10. What is the output of this program?



  1. import java.io.*;

  2. class Chararrayinput {

  3. public static void main(String[] args) {

  4. String obj = "abcdefgh";

  5. int length = obj.length();

  6. char c[] = new char[length];

  7. obj.getChars(0, length, c, 0);

  8. CharArrayReader input1 = new CharArrayReader(c);

  9. CharArrayReader input2 = new CharArrayReader(c, 1, 4);

  10. int i;

  11. int j;

  12. try {

  13. while((i = input1.read()) == (j = input2.read())) {

  14. System.out.print((char)i);

  15. }

  16. }

  17. catch (IOException e) {

  18. e.printStackTrace();

  19. }

  20. }

  21. }

a) abc
b) abcd
c) abcde
d) None of the mentioned
View Answer

Answer: d


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.
Output:
$ javac Chararrayinput.java
$ java Chararrayinput
Download 17.01 Kb.

Share with your friends:




The database is protected by copyright ©ininet.org 2024
send message

    Main page