top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Beginner Friendly Java String Interview Questions

Hello Everyone! Welcome to the second section of the Java Strings blog. Here are some interesting coding questions that has been solved with different methods and approaches. “Better late than never!”- To make it more engaging, join me as we begin with some fundamentals for those who are extremely new to coding.



1) Getting Character From a String:


NOTE: I have used different methods for reference. Any one method can be followed.



Approaches:



public class GettingCharacterFromString {



 // --------------------Method 1------------------------------------



 public static void getCharacter(String s, int a) {



  System.out.println(s.charAt(a)); // --charAt method



 }



 // --------------------Method 2------------------------------------


 public static char getChar(String s1, int b) {



  System.out.println(s1.toCharArray()[b]);


  return s1.toCharArray()[b]; // toCharArray method



 }



 // --------------------Method 3------------------------------------


 public static char getCharStreams(String s2, int c) {



  System.out.println(s2.chars().mapToObj(ch -> (char) ch).toArray(Character[]::new)[c]);// Streamsmethod


  return s2.chars().mapToObj(ch -> (char) ch).toArray(Character[]::new)[c];



 }



//--------------------Method 4------------------------------------


 public static char getCharCodePoint(String s2, int c) {



  System.out.println((char) s2.codePointAt(c)); // codePoint


  return (char) s2.codePointAt(c);



 }



//--------------------Method 5------------------------------------


 public static char getCharChars(String s2, int c) {


  char[] a = new char[1];



  // getChars


  s2.getChars(c, c + 1, a, 0);


  return a[0];



 }



 public static void main(String[] args) {



  getCharacter("Geeks", 2);


  getChar("GeeksForGeeks", 5);


  getCharStreams("Java", 3);


  getCharCodePoint("Application", 1);


  char ch = getCharChars("Development", 2);


  System.out.println(ch);


 }



}


2) Replace Character at specific index


Approches:




public class ReplaceCharacterAtSpecificIndex {


 


// ---Method 1 using String builder


 


 public static void replaceChar(String str,char ch,int index) {


  


  System.out.println("Original String: "+str);


  


  StringBuilder sb=new StringBuilder(str);


  


  sb.setCharAt(index,ch);


  


  System.out.println("Modified String: "+sb);


  


 }



// ---Method 2 using String buffer


 


 


public static void replaceChar2(String str,char ch,int index) {


  


  System.out.println("Original String: "+str);


  


  StringBuffer sb=new StringBuffer(str);


  


  sb.setCharAt(index,ch);


  


  System.out.println("Modified String: "+sb);


  


 }



//Method 3 Substring



public static void replaceChar3(String str,char ch,int index) {


 


 System.out.println("Original String: "+str);


 


 String sb=str.substring(0,index)+ch+str.substring(index+1);


 


 System.out.println("Modified String: "+sb);


 


}



 public static void main(String[] args) {


  


                 


                 replaceChar("Geeks Gor Geeks",'F', 6);


                 replaceChar2("Testing",'h', 4);


                 replaceChar3("Selenium",'z', 2);


                 


 }



}


3) Reverse String


Approaches:



public class ReverseString {


 


 public static String revString(String str) {


  


  //---- Method 1---------------------------


  StringBuilder sb=new StringBuilder();


  for(int i=str.length()-1;i>=0;i--) {


   sb.append(str.charAt(i));


  }


  


  return sb.toString();


  


 }


 


//---- Method 2---------------------------


 public static String revString1(String str) {


         String nstr = "";


         char ch;


         for (int i = 0; i < str.length(); i++) {


             ch = str.charAt(i);


             nstr = ch + nstr;


         }


         return nstr;


     }


 




 public static void main(String[] args) {


  System.out.println(revString("Apple"));


  System.out.println(revString1("Mango"));



 }



}



4) String Palindrome


Approach:



public class StringPalindrome {



 


 public static boolean strPalindrome(String str) {


  str=str.toLowerCase();


  int start=0;


  int end=str.length()-1;


  


  while(start<end) {


   if(str.charAt(start)!=str.charAt(end)) {


    return false;


   }


   start++;


   end--;


  }


  return true;


 }



