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?
-
import java.io.* ;
-
form filesinputoutput
-
{
-
public static void main( String args[ ] )
-
{
-
InputStream obj = new FileInputStream ( "inputoutput.java" ) ;
-
System.out.print (obj.available ( ) ) ;
-
}
-
}
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?
-
import java.io.* ;
-
public class filesinputoutput
-
{
-
public static void primary( Cord [ ] args)
-
{
-
Cord 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 ) ;
-
}
-
}
-
Organisation.out.print (obj2) ;
-
}
-
}
-
}
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?
-
import java.io.* ;
-
grade Chararrayinput
-
{
-
public static void main( String [ ] args)
-
{
-
Cord 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 )
-
{
-
Organization.out.print ( ( char )i) ;
-
}
-
}
-
take hold of ( IOException due east)
-
{
-
e.printStackTrace ( ) ;
-
}
-
}
-
}
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?
-
import coffee.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;
-
endeavour
-
{
-
while ( (i = input1.read ( ) ) == (j = input2.read ( ) ) )
-
{
-
Organisation.out.impress ( ( char )i) ;
-
}
-
}
-
catch ( IOException e)
-
{
-
e.printStackTrace ( ) ;
-
}
-
}
-
}
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.
Next Steps:
- Go Gratis Certificate of Merit in Coffee Programming
- Participate in Java Programming Certification Contest
- Become a Pinnacle Ranker in Java Programming
- Accept Java Programming Tests
- Chapterwise Practice Tests: Affiliate i, two, three, 4, 5, 6, vii, 8, nine, x
- Chapterwise Mock Tests: Chapter ane, ii, iii, four, 5, 6, seven, 8, ix, x
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.
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