Interview Questions and Answers - Order By Newest Q271. 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 Q272. 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 Q273. 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 Q274. 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 Q276. 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 Q277. 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 Q278. 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 Q279. 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 Q280. 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 Q281. 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 Q282. 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 Q283. 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 ) Q284. 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 Q285. 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 Q286. 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 Q287. 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 Q288. 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 Q289. 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 Q290. 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 Q291. 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 Q292. 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 Q293. 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 Q294. 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 Q295. 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 Q296. 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 Q297. 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 Q298. 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 Q299. 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 Q300. 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