 public static void main(String[] args) {


  System.out.println("-----"+strPalindrome("anna"));


  System.out.println("-----"+strPalindrome("Madam"));


  System.out.println("-----"+strPalindrome("Apple"));


 }


}


5) String Pool and Equals Operator:


Approach:



public class StringPoolAndEqualsOperator {



 


 public static void main(String[] args) {


  String str1="Apple";


  String str2="Apple";


  String str3=new String("Apple"); //object created on heap and not in scp


  System.out.println(str1==str2); //true


  System.out.println(str1==str3); //false


  System.out.println(str1.equals(str3)); //true


 }


}


6) Reverse Words:


Approach:



public class ReverseWords {



 public static String revWords(String str) {


  


  StringBuilder sb=new StringBuilder();


  


  String[] words=str.split(" ");


  for(int i=words.length-1;i>=0;i--) {


   sb.append(words[i]).append(" ");


  }


  


  return sb.toString().trim();



 }



 public static void main(String[] args) {



  System.out.println("Reversed words: "+revWords("He is walking"));


  


 }



}



7) Remove Duplicates from String:


Approaches:



public class RemoveDuplicatesFromString {



//--------------------Method 1------------------------------------



 public static String findDuplicates(String str) {


  


  StringBuilder sb=new StringBuilder();


 for(char ch:str.toCharArray()) {


  if(!sb.toString().contains(String.valueOf(ch))) {


   sb.append(ch);


  }


 }


  return sb.toString();


 }



//--------------------Method 2------------------------------------


//set method--set removes duplicates


 public static String findUsingSet(String str) {


 StringBuilder sb=new StringBuilder();


 Set<Character>set=new LinkedHashSet<Character>();


 


 for(char ch:str.toCharArray()) {


  set.add(ch);


 }


 


 for(Character c:set) {


  sb.append(c);


 }


  


  return sb.toString();


 }



 public static void main(String[] args) {


  System.out.println(findDuplicates("hello"));


  System.out.println(findDuplicates("level"));


  System.out.println(findUsingSet("vilala"));


  


 }


}


8) Count Characters:


Approach:



public class CountCharacters {



   public static Map<Character, Integer> findCharacterCount(String str) {


        Map<Character, Integer> countMap = new HashMap<>();



        str = str.toLowerCase();


        for (char ch : str.toCharArray()) {


         if(ch!=' ') {


            countMap.put(ch, countMap.getOrDefault(ch,0)+1);


        }


        }


        return countMap;


    }



    public static void main(String[] args) {


        String text = "hi hello welcome";


        System.out.println(findCharacterCount(text));


    }


}


9) Word Occurences:


Approach:




public class FindWordOccurrences {



 public static Map<String, Integer> wordOccurences(String str) {


  str = str.toLowerCase();


  String[] text = str.split("\\s+");


  Map<String, Integer> countMap = new HashMap<>();



  for (String word : text) {


   countMap.put(word, countMap.getOrDefault(word, 0) + 1);


  }



  return countMap;



 }



 public static void main(String[] args) {



  String text = "We will always work and always we will be productive";


  System.out.println(wordOccurences(text));



 }



}


10) Remove Characters:


Approach:




public class RemoveChar {


 


     public static String removeChars(String str1, String str2) {


         StringBuilder result = new StringBuilder();



         for (char ch : str1.toCharArray()) {


             if (str2.indexOf(ch) == -1) {


                 result.append(ch);


             }


         }



         return result.toString();


     }



     public static void main(String[] args) {


         String firstString = "abcdef";


         String secondString = "bdh";



         String result = removeChars(firstString, secondString);


         System.out.println("Result: " + result);


     }


 }


Hope this blog will be helpful for the people who were very new to java or learning String concepts.

I will also try to write more general interview questions about Java concepts in upcoming blogs.

Thanks for reading.



59 views0 comments

Recent Posts

See All

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page