Saturday, 14 June 2014

String Related Interview Questions

 Hi..i will share some String interview questions with answers so it can help for the job seekers.

Question: Is String data type in Java?

Answer:- No, String is a class, not data type.

Question: How many ways to create a String object?

Answer:--Two ways   1) String literal
                                    2) Using new Operator

Question: What is difference between creating String as new and literal?

Answer:-- 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.
           
Question:     String s1="nrk";
                       String s2="nrk";
                       String s3=new String("nrk");
                       if(s1==s2){
                                     System.out.println("s1 & s2 equal");
                        }
                        else{
                                     System.out.println("s1 & s2 not equal");
                        }
                        if(s2==s3){
                                     System.out.println("s2 & s3 equal");
                        }
                          else{
                                      System.out.println("s2 & s3 not equal");
                         }
                what would be the output?

Answer :    s1 & s2 equal
                    s2 & s3 not equal 

Question :--see below two variables what is difference?
                             String a="hcl technology";
                    final String a="hcl technology";

Answer :  Here both are immutable but in first variable reassignment of variable
                is  possible but in case of final it is not possible. It will give
               compile-time  error.
           e.g          String a="hcl";
                            a=a.concat("b");            //no error,compile & run normally
            in case of final,  
                            final String a="hcl";
                            a=a.concat("b");    //give compile time error,reassignment
                                                                 // not possible.

Question :    System.out.println(2+3+"4");
                       System.out.println("2"+3+4);
                  what would be the output for above two lines?

Answer :--    54
                     234

Question :-- What is the output of the given code?
                            
                         public class A{
                                public static void main(String [] args) {
                                      String s1 = null;
                                      String s2 = s1+"java";
                                      System.out.println(s2);
                               }
                         }
Answer: nulljava

Question:--What is the output of the given code?

                               public class A {
                                   public static void main(String [] args) {
                                         String s1 = null;
                                         System.out.println(s1.length());
                                   }
                              }
Answer:java.lang.NullPointerException


Question:--What is the output of the given code?
                    
                              public class A {
                                   public static void main(String [] args) {
                                         String s1 = null;
                                         System.out.println(s1);
                                   }
                              }
 Answer:--null

Question:--What is the output of the given code?
                    
                              public class A {
                                   public static void main(String [] args) {
                                         System.out.println(null);
                                   }
                              }
 Answer:--Compile Time Error.

Question:--Write a program to swap two String variables without using the third 
                     variable.
Answer:---
                                   String a="var1";
                                   String b="var2";
                                   a= a+b;
                                   b = a.substring(0,(a.length()-b.length()));
                                   a = a.substring(b.length(),(a.length()));
                                   System.out.println("a = "+a+" b="+b);   //a=var2 b=var1

 Question:--Write a program to replace particular character with another
                  character  from the given string without using Java API's.

                          public class ReplaceCh {
                               public static void main(String[] args) {
                                            String a="Software";
                                            String[] a1=a.split("");
                                            String ch="";
                                            for(int i=0;i<a1.length;i++){
                                                     if(a1[i].equals("w")){
                                                                 ch=ch+"g";
                                                      }
                                                      else{
                                                                ch=ch+a1[i];
                                                     }
                                           }
                                           System.out.println(ch);
                              }
                      }
                   it will print Softgare.
Question:--
                            public class StrEx {
                                          public static void main(String[] args) {
                                                  StrEx g=new StrEx();
                                                  g.m(null);
                                         }
                                         public void m(String a){
                                                System.out.println(a);
                                        }
                                 }
             What would be the output?
Answer:  null.

Question:--null method overloading example
                            public class StrEx {
                                         public static void main(String[] args) {
                                                  m(null);
                                        }
                                        public static void m(String a){
                                                System.out.println("String");
                                       }
                                       public static void m(int a){
                                                System.out.println("int");
                                      }

                                      public static void m(Object o){     
                                               System.out.println("Object");
                                     }
                             }
             What would be the output?
Answer: String.

Question:-- null method overloading example--
                        public class StrEx {
                                   public static void main(String[] args) {
                                                  m(null);
                                   }
                                   public static void m(String a){
                                                System.out.println("String");
                                   }
                                   public static void m(int a){
                                                System.out.println("int");
                                   }
                                   public static void m(Object o){     
                                               System.out.println("Object");
                                   }
                                   public static void m(Integer i){     
                                               System.out.println("Integer");
                                   }
                           }
             What would be the output?

