Java Programming Interview Questions

Essential Java programming concepts and string manipulation problems

Showing 30 of 30 questions

Q1: Write a Java program to compare two strings?

Answer: Use equals() for case-sensitive comparison, equalsIgnoreCase() for case-insensitive, compareTo() for lexicographic comparison, and == for reference comparison.

Q2: Write a Java program to find all sub-strings of given string?

Answer: Use nested loops: for(int i=0; i<str.length(); i++) { for(int j=i+1; j<=str.length(); j++) { System.out.println(str.substring(i,j)); } }

Q3: Write a Java program to print string in reverse?

Answer: Use for loop: for(int i=length-1; i>=0; i--) { reverse = reverse + original.charAt(i); } or use StringBuilder.reverse().

Q4: How to find whether a String ends with specific character?

Answer: Use endsWith() method: str.endsWith("e") returns true if string ends with 'e'.

Q5: How to replace a string with another string?

Answer: Use replace() method: String newStr = s1.replace("Hello", "Welcome"); replaces all occurrences.

Q6: How to split a given string?

Answer: Use split() method: String[] parts = str.split(","); splits string by comma delimiter.

Q7: How to remove spaces before and after string?

Answer: Use trim() to remove leading/trailing spaces, or replaceAll("\\s", "") to remove all spaces.

Q8: How to convert string to lower case?

Answer: Use toLowerCase() method: String lower = str.toLowerCase(); converts entire string to lowercase.

Q9: How to find length of string?

Answer: Use length() method: int len = str.length(); returns number of characters in string.

Q10: How to concatenate strings?

Answer: Use concat() method: str1.concat(str2) or + operator: str1 + str2 for string concatenation.

Q11: How to convert string to integer?

Answer: Use Integer.parseInt(): int i = Integer.parseInt("300"); converts string to int primitive.

Q12: How to convert integer to string?

Answer: Use Integer.toString(): String s = Integer.toString(200); or String.valueOf(): String s = String.valueOf(200).

Q13: How to convert string to long?

Answer: Use Long.parseLong(): long l = Long.parseLong("88898678765"); converts string to long.

Q14: How to convert string to float?

Answer: Use Float.parseFloat(): float f = Float.parseFloat("56.6"); converts string to float.

Q15: How to convert string to double?

Answer: Use Double.parseDouble(): double d = Double.parseDouble("56.6"); converts string to double.

Q16: How to convert string to date?

Answer: Use SimpleDateFormat: Date date = new SimpleDateFormat("dd/MM/yyyy").parse("31/12/1998"); or LocalDate.parse() for modern approach.

Q17: Check whether string is anagram of another?

Answer: Convert both strings to char arrays, sort them, and compare: Arrays.sort(arr1); Arrays.sort(arr2); return Arrays.equals(arr1, arr2);

Q18: How to convert string to upper case?

Answer: Use toUpperCase() method: String upper = str.toUpperCase(); converts entire string to uppercase.

Q19: How to print occurrence of each character?

Answer: Use HashMap: for each character, if map contains key increment count, else put with count 1.

Q20: How to reverse words in string? Input: I Love India, Output: India Love I

Answer: Split string by space, iterate from last word to first: String[] words = str.split(" "); for(int i=words.length-1; i>=0; i--) print(words[i]);

Q21: How to reverse words and characters? Input: i love java, Output: java evol i

Answer: Exchange first and last words, reverse middle words: complex algorithm involving string manipulation and character reversal.

Q22: How to reverse comma-separated values? Input: abc,bbc,ccd, Output: ccd,bbc,abc

Answer: Split by comma, reverse array order, join back: String[] parts = str.split(","); reverse array; join with comma.

Q23: How to find string length without length() method?

Answer: Use enhanced for loop: int count=0; for(char c : str.toCharArray()) count++; return count;

Q24: How to count specific character in string?

Answer: Loop through string: int count=0; for(int i=0; i<str.length(); i++) if(str.charAt(i)==targetChar) count++;

Q25: How to extract numbers from string? Input: BSG234JGJH, Output: 234

Answer: Use Character.isDigit(): StringBuilder sb = new StringBuilder(); for(char c : str.toCharArray()) if(Character.isDigit(c)) sb.append(c);

Q26: How to remove duplicate adjacent characters recursively?

Answer: Recursively remove adjacent duplicates: check each character with next, if same remove both, continue until no more adjacent duplicates.

Q27: How to remove repeated characters? Input: Java, Output: Jav

Answer: Check each character against previous characters: for(int i=0; i<n; i++) { for(int j=0; j<i; j++) if(str[i]==str[j]) break; if(j==i) result[index++]=str[i]; }

Q28: How to remove repeated words from sentence?

Answer: Split into words, compare each word with others, set duplicates to null: if(words[i].equals(words[j])) words[j]=null;

Q29: How to check if character is present in string?

Answer: Use contains() method: boolean present = str.contains("a"); returns true if character/substring exists.

Q30: How to count occurrences of substring in string?

Answer: Split by substring and count parts: String[] parts = str.split("test"); int count = parts.length - 1; (if no trailing matches)