Most Asked JAVA Coding Programs Automation QA SDET 1740815758
Most Asked JAVA Coding Programs Automation QA SDET 1740815758
Programs
(Automation QA/SDET)
Prepared by
besant_technologies_headoffice
Most Asked SDET JAVA Programs
1.) Java program to Find Odd or Even number
import java.util.Scanner;
if (number % 2 == 0)
{ System.out.println(number + " is even.");
} else {
System.out.println(number + " is odd.");
}
} besant_technologies_headoffice
}
} else {
System.out.println(number + " is not a prime number.");
}
}
public staticscvoid
{ Scanner main(String[]
= new args)
Scanner(System.in);
System.out.println("enter number of terms");
int number = 6;
int first = 0, second = 1, next;
System.out.println("Fibonacci series is ");
for ( int i = 0; i<=number; i++)
{
System.out.println(first + "");
next = second+first;
first = second;
second = next; besant_technologies_headoffice
}
}
Output: 0112358
SwapNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
int a = 5,
System.out.print("Enter the second number: ");
int b = 10;
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);
}
}
}
}
Input: 5!
besant_technologies_headoffice
Output: 5! = 5*4*3*2*1 = 120
}
}
Input: 15786
Output: 68751
7.) Java program to find Armstrong Number
C. importjava.util.Scanner;
public class ArmstrongNumber {
}else{
} System.out.println("Not Armstrong number”);
}
} else if (no==0)
{ no=1;
}
while(no>0)
{
no=no/10;
a++;}
System.out.println("Number of digits in given number is :" +a); }
9.) Java program to find Palindrome number
import java.util.Scanner;
1001 is a palindrome.
10.) Java program to calculate the sum of digits of a number
sumOfDigits);
}
II. Output:
besant_technologies_headoffice
Strings
1.) Java program to reverse a string
import java.util.Scanner;
public class Test {
public static void main(String[] args)
{ Scanner scanner = new
Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
char ch;
String nstr = "";
for (int i = 0; i < input.length(); i++)
{ ch = input.charAt(i);
nstr = ch + nstr;
}
System.out.println("Reversed String is : " + nstr);
import java.util.HashMap;
import java.util.Set;
public class Main {
a : 4
g : 2
besant_technologies_headoffice
m : 2
n : 2
r : 3
4.) Java program to count Occurrences of Each Character in
String
import java.util.HashMap;
{ charCountMap.put(s,charCountMap.get(s)+1);
elsecharCountMap.put(s,1);
}
{
System.out.println("Count of Characters in a given string : " +
charCountMap);
}}
}
Count of Characters in a given string : {Java=1, Automation=2, Test=1}
besant_technologies_headoffice
6.) Java program to find all permutations of a given string
import java.util.Scanner;
} else {
for (int i = 0; i < str.length(); i++) {
String rem = str.substring(0,i) + str.substring(i+1);
} permute(rem,prefix + str.charAt(i));
}
}
}
abc
acb
bac
besant_technologies_headoffice
bca
cab
cba
7.) Java program to find if a string is Palindrome
import java.util.Scanner;
besant_technologies_headoffice
8.) Java program to determine if Two Strings are Anagrams
Vowels : 3
Consonants : 7
besant_technologies_headoffice
10.) Java program to print unqiue characters
import java.util.Scanner;
} besant_technologies_headoffice
}
J a v A u t o m i n
11.) Java program to print even indexed characters
import java.util.Scanner;
}
} besant_technologies_headoffice
import java.util.Scanner;
stringWithoutSpaces);
}
besant_technologies_headoffice
13.) Java program to print each letter twice from a given string
import java.util.Scanner;
}
return doubled.toString();
}
}
besant_technologies_headoffice
14.) Java program to swap two string without using 3 variable
rd
import java.util.Scanner;
besant_technologies_headoffice
15.) Java program to gives Output: a2b2c3d2 for the Input String
Str = “aabbcccdd”
import java.util.Scanner;
return result.toString();
}
}
import java.util.Scanner;
{ upperCase.append(ch);
} }
System.out.println("Output in lowercase: "+lowerCase);
System.out.println("Output in uppercase "+upperCase);
}
import java.util.Scanner;
besant_technologies_headoffice
Enter a string: Subbu123raj
import java.util.HashSet;
return maxLength;
}
}
Arrays
1.) Find common elements
between two arrays
import java.util.HashSet;
import java.util.Set;
public class CommonElements {
public static void main(String[] args)
{ int[] array1 = {1, 2, 3, 4, 5};
int[] array2 = {4, 5, 6, 7, 8};
Set<Integer> commonElements = findCommonElements(array1, array2);
System.out.println("Common elements: " + commonElements);
}
for {
(int num : array1)
set1.add(num);
}
return commonSet;
}
import java.util.ArrayList;
besant_technologies_headoffice
Fiursttpeulte:ment:Apple
O
System.out.println("Sorted array:");
forSystem.out.print(num
(int num : array) { + " ");
}
}
besant_technologies_headoffice
Output:
Sorted array:
12569
4.) Remove duplicates from an
Array
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args)
{ int[] array = {5, 2, 9, 1, 6, 2, 5};
int[] uniqueArray = removeDuplicates(array);
System.out.println("Array with duplicates removed:");
for (int num : uniqueArray) {
return result;
}
}
besant_technologies_headoffice
Output:
Array with duplicates removed:
12569
5.) Remove duplicates from an
ArrayList
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args)
{ ArrayList<Integer> arrayList = new ArrayList<>();
arrayList.add(5);
arrayList.add(2);
arrayList.add(9);
arrayList.add(1);
arrayList.add(6);
arrayList.add(2);
arrayList.add(5);
ArrayList<Integer> uniqueList =
removeDuplicates(arrayList);
besant_technologies_headoffice
Output:
ArrayList with duplicates removed:
12569
6.) Find the missing number in an
Array
int arraySum = 0;
for (int num : array)
{ arraySum += num;
}
return totalSum - arraySum;
}
}
Output:
The missing number is: 3
besant_technologies_headoffice
7.) Find the largest and smallest
element in an Array
public class Main {
besant_technologies_headoffice
Output:
Smallest element: 1
Largest element: 9
8.) Search element in an Array
public class Main {
public static void main(String[] args)
{ int[] array = {5, 2, 9, 1, 6, 3};
int target = 6;
int index = linearSearch(array, target);
ifSystem.out.println("Element
(index != -1) { " + target + " found at index: " +
index);
} else {
System.out.println("Element " + target + " not found in the
array.");
} }
Output:
Element 6 found at index: 4
Element 10 not found in the array
besant_technologies_headoffice
9.) Array consists of integers and
special
characters,sum only integers
besant_technologies_headoffice
Output:
Sum of integers in the array: 26
10.) Find Minimum and Maximum
from an Array
public class Main {
public static void main(String[] args)
{ int[] array = {5, 2, 9, 1, 6, 3};
// Find maximum and minimum int max
= findMaximum(array); int min =
findMinimum(array);
// Print the results
System.out.println("Minimum value in the array: " + min);
System.out.println("Maximum value in the array: " + max);
}
Output: besant_technologies_headoffice
besant_technologies_headoffice
Output :
Non-repeated elements: [3, 4]
Java program to implement
hashcode and equals
import java.util.Objects;
public class
Student
{ private int
id; private
String name;
// Constructor
public Student(int id, String name)
{ this.id = id;
this.name = name;
}
// hashCode method
@Override
public int hashCode() {
return Objects.hash(id, name);
}
// equals method
@Override
public boolean equals(Object
obj) { if (this == obj)
return true;
if (obj == null || getClass() !=
obj.getClass()) return false;
Student student = (Student) obj;
return id == student.id && Objects.equals(name, student.name);
}
besant_technologies_headoffice