Interview Questions and Answers - Order By Newest Q301. 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 Q302. 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 Q303. 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 Q304. 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 Q305. What's wrong with this code ? public static void main(String[] args) { String regex = "(\\w+)*"; String s = "Java is a programming language."; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(s); while (matcher.next()) { System.out.println("The e-mail id is: " + matcher.group()); } } Core Java
Ans. matcher.find() should have been used instead of matcher.next() within while. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   regex   pattern.matcher   java.util   coding   code Q306. Which are the sorted collections ? Core Java
Ans. TreeSet and TreeMap Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   collections   treemap   treeset   basic interview question basic   frequent Q307. What is the difference between these two approaches of creating singleton Class ?
//Double Checked Locking Code
public static Singleton createInstance() {
if(singleton == null){
synchronized(Singleton.class) {
if(singleton == null) {
singleton = new Singleton();
}
}
}
return singleton;
}
//Single checked locking code
public static Singleton createInstance() {
synchronized(Singleton.class) {
if(singleton == null) {
singleton = new Singleton();
}
}
return singleton;
} Design
Ans. In First Case , Lock for the synchronized block will be received only if singleton == null whereas in second case every thread will acquire the lock before executing the code.
The problem of synchronization with singleton will only happen when the object has not be instantiated. Once instantiated , the check singleton == null will always generate true and the same object will be returned and hence no problem. First condition will make sure that synchronized access ( acquiring locks ) will only take place if the object has not been created so far. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   singleton   synchronization Try 1 Question(s) TestVery frequently asked Hibernate interview question. Frequently asked in TCS ( based on 2 feedback ) Q308. What are different types of associations in Hibernate ? Hibernate
Ans. There are 4 types of associations in Hibernate
One to One
One to Many
Many to One
Many to Many Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  hibernate   associations Asked in 11 Companies   frequent Q309. 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 filesVery frequently asked if being interviewed for hibernate. Frequently asked in Tata Consultancy (TCS) and Overstock.com Q310. Difference between load and get ? Hibernate
Ans. If id doesnt exist in the DB load throws an exception whereas get returns null in that case.get makes the call to DB immediately whereas load makes the call to proxy. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  hibernate Asked in 16 Companies basic   frequent Q311. What is the way to rollback transaction if something goes wrong using hibernate API ? Hibernate
Ans. We can have the code calling Hibernate API within try block and can have transaction.rollback within Catch. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  hibernate   hibernate rollback Q312. What are the restrictions for the entity classes ? Hibernate
Ans. 1. Entity classes should have default constructor. 2. Entity classes should be declared non final. 3. All elements to be persisted should be declared private and should have public getters and setters in the Java Bean style. 4. All classes should have an ID that maps to Primary Key for the table. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  hibernate   entity classes hibernate Q313. Does java allow implementation of multiple interfaces having Default methods with Same name and Signature ? Core Java
Ans. No. Compilation error. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   java8   default method   yes-no Asked in 1 Companies intermediate Ans. With Java 8, We can provide method definitions in the Interfaces that gets carried down the classes implementing that interface in case they are not overridden by the Class. Keyword "default" is used to mark the default method. Sample Code for interface default Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   java8   default methods Asked in 7 Companies expert   frequent Q315. Can we use static method definitions in Interfaces ? Core Java
Ans. Yes, Effective Java 8. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   java8   static interface methods   yes-no Asked in 1 Companies Q316. How can we test methods individually which are not visible or declared private ?
Ans. We can either increase their visibility and mark them with annotation @VisibleForTesting or can use reflection to individually test those methods. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  junit   reflection api   @visiblefortesting   white box tester Q317. When does an application throw NullPointerException ? Core Java
Ans. When it tries to access an object element or method using reference which is actually null. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  exceptions   npe   nullpointerexception Asked in 1 Companies basic   frequent Q318. What will be the output of this code ? Set mySet = new HashSet(); mySet.add("4567"); mySet.add("5678"); mySet.add("6789"); for(String s: mySet){ System.out.println(s); }
Ans. It will print 4567,5678 and 6789 but Order cannot be predicted. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   collections   set   hashset   coding   code Try 1 Question(s) Test Q319. What will be the output of this code ? Set mySet = new HashSet(); mySet.add("4567"); mySet.add("5678"); mySet.add("6789"); System.out.println(s.get(0));
Ans. This will give compile time error as we cannot retrieve the element from a specified index using Set. Set doesn't maintain elements in any order. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   collections   set   hashset   coding   code Try 1 Question(s) Test Q320. How to generate getters and setters automatically in eclipse ? Eclipse
Ans. Right Click -> Source -> Generate Getters and Setters. and it will open up a dialog wherein we can select the fields to generate Getters and Setters. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  eclipse   java beans Q321. What are the considerations to be made in case of loops in Java ? Core Java
Ans. 1. It shouldn't result in infinite loop. Please make sure that you have a condition that will terminate the loop and that condition should be reached. 2. Make sure to use the break statement if you aspire to only look for something. Not using break will unnecessarily execute it till the end of for loop in some cases. 3. Similarly use continue to execute the loop with next iteration and bypass the rest of the code block if required. 4. Try to avoid multiple nesting of for loops. If it''s required, Make sure to use break and continue properly so as to avoid some unnecessary processing. 5. Make sure to use try catch within the loop and not outside the for loop if you expect it to continue if one of the iteration fails. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   loops   continue   break   for loop  control statements  loop statement.while loop  control statements  loop statement   architecture Q322. How is Abstraction implemented in Java ? Core Java
Ans. Abstraction is provided in Java by following ways -
Coding to the ( Interfaces / Abstract Classes ) or contracts
By Encapsulating details within classes and exposing the minimal Door ( few public methods ) Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   oops concepts   abstraction   interfaces   abstract class   encapsulation  object oriented programming (oops)  oops concepts Asked in 3 Companies basic   frequent Q323. What are some different logging levels for log4j ? Log4j
Ans. DEBUG
ERROR
ALL
WARN
INFO
FATAL
OFF
TRACE
TRACE_INT
WARN Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   log4j   log4j level   logging   error logging Asked in 10 Companies Not frequently asked as it was introduced with Java 8. Ans. Optional is a good way to protect application from runtime nullPointerException in case the the absent value has been represented as null. So basically Optional class provides the type checking during compile time and hence will never result in NPE. For ex - List> intList = new ArrayList>(); intList.add(Optional.empty()); intList.add(Optional.of(new Employee("abc"))); intList.add(Optional.of(new Employee("xyz"))); intList.add(Optional.of(new Employee("123"))); System.out.println(intList.get(0).getName()); So Now , even when the first list element is empty, this code will never throw an NullPointerException. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   java8   java 8   optional Q325. Can we generate Java Documentation using Export -> JavaDoc using a jar file ? Eclipse
Ans. Yes, If the jar contains the source code. No, If it contains only classes. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   javadoc   eclipseRarely asked as it was introduced with Java 8. Q326. Name few "Optional" classes introduced with Java 8 ? Core Java
Ans. http://www.buggybread.com/2015/01/java-optional-classes-and-interfaces.html Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   optional   java 8   java8 Q327. 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 Q328. What are the various ways in which build can be scheduled in Jenkins ? Jenkins
Ans. Builds can be triggered by source code management commits. Can be triggered after completion of other builds. Can be scheduled to run at specified time ( crons ) Manual Build Requests Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   jenkins   build management   build manager Q329. Difference between Stack and Heap memory ? Core Java
Ans. Stack memory areas is used to hold method and local variables while objects are always allocated memory in the heap. The heap memory is shared between multiple threads whereas Stack memory isn't. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   memory management   stack memory   heap memory   difference between   ebay Asked in 2 Companies intermediate   frequent Try 1 Question(s) Test Q330. How to access a Web Element if there are many elements with the same XPath ?
Ans. By specifying the Index like //button[@class='button'])[2] Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  selenium   selenium webdriver   automation testing