Top 25 Java Programs for SDET / Automation Testing Interviews
1. Reverse a String
Question: How do you reverse a given string in Java?
Solution:
Java
public class ReverseString {
public static void main(String[] args) {
String str = "Automation";
String reversedStr = "";
for (int i = str.length() - 1; i >= 0; i--) {
reversedStr += str.charAt(i);
}
System.out.println("Reversed string is: " + reversedStr);
}
}
2. Palindrome Check
Question: How do you check if a string is a palindrome?
Solution:
Java
public class PalindromeCheck {
public static void main(String[] args) {
String str = "madam";
String reversedStr = "";
for (int i = str.length() - 1; i >= 0; i--) {
reversedStr += str.charAt(i);
}
if (str.equals(reversedStr)) {
System.out.println(str + " is a palindrome.");
} else {
System.out.println(str + " is not a palindrome.");
}
}
}
3. Count Vowels and Consonants
Question: How do you count the number of vowels and consonants in a string?
Solution:
Java
public class CountVowels {
public static void main(String[] args) {
String str = "Java Programming";
int vowels = 0, consonants = 0;
str = str.toLowerCase();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowels++;
} else if (ch >= 'a' && ch <= 'z') {
consonants++;
}
}
System.out.println("Number of vowels: " + vowels);
System.out.println("Number of consonants: " + consonants);
}
}
4. Reverse an Integer
Question: How do you reverse an integer in Java?
Solution:
Java
public class ReverseInteger {
public static void main(String[] args) {
int num = 12345;
int reversedNum = 0;
while (num != 0) {
int digit = num % 10;
reversedNum = reversedNum * 10 + digit;
num /= 10;
}
System.out.println("Reversed number is: " + reversedNum);
}
}
5. Find the Largest of Three Numbers
Question: How do you find the largest of three numbers?
Solution:
Java
public class LargestOfThree {
public static void main(String[] args) {
int a = 10, b = 25, c = 15;
int largest = (a > b) ? (a > c ? a : c) : (b > c ? b : c);
System.out.println("The largest number is: " + largest);
}
}
6. Swapping Two Numbers
Question: How do you swap two numbers without using a third variable?
Solution:
Java
public class SwapNumbers {
public static void main(String[] args) {
int a = 10, b = 20;
System.out.println("Before swapping: a = " + a + ", b = " + b);
a = a + b;
b = a - b;
a = a - b;
System.out.println("After swapping: a = " + a + ", b = " + b);
}
}
7. Find Duplicates in an Array
Question: How do you find duplicate elements in an array?
Solution:
Java
public class FindDuplicates {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 2, 7, 8, 8};
System.out.println("Duplicate elements in the array are:");
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] == arr[j]) {
System.out.println(arr[j]);
}
}
}
}
}
8. Check for Anagrams
Question: How do you check if two strings are anagrams?
Solution:
Java
import java.util.Arrays;
public class AnagramCheck {
public static void main(String[] args) {
String str1 = "listen";
String str2 = "silent";
char[] arr1 = str1.toCharArray();
char[] arr2 = str2.toCharArray();
Arrays.sort(arr1);
Arrays.sort(arr2);
if (Arrays.equals(arr1, arr2)) {
System.out.println("The strings are anagrams.");
} else {
System.out.println("The strings are not anagrams.");
}
}
}
9. Find the Second Largest Number in an Array
Question: How do you find the second largest number in an array?
Solution:
Java
import java.util.Arrays;
public class SecondLargest {
public static void main(String[] args) {
int[] arr = {1, 5, 2, 9, 3, 7};
Arrays.sort(arr);
System.out.println("The second largest number is: " + arr[arr.length - 2]);
}
}
10. Fibonacci Series
Question: How do you generate the Fibonacci series up to a given number?
Solution:
Java
public class FibonacciSeries {
public static void main(String[] args) {
int n = 10;
int a = 0, b = 1;
System.out.print("Fibonacci series: " + a + ", " + b);
for (int i = 2; i < n; i++) {
int c = a + b;
System.out.print(", " + c);
a = b;
b = c;
}
}
}
11. Prime Number Check
Question: How do you check if a number is prime?
Solution:
Java
public class PrimeCheck {
public static void main(String[] args) {
int num = 29;
boolean isPrime = true;
if (num <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) {
System.out.println(num + " is a prime number.");
} else {
System.out.println(num + " is not a prime number.");
}
}
}
12. Factorial of a Number
Question: How do you find the factorial of a number?
Solution:
Java
public class Factorial {
public static void main(String[] args) {
int num = 5;
long factorial = 1;
for (int i = 1; i <= num; ++i) {
factorial *= i;
}
System.out.println("Factorial of " + num + " is: " + factorial);
}
}
13. Count Occurrences of a Character
Question: How do you count the occurrences of a given character in a string?
Solution:
Java
public class CountChar {
public static void main(String[] args) {
String str = "programming";
char ch = 'g';
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ch) {
count++;
}
}
System.out.println("The character '" + ch + "' appears " + count + " times.");
}
}
14. Remove Duplicates from an Array
Question: How do you remove duplicate elements from an array?
Solution:
Java
import java.util.LinkedHashSet;
import java.util.Arrays;
public class RemoveDuplicates {
public static void main(String[] args) {
Integer[] arr = {1, 2, 3, 4, 2, 7, 8, 8};
LinkedHashSet<Integer> set = new LinkedHashSet<Integer>(Arrays.asList(arr));
System.out.println("Array with duplicates removed: " + set);
}
}
15. Find Sum of Digits
Question: How do you find the sum of digits of a number?
Solution:
Java
public class SumOfDigits {
public static void main(String[] args) {
int num = 12345;
int sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
System.out.println("The sum of digits is: " + sum);
}
}
16. Check Armstrong Number
Question: How do you check if a number is an Armstrong number?
Solution:
Java
public class ArmstrongNumber {
public static void main(String[] args) {
int num = 153;
int originalNum = num;
int sum = 0;
while (num > 0) {
int digit = num % 10;
sum += Math.pow(digit, 3);
num /= 10;
}
if (originalNum == sum) {
System.out.println(originalNum + " is an Armstrong number.");
} else {
System.out.println(originalNum + " is not an Armstrong number.");
}
}
}
17. Find the Smallest and Largest Number in an Array
Question: How do you find the smallest and largest numbers in an array?
Solution:
Java
public class MinMaxArray {
public static void main(String[] args) {
int[] arr = {10, 5, 25, 2, 30};
int min = arr[0];
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
}
if (arr[i] > max) {
max = arr[i];
}
}
System.out.println("Smallest number: " + min);
System.out.println("Largest number: " + max);
}
}
18. Sort an Array
Question: How do you sort an array in ascending order?
Solution:
Java
import java.util.Arrays;
public class SortArray {
public static void main(String[] args) {
int[] arr = {5, 2, 8, 1, 9};
Arrays.sort(arr);
System.out.println("Sorted array: " + Arrays.toString(arr));
}
}
19. Find Missing Number in an Array
Question: How do you find the missing number in an integer array from 1 to 10?
Solution:
Java
public class FindMissingNumber {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 6, 7, 8, 9, 10};
int n = 10;
int totalSum = n * (n + 1) / 2;
int arraySum = 0;
for (int num : arr) {
arraySum += num;
}
int missingNumber = totalSum - arraySum;
System.out.println("The missing number is: " + missingNumber);
}
}
20. Count Words in a String
Question: How do you count the number of words in a string?
Solution:
Java
public class CountWords {
public static void main(String[] args) {
String str = "Java is a programming language.";
String[] words = str.split("\\s+");
System.out.println("Number of words: " + words.length);
}
}
21. Check for a Leap Year
Question: How do you check if a year is a leap year?
Solution:
Java
public class LeapYear {
public static void main(String[] args) {
int year = 2024;
boolean isLeap = false;
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0)
isLeap = true;
else
isLeap = false;
} else
isLeap = true;
} else {
isLeap = false;
}
if (isLeap)
System.out.println(year + " is a leap year.");
else
System.out.println(year + " is not a leap year.");
}
}
22. Find the First Non-Repeated Character
Question: How do you find the first non-repeated character in a string?
Solution:
Java
public class FirstNonRepeatedChar {
public static void main(String[] args) {
String str = "automation";
for (char ch : str.toCharArray()) {
if (str.indexOf(ch) == str.lastIndexOf(ch)) {
System.out.println("First non-repeated character is: " + ch);
break;
}
}
}
}
23. Reverse an Array
Question: How do you reverse an array?
Solution:
Java
public class ReverseArray {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
System.out.print("Original array: ");
for (int i : arr) {
System.out.print(i + " ");
}
System.out.print("\nReversed array: ");
for (int i = arr.length - 1; i >= 0; i--) {
System.out.print(arr[i] + " ");
}
}
}
24. Merge Two Arrays
Question: How do you merge two arrays?
Solution:
Java
import java.util.Arrays;
import java.util.stream.IntStream;
public class MergeArrays {
public static void main(String[] args) {
int[] arr1 = {1, 2, 3};
int[] arr2 = {4, 5, 6};
int[] mergedArray = IntStream.concat(Arrays.stream(arr1), Arrays.stream(arr2))
.toArray();
System.out.println("Merged array: " + Arrays.toString(mergedArray));
}
}
25. Check for Substring
Question: How do you check if a string contains another substring?
Solution:
Java
public class SubstringCheck {
public static void main(String[] args) {
String mainStr = "Hello, world!";
String subStr = "world";
if (mainStr.contains(subStr)) {
System.out.println("The string '" + mainStr + "' contains the substring '" + subStr + "'.");
} else {
System.out.println("The string '" + mainStr + "' does not contain the substring '" + subStr +
"'.");
}
}
}