Thursday 18 July 2013

The most frequently asked interview questions java


1.     What are string, string buffer and String builder?
Ans:-  String:
             String is a final class available in java.lang.package.
             String is immutable i.e String  object can't be modified
             We can create the string class object in three ways
  1. Using the new operator    eg: String s1= new String(“Java”);
  2. Without new operator as a string literal        eg: String s2= “Hello”;
  3. Cha array[] = {‘j’,’a’,’v’,’a’};      String s3 = new String(array) 
      String Buffer   :
      String Buffer  is a mutable ie. String Buffer object is mutable .
      String Buffer object will be created only one way that is using new operator. 


String
String Buffer
String objects are immutable
String Buffer object are mutable
We can create the string object in two ways
              With new operator
            Without new operator
We can create String Buffer object only in one way
           With new operator
String will use string constant pooling mechanism internally for the reusability of the string
There is no concept of pooling in the string Buffer
When you do any concatenation of strings always a new String object will be created for the result
When u do any concatenation then existing string only modified
String class methods are not synchronized
Majority of the string buffer class methods are synchronized.

    String Builder: String  Builder and String Buffer both are same only one difference is String Builder class  methods are not Synchronized.
       2.  What are the differences between these three Final, Finally and Finalize?
      Ans:- Final:  
          Final is a keyword can be used for the following 3 things 
  • To declare the final variables i.e.,  Final variable cannot be modified
  • To specify the final methods i.e.,  Final method cannot be overridden
  • To specify the final class  i.e.,  Final classes cannot be inherited
3. What does mean Garbage collector?
Ans:- 
  •  Garbage collector is form of memory management.
  •  It mostly uses mark and sweep algorithms. 
  • It is automatically invoked when program is being run(when JVM starts).
  • It can be also called by calling gc() method of run time class or system class in java. 
4. Why java is slower then C, C++ ?
Ans:-