Interview Questions and Answers - Order By Newest Very frequently asked in HCL Tech ( Based of 4 inputs ) Q211. 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 Q212. 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 Q213. 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 Q214. 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 Q215. 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 Q216. 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 Q217. What are temp tables ?
Ans. These are the tables that are created temporarily and are deleted once the Stored Procedure is complete. For example - we may like to pull some info from a table and then do some operations on that data and then store the output in final output table. We can store the intermediary values in a temp table and once we have final output with us, we can just delete it. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  database   sql   temp tables   tables Q218. 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 Q219. 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 Q220. 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 Q221. 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 Q222. 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 Q223. What are the advantages of Hibernate ? Hibernate
Ans. 1. No need to know SQL, RDBMS, and DB Schema. 2. Underlying Database can be changed without much effort by changing SQL dialect and DB connection. 3.Improved Performance by means of Caching. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  hibernate   orm   architecture Q224. 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 configuration Q225. Please specify in what sequence the objects of following classes will be created ?
Session , SessionFactory, Query , Configuration Hibernate
Ans. Configuration -> SessionFactory -> Session -> Query Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  hibernateVery frequently asked Hibernate interview question. Frequently asked in TCS ( based on 2 feedback ) Q226. 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 Q227. 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 files Q228. What are the Core Interfaces of Hibernate Framework ? Hibernate
Ans. Configuration SessionFactory Session Transaction Query and Citeria Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  hibernate Q229. What are collection types in Hibernate ? Hibernate
Ans. Bag, Set , List , Array, Map Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  hibernateVery frequently asked if being interviewed for hibernate. Frequently asked in Tata Consultancy (TCS) and Overstock.com Q230. 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 Q231. 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 Q232. 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 Q233. 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 Q235. 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 Q236. Difference between a Pointer and a Reference ?
Ans. We can't get the address of a reference like a pointer. Moreover we cannot perform pointer arithmetic with references. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   pointer   reference   at&t Q237. 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 Q238. 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 Q239. Why can't we use this in static context ? Core Java
Ans. Static methods can be called using instance references wherein this would have made sense but static method can also be called using Class name wherein this would mean nothing and hence forbidden. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   static methods   this keyword Try 2 Question(s) Test Which of the following cannot be marked static ? Constructors , Classes ( Outer ) , Classes ( nested ), Interfaces , Local variables , Inner Class methods and instance variables. Constructors , Classes ( Outer ) , Interfaces , Local variables , Class variables , Class Methods , Inner Class methods and instance variables. Constructors , Classes ( Outer ) , Interfaces , Local variables , Inner Class methods and instance variables. Constructors , Classes ( Outer ) , Classes (Nested), Interfaces , Local variables , Inner Class methods and instance variables.
Q240. 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