Interview Questions and Answers - Order By Rating Ans. An enumeration is an interface containing methods for accessing the underlying data structure from which the enumeration is obtained. It allows sequential access to all the elements stored in the collection. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   enum   enumeration interface   basic interview question Try 2 Question(s) Test Q602. In a case where there are no instance variables what does the default constructor initialize? Core Java
Ans. Java expects the superclass ( Object Class ) constructor to be called while creation of any object. So super constructor is called in case there are no instance variables to initialize. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   constructor   object creation   default constructor   super Try 1 Question(s) Test Q603. Difference between Encapsulation and Data Hiding ?
Ans. Data Hiding is a broader concept. Encapsulation is a OOP's centri concept which is a way of data hiding in OOP's. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   oops   oops concepts   encapsulation   data hiding   build management basic Q604. Difference between Abstraction and Implementation hiding ?
Ans. Implementation Hiding is a broader concept. Abstraction is a way of implementation hiding in OOP's. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   oops   oops concepts   abstraction   hiding information   build management Q605. 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 Q606. What are the examples of Abstraction in Java ?
Ans. function calling - hides implementation details wrapper classes new operator - Creates object in memory, calls constructor Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   oops   oops concepts   abstraction Q607. 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 Asked multiple times in Capgemini. Q608. Why Char array is preferred over String for storing password? Core Java
Ans. String is immutable in java and stored in String pool. Once it's created it stays in the pool until unless garbage collected, so even though we are done with password it's available in memory for longer duration and there is no way to avoid it. It's a security risk because anyone having access to memory dump can find the password as clear text. 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   string pool   garbage collection   advanced Asked in 6 Companies Expert Q609. What is the use of HashCode in objects ? Core Java
Ans. Hashcode is used for bucketing in Hash implementations like HashMap, HashTable, HashSet etc. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   string   hashcode   hash code   string comparison  hashtable Asked in 17 Companies basic   frequent Frequently asked to fresh graduates and less experienced. Q610. Difference between Composition and Inheritance ? Core Java
Ans. Inheritance means a object inheriting reusable properties of the base class. Compositions means that an abject holds other objects.
In Inheritance there is only one object in memory ( derived object ) whereas in Composition , parent object holds references of all composed objects.
From Design perspective - Inheritance is "is a" relationship among objects whereas Composition is "has a" relationship among objects. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   oops   oops concepts   inheritance  object oriented programming (oops)  oops concepts   composition  object oriented programming (oops)  oops concepts   difference between   basic interview question Asked in 12 Companies basic   frequent Try 2 Question(s) Test Q611. Will finally be called always if all code has been kept in try block ? Core Java
Ans. The only time finally won't be called is if you call System.exit() or if the JVM crashes first. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   exceptions   exception handling   try   catch   finally   system   system.exit Q612. 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 ) Q613. 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 Q614. If you have access to a function that returns a random integer from one to five, write another function which returns a random integer from one to seven. Core Java
Ans. We can do that by pulling binary representation using 3 bits ( random(2) ).
getRandom7() {
String binaryStr = String.valuesOf(random(2))+String.valuesOf(random(2))+String.valuesOf(random(2));
binaryInt = Integer.valueOf(binaryStr);
int sumValue=0;
int multiple = 1;
while(binaryInt > 0){
binaryDigit = binaryInt%10;
binaryInt = binaryInt /10;
sumValue = sumValue + (binaryDigit * multiple);
multiple = multiple * 2;
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   tricky question   interesting questions   google interview questions   random Q615. Write a method to convert binary to a number ? Core Java
Ans.
convert(int binaryInt) {
int sumValue=0;
int multiple = 1;
while(binaryInt > 0){
binaryDigit = binaryInt%10;
binaryInt = binaryInt /10;
sumValue = sumValue + (binaryDigit * multiple);
multiple = multiple * 2;
}
return sumValue;
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   binary   interesting question   coding Q616. 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 Q617. How to find whether a given integer is odd or even without use of modulus operator in java? Core Java
Ans. public static void main(String ar[])
{
int n=5;
if((n/2)*2==n)
{
System.out.println("Even Number ");
}
else
{
System.out.println("Odd Number ");
}
} 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   modulus operator Asked in 4 Companies   frequent Q618. Can we use Ordered Set for performing Binary Search ?
Ans. We need to access values on the basis of an index in Binary search which is not possible with Sets. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   sets   set   collections   binary search Q619. 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 Frequently asked. Q620. If you are given a choice to use either ArrayList and LinkedList, Which one would you use and Why ? Core Java
Ans. ArrayList are implemented in memory as arrays and hence allows fast retrieval through indices but are costly if new elements are to be inserted in between other elements. LinkedList allows for constant-time insertions or removals using iterators, but only sequential access of elements
1. Retrieval - If Elements are to be retrieved sequentially only, Linked List is preferred.
2. Insertion - If new Elements are to be inserted in between other elements , Linked List is preferred.
3. Search - Binary Search and other optimized way of searching is not possible on Linked List.
4. Sorting - Initial sorting could be pain but lateral addition of elements in a sorted list is good with linked list.
5. Adding Elements - If sufficiently large elements needs to be added very frequently ,Linked List is preferable as elements don't need consecutive memory location. Sample Code for ArrayList Sample Code for LinkedList 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   architecture   data structure   ebay Asked in 2 Companies basic   frequent Try 2 Question(s) Test Q621. 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 Advanced level question frequently asked in US based companies. Recently asked in EMC and Intuit. Q622. Can you provide some implementation of a Dictionary having large number of words ? Solution
Ans. Simplest implementation we can have is a List wherein we can place ordered words and hence can perform Binary Search.
Other implementation with better search performance is to use HashMap with key as first character of the word and value as a LinkedList.
Further level up, we can have linked Hashmaps like ,
hashmap {
a ( key ) -> hashmap (key-aa , value (hashmap(key-aaa,value)
b ( key ) -> hashmap (key-ba , value (hashmap(key-baa,value)
....................................................................................
z( key ) -> hashmap (key-za , value (hashmap(key-zaa,value)
}
upto n levels ( where n is the average size of the word in dictionary. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   collections   hashmap   binary search   search algorithm   advanced   architecture   data structure Asked in 6 Companies   frequent Try 1 Question(s) TestAns. PATH is the variable that holds the directories for the OS to look for executables. CLASSPATH is the variable that holds the directories for JVM to look for .class files ( Byte Code ). Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   path   classpath   byte code   jvm   basic interview question Asked in 3 Companies intermediate   rare Q624. Which Java collection class can be used to maintain the entries in the order in which they were last accessed?
Ans. LinkedHashMap Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   collections   hashmap   linkedhashmap Q625. Is it legal to initialize List like this ? LinkedList l=new LinkedList();
Ans. No, Generic parameters cannot be primitives. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   collections   list   linkedlist   generics   yes-no Q626. 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 Q627. 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 Q628. 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 Q629. what will be the output ?
class Animal {
public void eat() throws Exception {
}
}
class Dog2 extends Animal {
public void eat(){}
public static void main(){
Animal an = new Dog2();
an.eat();
}
} Core Java
Ans. Compile Time Error: Unhandled exception type Exception Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   code   coding   overridding   late binding   exception handling   abstract class   abstract methods Q630. 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