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 = [Link]() - 1; i >= 0; i--) {
reversedStr += [Link](i);
}
[Link]("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 = [Link]() - 1; i >= 0; i--) {
reversedStr += [Link](i);
}
if ([Link](reversedStr)) {
[Link](str + " is a palindrome.");
} else {
[Link](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 = [Link]();
for (int i = 0; i < [Link](); i++) {
char ch = [Link](i);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowels++;
} else if (ch >= 'a' && ch <= 'z') {
consonants++;
}
}
[Link]("Number of vowels: " + vowels);
[Link]("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;
}
[Link]("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);
[Link]("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;
[Link]("Before swapping: a = " + a + ", b = " + b);
a = a + b;
b = a - b;
a = a - b;
[Link]("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};
[Link]("Duplicate elements in the array are:");
for (int i = 0; i < [Link] - 1; i++) {
for (int j = i + 1; j < [Link]; j++) {
if (arr[i] == arr[j]) {
[Link](arr[j]);
}
}
}
}
}
8. Check for Anagrams
Question: How do you check if two strings are anagrams?
Solution:
Java
import [Link];
public class AnagramCheck {
public static void main(String[] args) {
String str1 = "listen";
String str2 = "silent";
char[] arr1 = [Link]();
char[] arr2 = [Link]();
[Link](arr1);
[Link](arr2);
if ([Link](arr1, arr2)) {
[Link]("The strings are anagrams.");
} else {
[Link]("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 [Link];
public class SecondLargest {
public static void main(String[] args) {
int[] arr = {1, 5, 2, 9, 3, 7};
[Link](arr);
[Link]("The second largest number is: " + arr[[Link] - 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;
[Link]("Fibonacci series: " + a + ", " + b);
for (int i = 2; i < n; i++) {
int c = a + b;
[Link](", " + 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 <= [Link](num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) {
[Link](num + " is a prime number.");
} else {
[Link](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;
}
[Link]("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 < [Link](); i++) {
if ([Link](i) == ch) {
count++;
}
}
[Link]("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 [Link];
import [Link];
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>([Link](arr));
[Link]("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;
}
[Link]("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 += [Link](digit, 3);
num /= 10;
}
if (originalNum == sum) {
[Link](originalNum + " is an Armstrong number.");
} else {
[Link](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 < [Link]; i++) {
if (arr[i] < min) {
min = arr[i];
}
if (arr[i] > max) {
max = arr[i];
}
}
[Link]("Smallest number: " + min);
[Link]("Largest number: " + max);
}
}
18. Sort an Array
Question: How do you sort an array in ascending order?
Solution:
Java
import [Link];
public class SortArray {
public static void main(String[] args) {
int[] arr = {5, 2, 8, 1, 9};
[Link](arr);
[Link]("Sorted array: " + [Link](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;
[Link]("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 = [Link]("\\s+");
[Link]("Number of words: " + [Link]);
}
}
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)
[Link](year + " is a leap year.");
else
[Link](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 : [Link]()) {
if ([Link](ch) == [Link](ch)) {
[Link]("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};
[Link]("Original array: ");
for (int i : arr) {
[Link](i + " ");
}
[Link]("\nReversed array: ");
for (int i = [Link] - 1; i >= 0; i--) {
[Link](arr[i] + " ");
}
}
}
24. Merge Two Arrays
Question: How do you merge two arrays?
Solution:
Java
import [Link];
import [Link];
public class MergeArrays {
public static void main(String[] args) {
int[] arr1 = {1, 2, 3};
int[] arr2 = {4, 5, 6};
int[] mergedArray = [Link]([Link](arr1), [Link](arr2))
.toArray();
[Link]("Merged array: " + [Link](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 ([Link](subStr)) {
[Link]("The string '" + mainStr + "' contains the substring '" + subStr + "'.");
} else {
[Link]("The string '" + mainStr + "' does not contain the substring '" + subStr +
"'.");
}
}
}