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

Search Interview Questions


 More than 3000 questions in repository.
 There are more than 900 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Core Java - Interview Questions and Answers for 'File' - 52 question(s) found - Order By Newest

next 30
 Q1. 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
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


 Q2. What is a stream and what are the types of Streams and classes of the Streams?Core Java
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     Asked in 3 Companies


 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
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. Which of the following file is called deployment descriptor ?

a. application.xml
b. project.xml
c. web.xml
d. build.xml
Java EE
Ans. web.xml

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

   Like         Discuss         Correct / Improve     configuration files   deployment   server   java


 Q5. Will this code compile fine ?

ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("newFile.txt")));
Core Java
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


 Q6. What are the configuration files in Hibernate ?Hibernate
Ans. hibernate.cfg.xml ( Main Configuration File )

and *.hbm.xml files ( Mapping Files )

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

   Like         Discuss         Correct / Improve     hibernate   configuration   mapping files


 Q7. Name few File IO related classes and interfaces ?Core Java
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


 Q8. Given two files with list of words, write a program to show the common words in both filesCore Java
Ans. import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.io.*;
public class Common {
public static void main(String ar[])throws Exception
   {
      File f=new File("a.txt");
File f1=new File("x.java");
      System.out.println(f.exists());
FileInputStream fin = new FileInputStream(f);
      FileInputStream fin1 = new FileInputStream(f1);

      byte b[]=new byte[10000];
byte b1[]=new byte[10000];
fin.read(b);
fin1.read(b1);
String s1 = new String(b);
String s2 =new String(b1);

String words1[] = s1.trim().split(" ");
String words2[] = s2.trim().split(" ");
Listlist1 = new ArrayList<>(Arrays.asList(words1));
Listlist2 = new ArrayList<>(Arrays.asList(words2));
list1.retainAll(list2);
System.out.println(list1);
}
}

 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     Asked in 1 Companies


 Q9. What are the ways in which an application can maintain config variables ?
Ans. 1. Property files
2. config xml
3. Database

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

   Like         Discuss         Correct / Improve     config  property files


 Q10. Write code to create a folder if it doesn't exist.Core Java
Ans. File folder = new File(path);
      
if(!folder.exists()){
try {
folder.mkdir();
            
} catch (Exception e) {}
}

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

   Like         Discuss         Correct / Improve     code  coding  file handling      basic


 Q11. Tell something about BufferedWriter ? What are flush() and close() used for ?
Ans. BufferedWriter is temporary source for data storage.BufferedWriter is used to write character data to the file.Flush() is method available in B.W which ensures that all data items are written to file including last character.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


 Q12. What is Scanner class used for ? when was it introduced in Java ?Core Java
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


 Q13. What is Spring configuration file?Spring
Ans. Spring configuration file is an XML file. This file contains the classes information and describes how these classes are configured and introduced to each other.

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

   Like         Discuss         Correct / Improve     j2ee   spring   mvc   frameworks   web applications   spring configuration file     Asked in 1 Companies


 Q14. What is the difference between the Reader/Writer class hierarchy and the InputStream/OutputStream class hierarchy?Core Java
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


 Q15. What is the purpose of the System class?Core Java
Ans. The purpose of the System class is to provide access to system resources.

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

   Like         Discuss         Correct / Improve     java   system   io   file io


 Q16. Which of the following is a canonical path ?

1. C:\directory\..\directory\file.txt
2. C:\directory\subDirectory1\directory\file.txt
3. \directory\file.txt
Ans. 2nd

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

   Like         Discuss         Correct / Improve     java   io   file   fileio


 Q17. 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
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


 Q18. 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
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


 Q19. 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
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


 Q20. Which is the abstract parent class of FileWriter ?Core Java
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


 Q21. Which class is used to read streams of characters from a file?Core Java
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


 Q22. Which class is used to read streams of raw bytes from a file?Core Java
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


 Q23. Which is the Parent class of FileInputStream ?Core Java
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


 Q24. 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
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


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

File file = new File("../file.txt");
FileWriter fileWriter = new FileWriter(file);
Core Java
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


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

FileOutputStream fileOutputStream = new FileOutputStream(new File("newFile.txt"));
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


 Q27. 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
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


 Q28. 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
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


 Q29. 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
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


 Q30. What is asynchronous I/O ?
Ans. It is a form of Input Output processing that permits other processing to continue before the I/O transmission has finished.

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

   Like         Discuss         Correct / Improve     java   io   file   fileio   java7   architecture


next 30

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: