Interview Questions and Answers - Order By Newest Q331. What is the difference between float and double?
Ans. Float can represent up to 7 digits accurately after decimal point, where as double can represent up to 15 digits accurately after decimal point. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   data types   float   double   difference between   basic interview question Q332. Is it possible to compile and run a Java program without writing main( ) method?
Ans. Yes, it is possible by using a static block in the Java program. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   main   static block   compile   yes-no Q333. Do you think that Java should have had pointers ? Core Java
Ans. Open ended Questions. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   open questions   pointers   advanced Q334. Tell something about history of Java ? Core Java
Ans. Java was initially found in 1991 by James Gosling, Sun Micro Systems. At first it was called as "Oak". In 1995 then it was later renamed to "Java". java is a originally a platform independent language. Currently Oracle, America owns Java. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   history of java Q335. How to find if JVM is 32 or 64 bit from Java program. ? Core Java
Ans. You can find JVM - 32 bit or 64 bit by using System.getProperty() from Java program. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   jvm 32   jvm 64   jvm Q336. According to Java Operator precedence, which operator is considered to be with highest precedence? Core Java
Ans. Postfix operators i.e () [] . is at the highest precedence. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   operators   operator precedence Asked in 2 Companies Ans. It is used to sort collections and arrays of objects using the collections.sort() and java.utils. The objects of the class implementing the Comparable interface can be ordered. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   collections   comparable interface Asked in 7 Companies intermediate   frequent Try 1 Question(s) Test Q338. What is Externalizable interface? Core Java
Ans. Externalizable is an interface which contains two methods readExternal and writeExternal. These methods give you a control over the serialization mechanism. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   serialization   externalizable interface Asked in 2 Companies intermediate   rare Q339. What is Maven's order of inheritance? Maven
Ans. 1. parent pom 2. project pom 3. settings 4. CLI parameters Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  maven   build technologies   pom.xml   parent pom   project pom   build management Try 1 Question(s) Test Q340. What is a Mojo? Maven
Ans. A mojo is a Maven plain Old Java Object. Each mojo is an executable goal in Maven, and a plugin is a distribution of one or more related mojos. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  maven   build technologies   mojo Q341. What are the features of encapsulation ? Core Java
Ans. Combine the data of our application and its manipulation at one place.
Encapsulation Allow the state of an object to be accessed and modified through behaviors.
Reduce the coupling of modules and increase the cohesion inside them. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   oops   oops concepts   encapsulation  object oriented programming (oops)  oops concepts   basic interview question Asked in 2 Companies basic   frequent Q342. Write a method to check if input String is Palindrome? Core Java
Ans. private static boolean isPalindrome(String str) {
if (str == null)
return false;
StringBuilder strBuilder = new StringBuilder(str);
strBuilder.reverse();
return strBuilder.toString().equals(str);
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   string   stringbuilder   stringbuilder   string class   code   palindrome Asked in 38 Companies Basic   frequent Q343. Write a method that will remove given character from the String? Core Java
Ans. private static String removeChar ( String str , char c ) {
if ( str == null )
return null ;
return str . replaceAll ( Character . toString ( c ), "" );
}
Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   string   string class   code   write code Q344. Why String is popular HashMap key in Java? Core Java
Ans. Since String is immutable, its hashcode is cached at the time of creation and it doesnt need to be calculated again. This makes it a great candidate for key in a Map and its processing is fast than other HashMap key objects. This is why String is mostly used Object as HashMap keys. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   string class   string   immutable  immutability   hashmap   immutable  immutability   hashcode   hash code   advanced Asked in 2 Companies expert   frequent Q345. 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 Q346. Will the following program display ""Buggy Bread"" ?
class Test{
static void display(){
System.out.println(""Buggy Bread"");
}
}
class Demo{
public static void main(String... args){
Test t = null;
t.display();
}
} Core Java
Ans. Yes. static method is not accessed by the instance of class. Either you call it by the class name or the reference. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   static   static method   code Q347. How substring() method of String class create memory leaks? Core Java
Ans. substring method would build a new String object keeping a reference to the whole char array, to avoid copying it. Hence you can inadvertently keep a reference to a very big character array with just a one character string. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   string class   string   substring   memory leaks   jvm   memory management   advanced   architecture   technical architect   technical lead expert Very frequently asked in HCL Tech ( Based of 4 inputs ) Q348. 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 Q349. 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 Q350. 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 Q351. 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 Q352. 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 Q353. 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 Q354. 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 Q355. 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 Q356. 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 Q357. 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 Q358. 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 Q359. 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 Q360. 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