Interview Questions and Answers - Order By Newest Q331. 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 Q332. 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 Q333. 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 Q334. 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 Q335. 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 Q336. 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 Q337. 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 Q338. 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 Q339. 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 Q340. 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 Q341. 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 Q342. 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 Q343. 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 Q344. 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 Q345. Which of the following syntax are correct ?a. LinkedList<Integer> l=new LinkedList<int>();b. List<Integer> l=new LinkedList<int>();c. LinkedList<Integer> l=new LinkedList<Integer>();d. List<Integer> l = new LinkedList<Integer>(); Core Java
Ans. All are correct. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   generics   linkedlist   list   collections basic Q346. Which methods of the Pattern class have equivalent methods in the String class? Core Java
Ans. split() and macthes() Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   io   string class   string   patternVery Frequently asked. Favorite question in walkins and telephonic interviews. Usually among first few questions. Asked in different variants. Must know for intermediate and expert professionals.Among Top 10 frequently asked questions. Q347. What is rule regarding overriding equals and hashCode method ? Core Java
Ans. A Class must override the hashCode method if its overriding the equals method. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   collections   hashcode  hash code   equals   collections Asked in 44 Companies intermediate   frequent Try 1 Question(s) Test Q348. What is the difference between Collection and Collections ? Core Java
Ans. Collection is an interface whereas Collections is a utility class. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   collections Asked in 1 Companies Q349. How can we reverse the order in the TreeMap ?
Ans. Using Collections.reverseOrder() Map tree = new TreeMap(Collections.reverseOrder()); Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   collections   map   treemap Try 1 Question(s) Test Q350. TreeMap orders the elements on which field ?
Ans. Keys Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   collections   map   treemap Q351. How TreeMap orders the elements if the Key is a String ?
Ans. As String implements Comparable, It refers to the String compareTo method to identify the order relationship among those elements. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   collections   comparable interface   treemap   compareto Q352. Can we add heterogeneous elements into TreeMap ?
Ans. No, Sorted collections don't allow addition of heterogeneous elements as they are not comparable. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   collections   comparable interface   treemap Q353. Will it create any problem if We add elements with key as user defined object into the TreeMap ?
Ans. It won't create any problem if the objects are comparable i.e we have that class implementing Comparable interface. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   collections   treemap   comparable interface Q354. Can we have null keys in TreeMap ? Core Java
Ans. No, results in exception. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   collections   treemap Asked in 6 Companies Basic   frequent Q355. Can value be null in TreeMap ? Core Java
Ans. Yes. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   collections   treemap   yes-no Q356. Which interface TreeMap implements ?
Ans. TreeMap implements NavigableMap, SortedMap, Serializable and Clonable. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   collections   treemapFrequently asked in Phone Interviews. Q357. What are the various Auto Wiring types in Spring ?
Ans. By Name , By Type and Constructor. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   spring   spring container   dependency injection   auto wiring   auto wiring types   general electric   ge Q358. Is It Good to use Reflection in an application ? Why ? Core Java
Ans. no, It's like challenging the design of application. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   reflection api   yes-no   architecture Asked in 1 Companies intermediate Q359. Why is Reflection slower ?
Ans. Because it has to inspect the metadata in the bytecode instead of just using precompiled addresses and constants. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   reflection   reflection api   architecture Q360. Difference between Assert and Verify ? Testing
Ans. Assert works only if assertions ( -ea ) are enabled which is not required for Verify.Assert throws an exception and hence doesn't continue with the test if assert evaluates to false whereas it's not so with Verify. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  assert   junit   mockito   verify   testing   unit testing Asked in 5 Companies