Which of These Class Contains the Methods Used to Read Objects From a File?

This department of our one thousand+ Java MCQs focuses on reading & writing files in Java Programming Linguistic communication.

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

Reply: b
Caption: None.

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

Reply: c
Explanation: In cases when the file specified is not found, then FileNotFoundException is thrown past java run-time organisation, earlier versions of java used to throw IOException merely after Java 2.0 they throw FileNotFoundException.

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

Answer: b
Explanation: Each time read() is chosen, it reads a single byte from the file and returns the byte equally 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 cease of file (EOF) is encountered?
a) 0
b) 1
c) -1
d) Null
View Answer

Answer: c
Explanation: Each time read() is called, information technology reads a single byte from the file and returns the byte every bit an integer value. read() returns -one when the stop 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

Respond: 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

Reply: c
Explanation: None.

vii. What volition exist the output of the post-obit Coffee program?

  1.                       import                      java.io.*                      ;                    
  2.                       form                      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.                       }                    

Notation: 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.bachelor() returns the number of bytes.
Output:

$ javac filesinputoutput.java                    $ java filesinputoutput                    1422                  

(Output volition be unlike in your case)

viii. What will exist the output of the following Java program?

  1.                       import                      java.io.*                      ;                    
  2.                       public                      class                      filesinputoutput
  3.                       {                    
  4.                       public                      static                      void                      primary(                      Cord                      [                      ]                      args)                    
  5.                       {                    
  6.                       Cord                      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.                       Organisation.out.print                      (obj2)                      ;                    
  21.                       }                    
  22.                       }                    
  23.                       }                    

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

Reply: d
Explanation: None.
Output:

$ javac filesinputoutput.java                    $ coffee filesinputoutput AaBaaCaaa

ix. What volition be the output of the following Coffee program?

  1.                       import                      java.io.*                      ;                    
  2.                       grade                      Chararrayinput
  3.                       {                    
  4.                       public                      static                      void                      main(                      String                      [                      ]                      args)                    
  5.                       {                    
  6.                       Cord                      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.                       Organization.out.print                      (                      (                      char                      )i)                      ;                    
  18.                       }                    
  19.                       }                    
  20.                       take hold of                      (                      IOException                      due east)                    
  21.                       {                    
  22.                       e.printStackTrace                      (                      )                      ;                    
  23.                       }                    
  24.                       }                    
  25.                       }                    

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

Answer: a
Explanation: None.
Output:

$ javac Chararrayinput.java                    $ java Chararrayinput abc

10. What will exist the output of the post-obit Coffee plan?

  1.                       import                      coffee.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.                       endeavour                    
  15.                       {                    
  16.                       while                      (                      (i                      =                      input1.read                      (                      )                      )                      ==                      (j                      =                      input2.read                      (                      )                      )                      )                    
  17.                       {                    
  18.                       Organisation.out.impress                      (                      (                      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
View Answer

Answer: d
Caption: 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 grapheme of each object is compared since they are unequal control comes out of loop and nothing is printed on the screen.
Output:

$ javac Chararrayinput.coffee                    $ java Chararrayinput

Sanfoundry Global Education & Learning Series – Java Programming Linguistic communication.

Manish Bhojasia - Founder & CTO at Sanfoundry

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Information Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.

rofeonfunien1984.blogspot.com

Source: https://www.sanfoundry.com/java-mcqs-reading-writing-files/

0 Response to "Which of These Class Contains the Methods Used to Read Objects From a File?"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel