Wednesday 30 September 2015

Important String interview questions



Important String questions which are mostly asked in interviews.Many interviewer focus on String concepts to check the fundamentals of interviewee.Go through the below questions which will help you to improve your understanding about String.


1) Find the output of below program ?

 public class TestProgram {  
      public void method(Object o) {  
           System.out.println("Object ");  
      }  
      public void method(String s) {  
           System.out.println("String ");  
      }  
      public static void main(String a[]) {  
           TestProgram testProgram = new TestProgram();  
           testProgram.method(null);  
      }  
 }  

 Output:-  
 String   

Reason:- null value is considered as String which calls the method with String as an argument.

2) Find the output of below program ?

 public class TestProgram {  
   public static void main(String a[]) {  
    String s1=new String("Hello");  
    String s2=new String("Helloo");  
    System.out.println(s1=s2);  
   }  
 }  

Output:-  
 Helloo  

Reason:- String s2 value is assigned to s1 in System.out.println(s1=s2) statement.

3)  Find the output of below program ?

 public class TestProgram {  
      public void method(StringBuffer sb) {  
           System.out.println("StringBuffer Version");  
      }  
      public void method(String s) {  
           System.out.println("String Version");  
      }  
      public static void main(String a[]) {  
           TestProgram q = new TestProgram();  
           q.method(null);  
      }  
 }  

Output:- Compile time error.

Reason:- The method method(StringBuffer sb) is ambiguous for the type TestProgram.
If you comment the public void method(String s), then code works fine and it will print StringBuffer Version.

4) Find the output of below program ?

 public class TestProgram {  
      public static void main(String a[]) {  
           String s1 = new String("Hello");  
           String s2 = new String("s1");  
           System.out.println(s1 == s2);  
      }  
 }  


 Output:-  
 false  

Reason:- Both the instances s1 and s2 does not refer to the same object.They are referring to different object with different memory address.Hence equality comparison returns false whereas if we check the contents of both the strings using equals method, then it will return true.

5) Find the output of below program ?

public class TestProgram {  
      public static void main(String a[]) {  
           String s1 = new String("Hello");  
           StringBuffer s2 = new StringBuffer(s1);  
           System.out.println(s1 == s2.toString());  
           System.out.println(s1.equals(s2));  
      }  
 }  

 Output:-  
 false  
 false 

Reason:- first print statement returns false because both s1 and s2 are referring to distinct object with distinct memory address and second print statement returns false because the equals method from StringBuffer is not overridden from Object class, so it uses reference equality(==).

6) Find the output of below program ?

1:  public class TestProgram {  
2:       public static void main(String a[]) {  
3:            String obj = "hello";  
4:            String obj2 = obj;  
5:            obj2 = " world";  
6:            System.out.println(obj + " " + obj2);  
7:       }  
8:  }  

 Output:-  
 hello world 

Reason:- In line number 3,obj is referring to String literal "hello".In line 4,obj2 is referring to same String as referred by obj.In line 5,obj2 is referring to new String literal " world".

7) Find the output of below program ?

 public class TestProgram {  
      public static void main(String a[]) {  
           String x = "hello ";  
           x.concat("world");  
           System.out.println(x);  
      }  
 } 

 Output:-  
 hello  

Reason:- concat method called on String x creates a new String object which needs to be assigned to a reference variable.String x is still referring to original String literal "hello ".

8) How many String objects created below ?

 public class TestProgram {  
      public static void main(String a[]) {  
           String s1 = "Hello ";  
           String s2 = s1 + "john ";  
           s1.concat("pallavi");  
           s2.concat(s1);  
           s1 += "ashish";  
           System.out.println(s1 + " " + s2);  
      }  
 }  

 Output:-  
 Hello ashish Hello john 

8 Strings objects get created.

Reason:-
1 :- "Hello " object gets created in String pool.
2:- "john " gets created in String pool and lost because no String variable is directly referring to it.
3:- "Hello john " s2 gets created.
4:- "pallavi" gets created in String pool and lost because no String variable is directly referring to it.
5:- "Hello pallavi " gets created in String pool and lost because no String variable is directly referring to it.
6:- "Hello john Hello" gets created in String pool and lost because no String variable is directly referring to it.
7:- "ashish " gets created in String pool and lost because no String variable is directly referring to it.
8:- "Hello ashish" gets created in String pool.

9) How many String objects created below ?

 public class TestProgram {  
   public static void main(String a[]) {  
    String s1 = new String("Hello");  
    String s2 = new String("Hello");  
   }  
 }  

Answer:- 3

Reason:-
1:- first String literal object "Hello"  gets created in String pool.
2:- a new String object with the value "Hello" placed in Heap memory.
3:- another new String object with the value "Hello" placed in Heap memory.

10) Find the output of below program ?

 public class TestProgram {  
   public static void main(String a[]) {  
    StringBuffer str = "";  
   }  
 } 

Output:- Compile time error.

Reason:- In order to create a StringBuffer object ,we need to build an object explicitly like new StringBuffer(""); .whereas in case String we can directly assign the literal like String str="";

11) Find the output of below program ?

1:  public class TestProgram {  
2:    public static void main(String a[]) {  
3:     String str = "";  
4:     String str1="Hello";  
5:     System.out.println(str.length());  
6:     System.out.println(str1.length());  
7:     System.out.println(str1.charAt(0));  
8:     System.out.println(str1.charAt(5));  
9:    }  
10:  }  

Output:-  
 0  
 5  
 H  
 Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5  
      at java.lang.String.charAt(Unknown Source)  
      at TestProgram.main(TestProgram.java:9)  


Reason:- At line number 8, it gives an StringIndexOutOfBoundsException because String index starts from 0 .

12) How many String objects created below ?

 public class TestProgram {    
   public static void main(String a[]) {  
    String s1="Hello";  
    String s2="Hello";  
    String s3="Hello";  
   }  
 } 

Answer:- 1

Reason:- "Hello" String literal object gets created in String pool and s1,s2,s3 refers to the same literal object.

13) Difference between StringBuffer and String?
 refer to this link what is the differnce between StringBuffer and String?

14) Difference between String literal and String Object?
refer to this link difference between String literal and String object?


All the best :)