Interview Questions and Answers - Order By Newest Q541. What is the use of HTTPSession in relation to http protocol ? Java EE
Ans. http protocol on its own is stateless. So it helps in identifying the relationship between multiple stateless request as they come from a single source. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  j2ee   servlets   web application   session   httpsession   architecture Asked in 1 Companies Q542. Why using cookie to store session info is a better idea than just using session info in the request ? Java EE
Ans. Session info in the request can be intercepted and hence a vulnerability. Cookie can be read and write by respective domain only and make sure that right session information is being passed by the client. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  j2ee   servlets   session   session management   web applications   cookies   httpsession   architecture Q543. http protocol is by default ... ? Java EE
Ans. stateless Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  j2ee   servlets   session   session management   web applications   cookies   httpsession   architecture Q544. Can finally block throw an exception ? Core Java
Ans. Yes. Methods invoked from within a finally block can throw an exception. Failure to catch and handle such exceptions results in the abrupt termination of the entire try block. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   exceptions   exception handling   finally   yes-no Q545. 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 Q546. 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 Q547. 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 Q548. 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 Q549. 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 Q550. 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 Q551. 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 Q552. 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 Q553. 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 Q554. 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 Q555. 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 Q556. 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 Q557. 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 Q558. 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 Q559. Difference between Class Path and Build Path ? Core Java
Ans. The build path is used for building your application. It contains all of your source files and all Java libraries that are required to compile the application.
The classpath is used for executing the application. This includes all java classes and libraries that are needed to run the java application. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve   Asked in 1 Companies Q560. Will this code Work ? If not , Why ? java.util.Calendar c = new java.util.Calendar(); Core Java
Ans. No. It gives the error "Cannot Instantiate the type Calendar". Calendar is an abstract class and hence Calendar object should be instantiated using Calendar.getInstance(). Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   date   calendar   abstract class   yes-no Q561. Is java.util.Date an abstract Class ? Is java.util.Calendar an abstract Class ? Core Java
Ans. Date is not a abstract class whereas Calendar is. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   date   calendar   abstract class Q562. What will the following code print ? java.util.Calendar c = java.util.Calendar.getInstance(); c.add(Calendar.MONTH, 5); System.out.println(c.getTime()); Core Java
Ans. Date and Time after 5 months from now. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   date   calendar   scjp   ocjp   coding   code Q563. Which of the following code is correct ? a. DateFormat df = DateFormat.getInstance(); b. DateFormat df = DateFormat.getDateInstance(); c. DateFormat df = DateFormat.getInstance(DateFormat.FULL); d. DateFormat df = DateFormat.getDateInstance(DateFormat.FULL); Core Java
Ans. All except c are correct. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   date   dateformat Q564. What is the use of parse method in DateFormat ?
Ans. It is used to parse String to get the Date Object with initialized date. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   date   dateformat Q565. Which of the following is not a valid java.util.Locale initialization ? a. new Locale () b. new Locale ( String language ) c. new Locale ( String language , String country ) Core Java
Ans. a i.e new Locale() Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   locale   java.util Q566. Which of the following is not a valid NumberFormat initialization ? a. NumberFormat.getInstance() b. NumberFormat.getDateInstance() c. NumberFormat.getCurrencyInstance() d. NumberFormat.getNumberInstance() Core Java
Ans. b i.e NumberFormat.getDateInstance() Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   java.util.numberformat Q567. What will the following code print ? public static void main(String[] args){ Integer i1 = new Integer("1"); Integer i2 = new Integer("2"); Integer i3 = Integer.valueOf("3"); int i4 = i1 + i2 + i3; System.out.println(i4); } Core Java
Ans. 6 Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   int   integer   scjp   ocjp Q568. 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 Q569. Which of the following code is correct ? a. Date date = DateFormat.newInstance(DateFormat.LONG, Locale.US).parse(str); b. Date date = DateFormat.newInstance(DateFormat.LONG, Locale.US).format(str); c. Date date = DateFormat.getDateInstance(DateFormat.LONG, Locale.US).parse(str); Core Java
Ans. c Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   java.util   date   dateformat Q570. 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   pattern