Java - Interview Questions and Answers for 'File handling' | Search Java Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com
Share

Search Java Interview Questions


 2137 questions in repository.
 There are more than 200 unanswered questions.
Click here and help us by providing the answer.
Label / Company      Label / Company / Text

   



Interview Questions and Answers for 'File handling' - 31 question(s) found - Order By Newest

 Q1. What is a stream and what are the types of Streams and classes of the Streams?Core Java
Admin
info@buggybread.com
Ans. A Stream is an abstraction that either produces or consumes information. There are two types of Streams :

Byte Streams: Provide a convenient means for handling input and output of bytes.

Character Streams: Provide a convenient means for handling input & output of characters.

Byte Streams classes: Are defined by using two abstract classes, namely InputStream and OutputStream.

Character Streams classes: Are defined by using two abstract classes, namely Reader and Writer.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   file io   streams   byte stream   character stream  file handling


 Q2. What will be the output of following code ?

public static void main(String[] args){
String name = null;
File file = new File("/folder", name);
System.out.print(file.exists());
}
Core Java
Admin
info@buggybread.com
Ans. NullPointerException

at line: "File file = new File("/folder", name);"

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   io   file   fileio   coding   code   file handling


 Q3. What will be the output of following code ?

public static void main(String[] args){
String parent = null;
File file = new File(parent, "myfile.txt");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
Core Java
Admin
info@buggybread.com
Ans. It will create the file myfile.txt in the current directory.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   io   file   fileio   coding   code   file handling


 Q4. Tell something about BufferedWriter ? What are flush() and close() used for ?
Admin
info@buggybread.com
Ans. A Buffer is a temporary storage area for data. The BufferedWriter class is an output stream.It is an abstract class that creates a buffered character-output stream.Flush() is used to clear all the data characters stored in the buffer and clear the buffer.Close() is used to closes the character output stream.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   java io   input output   file handling   file io   output stream   bufferedwriter   flush   close


 Q5. What is Scanner class used for ? when was it introduced in Java ?Core Java
Admin
info@buggybread.com
Ans. Scanner class introduced in Java 1.5 for reading Data Stream from the imput device. Previously we used to write code to read a input using DataInputStream. After reading the stream , we can convert into respective data type using in.next() as String ,in.nextInt() as integer, in.nextDouble() as Double etc

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   java5   scanner   file io   file input   data input stream  file handling      basic


 Q6. What is the difference between the Reader/Writer class hierarchy and the InputStream/OutputStream class hierarchy?Core Java
Admin
info@buggybread.com
Ans. The Reader/Writer class hierarchy is character-oriented, and the InputStream/OutputStream class hierarchy is byte-oriented

  Sample Code for InputStream

  Sample Code for OutputStream

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   file io   streams   reader class   writer class   inputstream   outputstream   stream  file handling


 Q7. What will the following code print when executed on Windows ?

public static void main(String[] args){
String parent = null;
File file = new File("/file.txt");
System.out.println(file.getPath());
System.out.println(file.getAbsolutePath());
try {
System.out.println(file.getCanonicalPath());
} catch (IOException e) {
e.printStackTrace();
}
}
Core Java
Admin
info@buggybread.com
Ans. file.txtC:file.txtC:file.txt

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   io   file   fileio   coding   code   file handling


 Q8. What will be the output of following code ?

public static void main(String[] args){
String child = null;
File file = new File("/folder", child);
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
Core Java
Admin
info@buggybread.com
Ans. NullPointerException at line:File file = new File("/folder", child);

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   io   file   fileio   coding   code   file handling


 Q9. What will be the output of following code, assuming that currently we are in c:Project ?

public static void main(String[] args){
String child = null;
File file = new File("../file.txt");
System.out.println(file.getPath());
System.out.println(file.getAbsolutePath());
try {
System.out.println(file.getCanonicalPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Core Java
Admin
info@buggybread.com
Ans. ..file.txt
C:WorkspaceProject..file.txt
C:Workspacefile.txt

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   io   file   fileio   coding   code   file handling



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q10. Which is the abstract parent class of FileWriter ?Core Java
Admin
info@buggybread.com
Ans. OutputStreamWriter

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   io   file   fileio   filewriter   outputstreamwriter  file handling


 Q11. Which class is used to read streams of characters from a file?Core Java
Admin
info@buggybread.com
Ans. FileReader

  Sample Code for FileReader

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   io   file   fileio   filereader  file handling


 Q12. Which class is used to read streams of raw bytes from a file?Core Java
Admin
info@buggybread.com
Ans. FileInputStream

  Sample Code for FileInputStream

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   io   file   fileio   fileinputstream  file handling


 Q13. Which is the Parent class of FileInputStream ?Core Java
Admin
info@buggybread.com
Ans. InputStream

  Sample Code for InputStream

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   io   file   fileio   fileinputstream   inputstream  file handling


 Q14. Which of the following code is correct ?

a.


FileWriter fileWriter = new FileWriter("../file.txt");
File file = new File(fileWriter );
BufferedWriter bufferedOutputWriter = new BufferedWriter(fileWriter);

b.

BufferedWriter bufferedOutputWriter = new BufferedWriter("../file.txt");
File file = new File(bufferedOutputWriter );
FileWriter fileWriter = new FileWriter(file);


c.

File file = new File("../file.txt");
FileWriter fileWriter = new FileWriter(file);
BufferedWriter bufferedOutputWriter = new BufferedWriter(fileWriter);

d.

File file = new File("../file.txt");
BufferedWriter bufferedOutputWriter = new BufferedWriter(file);
FileWriter fileWriter = new FileWriter(bufferedOutputWriter );
Core Java
Admin
info@buggybread.com
Ans. c.

File file = new File("../file.txt");
FileWriter fileWriter = new FileWriter(file);
BufferedWriter bufferedOutputWriter = new BufferedWriter(fileWriter);

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   io   file   fileio   coding   code   filewriter   bufferedwriter   scjp   ocjp  file handling


 Q15. Which exception should be handled in the following code ?

File file = new File("../file.txt");
FileWriter fileWriter = new FileWriter(file);
Core Java
Admin
info@buggybread.com
Ans. IOException

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   io   file   fileio   coding   code   exception   ioexception  file handling


 Q16. Which exceptions should be handled with the following code ?

FileOutputStream fileOutputStream = new FileOutputStream(new File("newFile.txt"));
Admin
info@buggybread.com
Ans. FileNotFoundException

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   io   file   fileio   coding   code   scjp   ocjp   filenotfoundexception   fileoutputstream  file handling


 Q17. Will this code compile fine ?

ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("newFile.txt")));
Core Java
Admin
info@buggybread.com
Ans. Yes.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   io   file   fileio   coding   code   objectoutputstream   fileoutputstream   yesno  file handling


 Q18. What is the problem with this code ?

class BuggyBread1 {

private BuggyBread2 buggybread2;

public static void main(String[] args){
try {
BuggyBread1 buggybread1 = new BuggyBread1();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("newFile.txt")));
objectOutputStream.writeObject(buggybread1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Core Java
Admin
info@buggybread.com
Ans. Though we are trying to serialize BuggyBread1 object but we haven't declared the class to implement Serializable.

This will throw java.io.NotSerializableException upon execution.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   io   file   fileio   coding   code   serialization   notserializableexception   exception   file handling


 Q19. Will this code run fine if BuggyBread2 doesn't implement Serializable interface ?

class BuggyBread1 implements Serializable{
private BuggyBread2 buggybread2 = new BuggyBread2();

public static void main(String[] args){
try {
BuggyBread1 buggybread1 = new BuggyBread1();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("newFile.txt")));
objectOutputStream.writeObject(buggybread1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Core Java
Admin
info@buggybread.com
Ans. No, It will throw java.io.NotSerializableException.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   io   file   fileio   coding   code   serialization   notserializableexception   exception   file handling



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q20. Will this code work fine if BuggyBread2 doesn't implement Serializable ?

class BuggyBread1 extends BuggyBread2 implements Serializable{
private int x = 5;

public static void main(String[] args){
try {
BuggyBread1 buggybread1 = new BuggyBread1();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("newFile.txt")));
objectOutputStream.writeObject(buggybread1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Core Java
Admin
info@buggybread.com
Ans. Yes.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   io   file   fileio   coding   code   serialization   yesno  file handling


 Q21. Name few File IO related classes and interfaces ?Core Java
Admin
info@buggybread.com
Ans. http://www.buggybread.com/2015/01/java-file-io-classes-and-interfaces.html

  Sample Code for File

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   io   input output   file io  file handling


 Q22. Difference between Scanner and BufferedReader ? Which one is faster and Why ?Core Java
Admin
info@buggybread.com
Ans. Scanner is used for parsing tokens from the contents of the stream while BufferedReader just reads the stream.

BufferedReader read files efficiently by using a buffer to avoid physical disk operations.

Buffer size of Scanner is usually smaller than the Buffered Writer.

BufferedReader is faster that Scanner as it just reads the Stream and doesn't Parse the tokens to read the stream into primitive data types.

  Sample Code for Scanner

  Sample Code for BufferedReader

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   file io   input output   scanner   bufferedreader  file handling


 Q23. How and when the Buffer is cleared when using Buffered Writer Classes ?Core Java2016-08-08 11:26:13

Ans. Buffer is cleared in 2 circumstances, i.e 1. naturally when the buffer is filled and 2. explicitly when the method flush is called ( for flushing the residual )

Ideally we just need to call flush once at the end of file writing so that the residual content should be dumped to file.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     BufferedWriter  Writer  PrintWriter  File io  flush  file handling


 Q24. Have you ever tried compressing data using Java ?Core Java2016-11-15 11:10:29

Ans. Yes, we can use ZipInputStream class for compressing the FileInputstream object.

  Sample Code for ZipInputStream

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     file handling  FileInputstream  ZipInputStream  file handling


 Q25. Watching a directory for changes. Monitoring a directory.Core Java2016-11-15 11:13:30

Ans. http://www.logicbig.com/tutorials/core-java-tutorial/java-nio/java-watch-service/

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     file handling  monitoring directory


 Q26. Given two files with list of words, write a program to show the common words in both filesCore Java2017-01-28 11:31:20

 This question was recently asked at 'Google'.This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     file handling  file io  code  coding     Google


 Q27. What is the difference between

File f = new File("homexyz.txt");

and

File f = File.createTempFile("xyz",".txt","home"); ?
Core Java2017-01-23 12:31:28

Ans. First will not create physical file in storage whereas the second will.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     file handling  File.createTempFile  file handling      intermediate        rare


 Q28. Why is it important to close files and streams ?Core Java2017-02-03 12:19:22

Ans. Optimizing Resource utilization. When you open a file using any programming language , that is actually a request to operating system to access the file. When such a request is made , resources are allocated by the OS. When you gracefully close those files, those resources are set free.

In Java if you don’t close the streams and files, Garbage collection identifies the open files which have lost the reference and hence will close them.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     file handling


 Q29. In a file there are 1 million words . Find 10 most frequent words in that file.Design2017-10-21 08:25:11

 This question was recently asked at 'Amazon'.This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     file handling     Amazon



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q30. Write a program that reads file using FileReader and BufferedReaderCore Java2016-08-18 10:18:03

Ans. File file = new File("/home/sample.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}

  Sample Code for import java.io.BufferedReader; import java.io.File; import java.io.FileReader;

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     File handling  FileReader  BufferedReader


 Q31. Write a Program to read a file and then count number of words2017-08-24 14:28:29

Ans. public class CountwordsinFile {
public static void main(String[] args) throws IOException{
String line = null;

File file = new File("C:Hello.txt");
FileReader fileReader = new FileReader(file);

BufferedReader bufferedReader = new BufferedReader(fileReader);

int countWords = 0;

do {

line = bufferedReader.readLine();
countWords += line.split(" ").length;

} while(line != null);
}
}

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     file handling  count number of words  code  coding  file handling




Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus
 

Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked:
         

X Close this

X Close this

Help Us Improve.
Please share your
interview experience.

Company Name:   


Questions Asked: