Java Interview Programming Questions
to be continue...
How to reverse a string in java?
//Using StringBuffer class StringBuffer sbf = new StringBuffer("MyJava"); System.out.println(sbf.reverse()); //Output : avaJyM
//Using iterative method String str = "MyJava"; char[] strArray = str.toCharArray(); for (int i = strArray.length - 1; i >= 0; i--) { System.out.print(strArray[i]); //Output : avaJyM } //Using recursive method. static String recursiveMethod(String str) { if ((null == str) || (str.length() <= 1)) { return str; } return recursiveMethod(str.substring(1)) + str.charAt(0); } /* 1st Call —> recursiveMethod(“MyJava”) 2nd Call —> recursiveMethod(“yJava”) + “M” 3rd Call —> (recursiveMethod(“Java”) + “y”) + “M” 4th call —> ((recursiveMethod(“ava”) + “J”)+”y”) + “M” 5th Call —> (((recursiveMethod(“va”) + “a”) + “J”)+”y”) + “M” 6th Call —> ((((recursiveMethod(“a”) + “v”) + “a”) + “J”)+”y”) + “M” */
How to find duplicate characters in a string in java?
/* Problem : Write a java program to find duplicate characters and their count in a given string? For example, in a string “Better Butter”, duplicate characters and their count is t : 4, e : 3, r : 2 and B : 2. */ import java.util.HashMap; import java.util.Set; class DuplicateCharactersInString { static void duplicateCharCount(String inputString) { //Creating a HashMap containing char as key and it's occurrences as value HashMap<Character, Integer> charCountMap = new HashMap<Character, Integer>(); //Converting given string to char array char[] strArray = inputString.toCharArray(); //checking each char of strArray for (char c : strArray) { if(charCountMap.containsKey(c)) { //If char is present in charCountMap, incrementing it's count by 1 charCountMap.put(c, charCountMap.get(c)+1); } else { //If char is not present in charCountMap, //putting this char to charCountMap with 1 as it's value charCountMap.put(c, 1); } } //Getting a Set containing all keys of charCountMap Set charsInString = charCountMap.keySet(); System.out.println("Duplicate Characters In "+inputString); //Iterating through Set 'charsInString' for (Character ch : charsInString) { if(charCountMap.get(ch) > 1) { //If any char has a count of more than 1, printing it's count System.out.println(ch +" : "+ charCountMap.get(ch)); } } } public static void main(String[] args) { duplicateCharCount("JavaJ2EE"); duplicateCharCount("Fresh Fish"); duplicateCharCount("Better Butter"); } }
Ref:https://java2blog.com/java-interview-programs/
Ref:http://www.java2novice.com/java-interview-programs/
Ref:https://javaconceptoftheday.com/java-interview-programs-with-solutions/
Ref:https://www.edureka.co/blog/interview-questions/java-interview-questions/
Ref:https://www.google.com/search?q=java+interview+programming+questions
Ref:https://legacy.gitbook.com/book/lrh1993/android_interview_guide/details