Saturday, January 2, 2016

TOP 10 Java Program Interview Questions on String

1)  How Many Ways We can create String in Java?

Ans: There are Two Ways we can create String object. One is by using String literal or we can say that double quotes to create String object.The second way to create String object is that by using new operator same as we used to create normal java class.

Ex:  String str="lucky" ;// String literal
        String str=new String("lucky");//by using new operator

If we create String object by using String literal then it looks to store these object in the String Constant Pool(SCP),JVM checks if same String Value found in the String Constant Pool then it just returns the reference to that String object otherwise it creates new String object with the given value and stores into String Constant Pool.

If we create String object by using new operator JVM creates the String object and stores into heap memory but not store into String Constant Pool. Use intern()method  to Store String object into String Pool.

Read also: Strings in java

2) Can we use String in Switch Statement?Explain with Example?

Ans: From JDK7 we can use String for Switch Statement but before JDK6 version we can not use String for switch condition.

Ex:   switch(days){             //FOR USE ONLY FROM JDK7 
         case "January":     
           System.out.println("This is January month");
           break;
        case  "Feb":
         System.out.println("This is Feb month");  
         break;
        ----------------------------------
        -----------------------------------

       case "December": 
       System.out.println("This is December month");
       break;

      default:
      System.out.println("NOT FOUND");
}
                                      
3) How can you split a String with White space characters?

Ans: By using regular expressions we can easily split a String. \s stands for White space characters such as " ","\n","\t","\r".

Ex:  String strarray =st.spilt("\s+");

4) How to Convert String to Date in Java?

Ans:  By using Date class we can covert String to Date. In Date class we use SimpleDateFormat method.

Ex:   String str="02/01/2016";
         Date dt=new SimpleDateFormat("dd/mm/yyyy",Locale.English).parse(str);
         System.out.println(dt); // sat jan 02 00:00:00 IST 2016

5) Why we prefer char[] over String for Security Sensitive Information?

Ans: Obviously, We prefer char[] over string because String is immutable,that means once we have created a String,if another process can dump memory there is no way you can get rid of data before Garbage collection remove it from memory.
With an array, you can explicitly wipe the data after you are done it,you can overwrite the array with anything you like,and the password will not be present anywhere in the system before Garbage Collection.


6) How can you reverse a String Without Using String API?

Ans:     

public class ReverseStringDemo{  
public static void main(String args[])
{  String str="lucky";
String revstring="";
for(int i=str.length()-1;i>=0;i--)
{
revstring=revstring+str.charAt(i);
}
System.out.println(revstring);
}
}

Output: ykcul

Read also:  Difference between String,StringBuffer and StringBuilder in java
  

7) How can you tell String is final and Immutable in java?

Ans:  Strings are final in java because when  loading the class by using Classloader in java, Strings are Immutability that provides  security  to load the correct class. Since Strings are immutable it provides security while we use multi-threading concept and no need any Synchronization.

8) How can you write a Code to Sort the String in java?
Ans; 
public class SortStringTest{
public static void main(String args[])
{
String original="lucky";
char[] charArrays=original.toCharArray();
Arrays.sort(charArrays);
String sorted=new String(charArrays);
System.out.println(sorted);
}
}

Output: ckluy

9) can you explain how to check if two Strings equal in java?

Ans: We can check Strings are equal or not in Two ways. They are  == operator and equals()method in java.
When we use == operator to compare 2 objects. It checks to see if object refer to the same place in memory.

Ex:  
String st1=new String("lucky");
String st2=new String("lucky");
if(st1==st2)
{
System.out.println("True");
}
esle{
System.out.println("false");
}

output: False//because we are using == operator it checks only reference not values

10) How can you Write the code to check given no is Palindrome Or not?

Ans: 
class palindrome{
public static void main(String args[])
{
int num=Integer.parseInt(args[0]);
int s=num;//it is used at last time to check
int reverse=0,remainder;
while(num>0)
{
remainder=num%10;
reverse=reverse*10+remainder;
num=num/10;
}
if(num==s)
{
System.out.println(s+"is a palindrome number");
}
else
{
System.out.println(s+"is not a palindrome number");
}
}

Output: 

if you give input as 12345 then the output is not a palindrome number
if you give input as 121   then the output is palindrome number

Check the post to get more details on Strings: Strings in java

No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...