Core Java Interview Questions and Answers
Hi guys, I am attempting to put as many of Core Java interview questions and answers as possible in this post. You can bookmark this post as the no. of questions here will increase over time as I keep updating them.
Q.1 How is Java platform independent?
Ans: Before going into the detail,first you have to understand what is the mean of platform? Platform consists of the computer hardware(mainly architecture of the microprocessor) and OS. Platform=hardware+Operating System
Anything that is platform independent can run on any operating system and hardware.
Java is platform indepedent so java can run on any operating system and hardware. Now question is how it is platform independent?
This is because of the magic of Byte Code which is OS indepedent. When java compiler compile any code then it generate the byte code not the machine native code(unlike C compiler).Now this byte code need a interpreter to execute on a machine.This interpreter is JVM.So JVM read that byte code(that is machine indepedent) amd execute it. Different JVM is designed for different OS and byte code is able to run on different OS.
In case of C or C++(language that are not platform indepedent) compiler generate the .exe file that is OS depedent so when we run this .exe file on another OS it will not run because this file is OS depedent so is not compatible with the another OS.
Finally an intermediate OS independent Byte code make the java platform independent.
Q.2 Is Java compiled or interpreted?
Ans: The terms “interpreted language” or “compiled language” don’t make sense, because any programming language can be interpreted and/or compiled.
As for the existing implementations of Java, most involve a compilation step to bytecode, so they involve compilation. The runtime also can load bytecode dynamically, so some form of a bytecode interpreter is always needed. That interpreter may or may not in turn use compilation to native code internally.
These days partial just-in-time compilation is used for many languages which were once considered “interpreted”, for example Javascript.
Q.3 What is the difference between ArrayList and Vector?
Ans:
ArrayList | Vector |
---|---|
1) ArrayList is not synchronized. | Vector is synchronized. |
2) ArrayList increments 50% of current array size if number of element exceeds from its capacity. | Vector increments 100% means doubles the array size if total number of element exceeds than its capacity. |
3) ArrayList is not a legacyclass, it is introduced in JDK 1.2. | Vector is a legacy class. |
4) ArrayList is fast because it is non-synchronized. | Vector is slow because it is synchronized i.e. in multithreading environment, it will hold the other threads in runnable or non-runnable state until current thread releases the lock of object. |
5) ArrayList uses Iteratorinterface to traverse the elements. | Vector uses Enumeration interface to traverse the elements. But it can use Iterator also. |
Q.4 What is the difference between HashMap and Hashtable?
Ans:
HashMap | Hashtable |
---|---|
1) HashMap is non synchronized. It is not-thread safe and can’t be shared between many threads without proper synchronization code. | Hashtable is synchronized. It is thread-safe and can be shared with many threads. |
2) HashMap allows one null key and multiple null values. | Hashtable doesn’t allow any null key or value. |
3) HashMap is a new class introduced in JDK 1.2. | Hashtable is a legacy class. |
4) HashMap is fast. | Hashtable is slow. |
5) We can make the HashMap as synchronized by calling this code Map m = Collections.synchronizedMap(hashMap); | Hashtable is internally synchronized and can’t be unsynchronized. |
6) HashMap is traversed by Iterator. | Hashtable is traversed by Enumerator and Iterator. |
7) Iterator in HashMap is fail-fast. | Enumerator in Hashtable is not fail-fast. |
8) HashMap inherits AbstractMap class. | Hashtable inherits Dictionaryclass. |
Q.5 How many objects are created by the following code?
String s=new String("abc");
Ans:
Considering only the given statement, two objects are created.
“When a class is loaded (note that loading happens prior to initialization), the JVM goes through the code for the class and looks for String literals. When it finds one, it checks to see if an equivalent String is already referenced from the heap. If not, it creates a new String instance on the heap and stores a reference to that object in the string constant pool.”
So when the class containing this code is loaded, JVM finds the string literal “ABC”, creates a string object and puts its reference into string constant pool. Now when the statement is executed, JVM is forced to create a new string object with contents “ABC” because of the new operator. This time no reference will be put into string constant pool because already there is reference for the same literal.
So two objects are created by the statement.
There are 2 ways to create String Objects in Java
(1) Using the new operator i.e.
String s1=new String("abc");
(2) Using the String Literal i.e.
String s2="abc";
. . Now String allocation is costly in both time and memory so the JVM(Java Virtual Machine) performs some tasks? WHAT TASKS? See whenever you are using the “new” operator the object is created, JVM will not look in string pool it is just going to create the Object, but when you are using the string literals for creating String objects then JVM will perform the task of looking in the string pool i.e. When you will write
String s2="abc";
the JVM will look in string pool and check if “abc”already exists or not. If it exists then then a reference is returned to the already existed string “abc” and new object is not created and if it doesn’t exists then a object is created. So in your case (a)
String s1=new String("abc");
——–Since “new” is used to therefore Object is created (b)
String s2="abc";
—-using String literal a object is created and “abc” is not in string pool therefore Object is created (c)
String s3="abc";
—-Again using String literal and “abc” is in string pool therefore Object is not created. . . You can also check it out by using the following the code
class String_Check
{
public static void main(String[] n)
{
String s1=new String("abc");
String s2="abc";
String s3="abc";
if(s1==s2)
System.out.println("s1==s2");
if(s1==s3)
System.out.println("s1==s3");
if(s2==s3)
System.out.println("s2==s3");
}
}
Hope this helps…Note == is used to see if Object are equal and equals(Object) method is used to see if content are equal.
Comments
Post a Comment