Interview Questions and Answers - Order By Rating Advanced level question frequently asked in US based companies. Recently asked in EMC and Intuit. Q1301. Can you provide some implementation of a Dictionary having large number of words ? Solution
Ans. Simplest implementation we can have is a List wherein we can place ordered words and hence can perform Binary Search.
Other implementation with better search performance is to use HashMap with key as first character of the word and value as a LinkedList.
Further level up, we can have linked Hashmaps like ,
hashmap {
a ( key ) -> hashmap (key-aa , value (hashmap(key-aaa,value)
b ( key ) -> hashmap (key-ba , value (hashmap(key-baa,value)
....................................................................................
z( key ) -> hashmap (key-za , value (hashmap(key-zaa,value)
}
upto n levels ( where n is the average size of the word in dictionary. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   collections   hashmap   binary search   search algorithm   advanced   architecture   data structure Asked in 6 Companies   frequent Try 1 Question(s) TestAns. PATH is the variable that holds the directories for the OS to look for executables. CLASSPATH is the variable that holds the directories for JVM to look for .class files ( Byte Code ). Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   path   classpath   byte code   jvm   basic interview question Asked in 3 Companies intermediate   rare Q1303. Name few access and non access Class Modifiers ?
Ans. private , public and protected are access modifiers. final and abstract are non access modifiers. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   access specifiers   access modifiers   modifiers   access control Q1304. Which Java collection class can be used to maintain the entries in the order in which they were last accessed?
Ans. LinkedHashMap Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   collections   hashmap   linkedhashmap Q1305. Is it legal to initialize List like this ? LinkedList l=new LinkedList();
Ans. No, Generic parameters cannot be primitives. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   collections   list   linkedlist   generics   yes-no Q1306. Which of the following syntax is correct ?import static java.lang.System.*;or static import java.lang.System.*; Core Java
Ans. import static java.lang.System.*; Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   static import   generics   import   OCJP   SCJP   Oracle certified java developer Asked in 1 Companies Q1307. What will be the output of following code ? public static void main(String[] args) { int x = 10; int y; if (x < 100) y = x / 0; if (x >= 100) y = x * 0; System.out.println("The value of y is: " + y); }
Ans. The code will not compile raising an error that the local variable y might not have been initialized. Unlike member variables, local variables are not automatically initialized to the default values for their declared type. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   code   default value Q1308. What will be the output of following Code ?
class BuggyBread {
public static void main(String[] args) {
String s2 = "I am unique!";
String s5 = "I am unique!";
System.out.println(s2 == s5);
}
} Core Java
Ans. true, due to String Pool, both will point to a same String object. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   code   coding   tricky questions   interesting questions   string   string pool   .equal   == intermediate   frequent Q1309. What will be the output of following code ?
class BuggyBread2 {
private static int counter = 0;
void BuggyBread2() {
counter = 5;
}
BuggyBread2(int x){
counter = x;
}
public static void main(String[] args) {
BuggyBread2 bg = new BuggyBread2();
System.out.println(counter);
}
} Core Java
Ans. Compile time error as it won't find the constructor matching BuggyBread2().
Compiler won't provide default no argument constructor as programmer has already defined one constructor.
Compiler will treat user defined BuggyBread2() as a method, as return type ( void ) has been specified for that. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   code   coding   tricky questions   interesting questions   default constructor   constructor Try 3 Question(s) TestVery frequently asked in companies using SOA. Q1310. What are RESTful Web Services ? Rest
Ans. REST or Representational State Transfer is a flexible architecture style for creating web services that recommends the following guidelines -
1. http for client server communication,
2. XML / JSON as formatiing language ,
3. Simple URI as address for the services and,
4. stateless communication. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   web services   rest   java   j2ee  architecture Asked in 14 Companies intermediate   frequent Q1311. Which markup languages can be used in restful web services ? Rest
Ans. XML and JSON ( Javascript Object Notation ). Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   web services   rest   java   j2ee   xml   json   architecture basic   frequent Frequently asked to experienced developers. Recently asked in many US interviews. Q1312. What is database deadlock ? How can we avoid them? Database
Ans. When multiple external resources are trying to access the DB locks and runs into cyclic wait, it may makes the DB unresponsive.
Deadlock can be avoided using variety of measures, Few listed below -
Can make a queue wherein we can verify and order the request to DB.
Less use of cursors as they lock the tables for long time.
Keeping the transaction smaller. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   database   architecture Asked in 7 Companies expert   frequent Q1313. what will be the output of this code ?
public static void main(String[] args){
StringBuffer s1=new StringBuffer("Buggy");
test(s1);
System.out.println(s1);
}
private static void test(StringBuffer s){
s.append("Bread");
} Core Java
Ans. BuggyBread Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   code   coding   stringbuffer   string   method calling   pass by reference Q1314. what will be the output of this code ?
public static void main(String[] args) {
String s1=new String("Buggy");
test(s1);
System.out.println(s1);
}
private static void test(StringBuffer s){
s.append("Bread");
} Core Java
Ans. Buggy Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   code   coding   stringbuffer   string   method calling   pass by reference Q1315. what will be the output of this code ?
public static void main(String[] args) {
StringBuffer s1=new StringBuffer("Buggy");
test(s1);
System.out.println(s1);
}
private static void test(StringBuffer s){
s=new StringBuffer("Bread");
} Core Java
Ans. Buggy Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   code   coding   stringbuffer   string   method calling   pass by reference Q1316. what will be the output ?
class Animal {
public void eat() throws Exception {
}
}
class Dog2 extends Animal {
public void eat(){}
public static void main(){
Animal an = new Dog2();
an.eat();
}
} Core Java
Ans. Compile Time Error: Unhandled exception type Exception Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   code   coding   overridding   late binding   exception handling   abstract class   abstract methods Q1317. What are advantages of using Servlets over CGI ? Java EE
Ans. Better Performance as Servlets doesn't require a separate process for a single request. Servlets are platform independent as they are written in Java. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   j2ee   servlets   servelets   cgi Q1318. Can we add duplicate keys in a HashMap ? What will happen if we attempt to add duplicate values ?
Ans. No, We cannot have duplicate keys in HashMap. If we attempt to do so , the previous value for the key is overwritten. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   collections   hashmap   map   hashtable Try 1 Question(s) Test Q1319. 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 Q1320. 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 Q1321. What are different types of cookies ? Java EE
Ans. Session cookies , which are deleted once the session is over. Permanent cookies , which stays at client PC even if the session is disconnected. 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 Q1322. 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 Q1323. 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 Q1324. Can we have try and catch blocks within finally ? Core Java
Ans. Yes, if we have a cleanup code that might throw an exception in the finally block, then we can have a try-catch 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   try   catch   yesno Q1325. 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 Q1326. 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 Q1327. 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 Q1328. 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 Q1329. 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 Q1330. 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