---Advertisement---

Java String Interview Question and Answers (Level-1)

By Manisha

Updated On:

---Advertisement---

 1. Reverse a String in Java

Problem Statement:
Write a Java program to reverse a given string using built-in methods.

Code Example:

java

public class StringReversal {

    public static void main(String[] args) {

        String original = “automation”;

        String reversed = new StringBuilder(original).reverse().toString();

        System.out.println(“Reversed: ” + reversed);

    }

}

Output:

makefile

Reversed: noitamotua

Explanation:
This program uses StringBuilder’s reverse() method which is efficient and avoids manual iteration. Ideal for interviews asking how to reverse a string in Java using built-in functions.


2. Palindrome Checker in Java

Problem Statement:
Check whether a given string is a palindrome (reads the same forward and backward).

Code Example:

java

public class PalindromeCheck {

    public static void main(String[] args) {

        String str = “madam”;

        boolean isPalindrome = true;

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

            if (str.charAt(i) != str.charAt(str.length() – i – 1)) {

                isPalindrome = false;

                break;

            }

        }

        System.out.println(str + ” is palindrome: ” + isPalindrome);

    }

}

Output:

csharp

madam is palindrome: true

Explanation:
This logic compares the characters from the start and end moving towards the center. It’s efficient and works in O(n) time.


3. Swap Two Numbers Without Temp Variable

Problem Statement:
Swap two integer variables without using a temporary variable.

Code Example:

java

public class SwapNumbers {

    public static void main(String[] args) {

        int a = 10, b = 20;

        System.out.println(“Before: a=” + a + “, b=” + b);

        a = a + b;

        b = a – b;

        a = a – b;

        System.out.println(“After: a=” + a + “, b=” + b);

    }

}

Output:

makefile

Before: a=10, b=20  

After: a=20, b=10

Explanation:
This technique uses arithmetic operations to swap values without a third variable. This is a commonly asked beginner logic question in coding interviews.


4. Finding the Largest Number in an Array

Problem Statement:
Find the largest number in a given integer array.

Code Example:

java

public class LargestInArray {

    public static void main(String[] args) {

        int[] numbers = {10, 5, 25, 8, 15, 3};

        int max = numbers[0];

        for (int i = 1; i < numbers.length; i++) {

            if (numbers[i] > max) {

                max = numbers[i];

            }

        }

        System.out.println(“Largest number: ” + max);

    }

}

Output:

yaml

Largest number: 25

Explanation:
The loop updates the maximum value as it encounters larger numbers. Simple yet fundamental for array-based interview questions.


5. Finding the Smallest Number in an Array

Problem Statement:
Write a Java program to find the minimum value in an integer array.

Code Example:

java

public class SmallestInArray {

    public static void main(String[] args) {

        int[] numbers = {10, 5, 25, 8, 15, 3};

        int min = numbers[0];

        for (int i = 1; i < numbers.length; i++) {

            if (numbers[i] < min) {

                min = numbers[i];

            }

        }

        System.out.println(“Smallest number: ” + min);

    }

}

Output:

yaml

Smallest number: 3

Explanation:
Just like finding the max, this solution checks each number and keeps the smallest. A classic problem for data structure interviews.


6. Count Vowels and Consonants in a String

Problem Statement:
Create a Java program to count vowels and consonants in a string.

Code Example:

java

public class VowelConsonantCounter {

    public static void main(String[] args) {

        String str = “Automation World”;

        str = str.toLowerCase();

        int vowels = 0, consonants = 0;

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

            char ch = str.charAt(i);

            if (ch >= ‘a’ && ch <= ‘z’) {

                if (“aeiou”.indexOf(ch) != -1) {

                    vowels++;

                } else {

                    consonants++;

                }

            }

        }

        System.out.println(“Vowels: ” + vowels + “, Consonants: ” + consonants);

    }

}

Output:

yaml

Vowels: 6, Consonants: 10

Question 7: Character Occurrence Counter in Java

Q: How do you count character occurrences in a string using Java?

A:
We can use a HashMap to store each character as a key and its count as a value.

Code Example:

java

import java.util.HashMap;

public class CharOccurrence {

  public static void main(String[] args) {

    String str = “automation”;

    HashMap<Character, Integer> charCount = new HashMap<>();

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

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

    }

    System.out.println(charCount);

  }

}

Output: {a=2, u=2, t=2, o=1, m=1, i=1, n=1}
Time Complexity: O(n)
Application: Useful in data analysis, string processing, and frequency counting.


Question 8: Print Fibonacci Series in Java

Q: How do you print the first N Fibonacci numbers?

Code Example:

java

public class Fibonacci {

  public static void main(String[] args) {

    int n = 10;

    int a = 0, b = 1;

    System.out.print(a + ” ” + b + ” “);

    for (int i = 2; i < n; i++) {

      int c = a + b;

      System.out.print(c + ” “);

      a = b;

      b = c;

    }

  }

}

Output: 0 1 1 2 3 5 8 13 21 34
Concept: Each number is the sum of the two preceding numbers.
Tip: Often asked in logic-based Java interviews.


Question 9: Factorial Calculation (Loop & Recursion)

Q: How do you calculate factorial in Java?

Code Example:

java

public class Factorial {

  public static void main(String[] args) {

    int num = 5;

    int factorial = 1;

    for (int i = 1; i <= num; i++) {

      factorial *= i;

    }

    System.out.println(“Factorial (loop): ” + factorial);

    System.out.println(“Factorial (recursive): ” + factorialRecursive(num));

  }

  public static int factorialRecursive(int n) {

    if (n == 0 || n == 1) return 1;

    return n * factorialRecursive(n – 1);

  }

}

Output: 120 (loop & recursive)
Loop and Recursion: Know both approaches for interviews.


Question 10: Prime Number Checker

Q: How to check if a number is prime in Java?

Code Example:

java

public class PrimeChecker {

  public static void main(String[] args) {

    int num = 17;

    boolean isPrime = num > 1;

    for (int i = 2; i <= Math.sqrt(num); i++) {

      if (num % i == 0) {

        isPrime = false;

        break;

      }

    }

    System.out.println(num + ” is prime: ” + isPrime);

  }

}

👉The Next 10 Questions-I: JAVA STRING

---Advertisement---

Leave a Comment