5 Essential Java Coding Interview Questions and Answers for Senior Developers (part-3)
As a senior Java developer, you need to have a deep understanding of the language and be able to solve complex coding challenges. In this blog, we will look at five Java coding interview questions!
- Write a Java program to reverse a string. (not so hard!)
public static String reverseString(String str) {
StringBuilder sb = new StringBuilder(str);
return sb.reverse().toString();
}
Explanation: The reverseString
method takes a string as input, creates a StringBuilder
object with the string, and then calls the reverse()
method to reverse the characters in the string. Finally, it returns the reversed string.
2. Write a Java program to reverse a string without using the reverse()
method.
This can be a follow up question to the first question.
public static String reverseString(String str) {
char[] charArray = str.toCharArray();
int start = 0;
int end = charArray.length - 1;
while (start < end) {
char temp = charArray[start];
charArray[start] = charArray[end];
charArray[end] = temp;
start++;
end--;
}
return new String(charArray);
}