Answer:Compile-time error at m(null)

Question:-- Write a method  int Summation(String s). It will return summation of 
                  integers from  the   given string.
                   e.g  String s1="12 3 4 some text 3";
                             output should be summation of 12,3,4 & 3=22.
 Answer :--  
                         public int Summation(String s1){
                                     String[] s1=a.split(" ");
                                     int sum=0;
                                     for(int i=0;i<s1.length;i++){
                                           if(s1[i].matches("[0-9]+")){
                                                     sum=sum+Integer.parseInt(s1[i]);
                                           }
                                      }
                                      return sum;
                             }


Question: Write a program to find the length of the String without using the 
                   String method length.
 
   Answer:--             public class Slength {
                                           public int length(String s){
                                                   int i=0;
                                                   try{
                                                         for(i=0;i>=0;i++){
                                                               s.charAt(i);
                                                          }
                                                     }
                                                     catch(Exception e){
                                                              System.out.println("The length of the string ---");
                                                      }
                                             return i;
                                      }
                                      public static void main(String[] args) {
                                               Slength h = new Slength();
                                               int len = h.length("ghhjnnm");
                                               System.out.println(len);
                                      }
                                }

Question :--Write a program to count the letters from the given String.
Answer  :--
                      public class CountLetter {
                                  public static void countLetter(String s){
                                          if(s==null){
                                                return;
                                          }
                                          int counter=0;
                                          for(int i=0;i<s.length();i++){
                                                 if(Character.isLetter(s.charAt(i))){
                                                        counter++;
                                                }
                                          }
                                          System.out.println(counter);
                                  }

                                  public static void main(String[] args) {
                                          CountLetter.countLetter("ggg999");
                                 }
                        }

Question:--Write a program to check the given string is palindrome or not.
Answer:---
                import java.util.Scanner;
                   public class palindrome {
                             public static void main(String[] args) {
                                    Scanner sc=new Scanner(System.in);
                                    System.out.println("Enter a string");
                                    String str=sc.nextLine();
                                    int l=str.length();
                                    String reverse="";
                                    for(int i=l-1;i>=0;i--){
                                            reverse=reverse+str.charAt(i);
                                    }
                                    if(str.equals(reverse)){
                                            System.out.println("Palindrome");
                                    }
                                    else{
                                           System.out.println("Not palindrome");
                                   }
                             }
                      }

Question:-- What is output?
             public class test {
                     public static void main(String [] args) {
                              int x = 3;
                              int y = 1;
                              if (x = y)
                                    System.out.println("Not equal");
                             else
                                   System.out.println("Equal");
                   }
           }
Answer :- Compile Time Error at if (x = y).

Question:-- Does String is thread safe in java?
Answer :-- Strings are immutable, so we can’t change it’s value in program.
 Hence it’s thread-safe and can be safely used in multi-threaded environment.

Question:-- Can we use String in switch case?
 Answer:-- This is a tricky question used to check your knowledge of current Java developments. Java 7 extended the capability of switch case to use Strings also, earlier java versions doesn’t support this.
         If you are implementing conditional flow for Strings, you can use if-else conditions and you can use switch case if you are using Java 7 or higher versions.

Question:--Why String is immutable or final in Java?
Answer:-- In previous post already explained this, Please go through this link http://adnjavainterview.blogspot.in/2014/06/why-string-is-immutable-or-final-in-java.html

6 comments:

  1. Excellent
    Clear some miss understanding
    Thanks for sharing your knowledge.

    ReplyDelete
  2. Thanks for comment...If u have any questions or doubt please share it in the blog..

    ReplyDelete
  3. Very neat and clearly presented. Thanks for sharing.

    ReplyDelete
  4. perfect question and answers very use ful thank you very much

    ReplyDelete
  5. thanks for your comment. If you have any doubt please comment in the blog post or send email to anilnivargi49@gmail.com

    ReplyDelete
  6. Thank you very much for sharing your knowledge.It is usefully for me.

    ReplyDelete