Interview Questions and Answers - Order By Newest Q181. What does String intern() method do? Core Java
Ans. intern() method keeps the string in an internal cache that is usually not garbage collected.
Moreover provide reference for scp object for corresponding string object present in heap memory. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   string class   string   intern method   garbage collection   advanced Asked in 2 Companies expert Very frequently asked in HCL Tech ( Based of 4 inputs ) Q182. Write a program to reverse a string iteratively and recursively ? Core Java
Ans. Using String method -
new StringBuffer(str).reverse().toString();
Iterative -
public static String getReverseString(String str){
StringBuffer strBuffer = new StringBuffer(str.length);
for(int counter=str.length -1 ; counter>=0;counter--){
strBuffer.append(str.charAt(counter));
}
return strBuffer;
}
Recursive -
public static String getReverseString(String str){
if(str.length <= 1){
return str;
}
return (getReverseString(str.subString(1)) + str.charAt(0);
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   string   reverse   stringbuffer   string class   code Asked in 6 Companies   frequent Q183. Write a Program to check if 2 strings are Anagrams ? Core Java
Ans. public void checkIfAnagram(String str1,String str2){
boolean anagram = true;
for(char c:str1.toCharArray()){
if(!str2.contains(String.valueOf(c))){
System.out.println("Strings are Anagrams");
anagram = false;
}
if(anagram == true){
System.out.println("Strings are not Anagrams");
}
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve   check if 2 strings are Anagrams Asked in 30 Companies basic   frequent Q184. Difference between new operator and Class.forName().newInstance() ?
Ans. new operator is used to statically create an instance of object. newInstance() is used to create an object dynamically ( like if the class name needs to be picked from configuration file ). If you know what class needs to be initialized , new is the optimized way of instantiating Class. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   oops   object instantiation   object creation   class.forname   newinstance   new operator   difference between   advanced intermediate Q185. Difference between ArrayList and LinkedList ?
Ans. LinkedList and ArrayList are two different implementations of the List interface. LinkedList implements it with a doubly-linked list. ArrayList implements it with a dynamically resizing array. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   collections   list   arraylist   linkedlist   difference between basic   frequent Q186. What are the pre-requisite for the collection to perform Binary Search ?
Ans. 1. Collection should have an index for random access. 2. Collection should have ordered elements. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   collections   search algorithm   search   binary search   at&t intermediate Q187. 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 Q188. 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 Q189. 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) Test Q190. 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 Q191. 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 Q192. 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 Q193. 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 Q194. What are different types of dependency injections ? Design
Ans. Setter injection
Constructor injection
Interface injection
Look-up method/method injection Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  dependency injection   design patterns Asked in 1 Companies Q195. Can we compose the Parent Class object like this ? class BuggyBread1 extends BuggyBread2 { private BuggyBread2 buggybread2; public static void main(String[] args){ buggybread2 = new BuggyBread2(); } }
Ans. Yes. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   composition   inheritance   yesno Try 1 Question(s) Test Q196. 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 Q197. 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) Test Q198. What is the purpose of dialect configured in Hibernate configuration file ? Hibernate
Ans. It tells the framework which SQL varient to generate. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  hibernate   orm   hibernate configurationVery frequently asked Hibernate interview question. Frequently asked in TCS ( based on 2 feedback ) Q199. 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 Q200. 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 Q201. 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 Q202. How to create a Junit to make sure that the tested method throws an exception ?
Ans. Using annotation Test with the argument as expected exception. @Test (expected = Exception.class) Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  junit   junit annotations Q203. 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 Q204. 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 Q205. Name few Dependency Injection frameworks ?
Ans. Google Guice , Spring , PicoContainer and Dagger. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  dependency injection   di   frameworks   ioc Q206. 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 Q207. 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 Q208. 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 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   optionalRarely asked as it was introduced with Java 8. Q210. 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