Important Java
Pesquisas Acadêmicas: Important Java. Pesquise 862.000+ trabalhos acadêmicosPor: samrod • 26/9/2014 • 2.695 Palavras (11 Páginas) • 622 Visualizações
20 Important Java Programming Interview Questions
Java Programming Interview Questions
Powered by –
Contents
Q1. What is the difference between creating String as new() and literal? ........................ 3
Q2. What is difference between String, StringBuffer and StringBuilder in Java? ............ 3 Q3. What is Singleton? Is it better to make whole method synchronized or only critical section synchronized? ................................................................................................ 5 Q4. What will happen if you call return statement or System.exit on try or catch block? Will finally block execute? ........................................................................................... 6 Q5. Can you override private or static method in Java? ..................................................... 6
Q6. What are the differences between == and .equals()? ................................................... 7 Q7. What is the difference between final, finally and finalize? What do you understand by the java final keyword? ................................................................................. 8 Q 8 When is static variable loaded? Is it at compile time or runtime? When exactly a static block is loaded in Java? ............................................................................... 8 Q 9 What is reflection API? How are they implemented? ................................................... 8
Q 10 What is difference between String and StringTokenizer? .......................................... 9 Q 11 What is the Difference between JDK and JRE? .......................................................... 9 Q 12 Explain the difference between ITERATOR AND ENUMERATION INTERFACE with example. .................................................................................................. 10 Q 13 What is the use of the ‘SimpleDateFormat’ and how can you use it to display the current system date in ‘yyyy/MM/DD HH:mm:ss’ format? .......................... 11 Q.14 Why insertion and deletion in ArrayList is slow compared to LinkedList? ............ 11 Q.15 What is the Comparable interface? ........................................................................... 11 Q.16 What is an Iterator? What differences exist between Iterator and ListIterator? ........................................................................................................................... 12
Q.17 How HashMap works in Java? .................................................................................. 12 Q.18 What if when two different keys have the same hashcode? .................................. 12 Q.19 How will you measure the performance of HashMap? ........................................... 13
Q.20 Difference between throw and throws? .................................................................... 13
Q1. What is the difference between creating String as new() and literal?
Ans: When we create string with new() Operator, it’s created in heap and not added into string pool while String created using literal are created in String pool itself which exists in PermGen area of heap.
String s = new String("Test");
Does not put the object in String pool, we need to call String.intern() method which is used to put them into String pool explicitly. Its only when you create String object as String literal
E.g. String s = "Test”;
Java automatically put that into String pool.
Q2. What is difference between String, StringBuffer and StringBuilder in Java?
String String is immutable (once created cannot be changed) object. The object created as a String is stored in the Constant String Pool. Every immutable object in Java is thread safe that implies String is also thread safe. String cannot be used by two threads simultaneously. String once assigned cannot be changed. String demo = "hello”; // The above object is stored in constant string pool and its value cannot be modified. demo="Bye"; //new "Bye" string is created in constant pool and referenced by the demo variable // "hello" string still exists in string constant pool and its value is not overrided but we lost reference to the "hello” string
StringBuffer StringBuffer is mutable means one can change the value of the object. The object created through StringBuffer is stored in the heap. StringBuffer has the same methods as the StringBuilder, but each method in StringBuffer is synchronized that is StringBuffer is thread safe. Due to this it does not allow two threads to simultaneously access the same method. Each method can be accessed by one thread at a time.
But being thread safe has disadvantages too as the performance of the StringBuffer hits due to thread safe property. Thus StringBuilder is faster than the StringBuffer when calling the same methods of each class. StringBuffer value can be changed, it means it can be assigned to the new value. Nowadays it’s a most common interview question, the differences between the above classes. String Buffer can be converted to the string by using toString() method. StringBuffer demo1 = new StringBuffer("Hello"); // The above object stored in heap and its value can be changed. demo1=new StringBuffer("Bye"); // Above statement is right as it modifies the value which is allowed in the StringBuffer.
StringBuilder StringBuilder is same as the StringBuffer that is it stores the object in heap and it can also be modified. The main difference between the StringBuffer and StringBuilder is that StringBuilder is also not thread safe. StringBuilder is fast as it is not thread safe. StringBuilder demo2= new StringBuilder("Hello"); // The above object too is stored in the heap and its value can be modified demo2=new StringBuilder("Bye"); // Above statement is right as it modifies the value which is allowed in the StringBuilder.
-------------------------------------------------------------------------------------------------------- String StringBuffer StringBuilder -------------------------------------------------------------------------------------------------------- Storage Area | Constant String Pool Heap Heap Modifiable | No (immutable) Yes(mutable) Yes(mutable ) Thread Safe | Yes Yes No Performance | Fast Very slow Fast --------------------------------------------------------------------------------------------------------
Q3. What is Singleton? Is it
...