Java Interview Question Part - 1



Java coding examples

Question 1: Write a Java program to reverse a string without using any built-in methods or functions.

Solution: Here's one way to solve this problem:


public class StringReverse {
    public static void main(String[] args) {
        String original = "Hello, world!";
        String reversed = "";
        
        for (int i = original.length() - 1; i >= 0; i--) {
            reversed += original.charAt(i);
        }
        
        System.out.println("Original string: " + original);
        System.out.println("Reversed string: " + reversed);
    }
}

Explanation
  1. The program initializes a string with some value. 
  2. The reversed variable is initialized as an empty string.
  3.  'A' for loop is used to iterate over the characters of the original string in reverse order.
  4.  For each character, the program appends it to the reversed string using the += operator. 
  5. Finally, the program prints out both the original and reversed strings. 

Note: While this solution works for small strings, it's not efficient for large strings since the += operator creates a new string object for every concatenation. A better approach would be to use a StringBuilder object to efficiently append characters to the reversed string.

Question 2: Write a Java program to reverse a string.

Solution: Here's one way to solve this problem:

public class ReverseString {
    public static void main(String[] args) {
        String str = "Hello, world!";
        String reversed = "";
        
        for (int i = str.length() - 1; i >= 0; i--) {
            reversed += str.charAt(i);
        }
        
        System.out.println("Original string: " + str);
        System.out.println("Reversed string: " + reversed);
    }
}
Explanation:
  1. The program initializes a string with some value.
  2. The reversed variable is initialized to an empty string.
  3. A for loop is used to iterate over the characters of the string in reverse order, starting from the last character.
  4. For each character, the program appends it to the reversed string.
  5. Finally, the program prints out both the original and reversed strings.
Note: The above solution uses string concatenation with the += operator, which can be inefficient for large strings. An alternative approach is to use a StringBuilder object to build the reversed string more efficiently. 

For example:

public class ReverseString {
    public static void main(String[] args) {
        String str = "Hello, world!";
        StringBuilder reversed = new StringBuilder();
        
        for (int i = str.length() - 1; i >= 0; i--) {
            reversed.append(str.charAt(i));
        }
        
        System.out.println("Original string: " + str);
        System.out.println("Reversed string: " + reversed.toString());
    }
}


This solution is more efficient because it avoids the overhead of creating a new string object every time a character is appended. Instead, the StringBuilder object is used to build the string in-place, and the final reversed string is obtained by calling the toString() method.

Post a Comment

0 Comments