Java Programs for Automation Testers
1) Write a program on how to reverse a string value.
We can reverse String using the function charAt() as it will return char datatype we declared reverse as it will return String datatype.
CodeSnippet:
package strings_assisgnment;
public class Reverse{
public static void main(String[] args) {
String name1="Alex";
String reverse="";
for(int i=name1.length()-1;i>=0;i--)
{
char c= name1.charAt(i);
reverse=reverse+c;
}
System.out.println(reverse);
}
}
Screenshot:
2) Write a program to check if the given string is palindrome.
Palindrome means a word that reads the same as ‘mom’.
Code Snippet:
package strings_assisgnment;
public class Programs {
public static void main(String[] args) {
String name1="mom";
String reverse="";
for(int i=name1.length()-1;i>=0;i--)
{
char c= name1.charAt(i);
reverse=reverse+c;
}
System.out.println(reverse);
boolean g=name1.equals(reverse);
if(g==true)
{
System.out.println("palindrome");
}
}
}
Screenshot:
3) Write a program on a given string to determine how many numeric, letters, digits, spaces, and special characters are present.
We can count numbers, letters, spaces, and special characters in a given String.
Code Snippet:
package strings_assisgnment;
import java. util.Arrays;
public class Programs {
static int count =0;
static int letter =0;
static int spaces =0;
public static void main(String[] args) {
String name1="my name is Alex 1234 @#$%^";
System.out.println(name1.length());
char c1[]=name1.toCharArray();
System.out.println(Arrays.toString(c1));
for(int i=0;i
Screenshot:
4) Write a program to find out the sum and average value of 4 numbers in an array of int data type.
We can find the sum and average by declaring them globally and initializing them with a value of 0. We can then add a number at the indexing position of i.
Code Snippet:
package strings_assisgnment;
public class Programs {
static double sum=0;
static double avg=0;
public static void main(String[] args) {
int number[]=new int[4];
number [0]=65;
number [1]=75;
number [2]=85;
number [3]=90;
for(int i=0;i<4;i++)
{
sum=sum+number[i];
}
System.out.println(sum);
avg=sum/number.length;
System.out.println(avg);
}
}
Screenshot:
5) Write a program to Check whether the number 15 is present in a given array of size 4 and int datatype.
We have declared check_value globally and do a comparison with the number in Arrays and find the indexing position by printing the indexing position at i.
Code Snippet:
package strings_assisgnment;
public class Programs {
static int check_value=15;
public static void main(String[] args) {
int number[]=new int[4];
number [0]=65;
number [1]=15;
number [2]=85;
number [3]=90;
for(int i=0;i
Screenshot:
6) Write a program on a copy of one array into another array using iteration.
We can copy one array into another by copying one array into another with indexing position i and using the assignment operator.
Code Snippet:
package strings_assisgnment;
import java.util.Arrays;
public class Programs {
public static void main(String[] args) {
int number[]=new int[4];
number [0]=65;
number [1]=15;
number [2]=85;
number [3]=90;
int number1[]=new int[number.length];
for(int i=0;i
Screenshot:
7) Write a program to check that both the arrays are equal or not.
First, we will copy one array into another by copying one array into another with indexing position i and using the assignment operator. And use Arrays.equals function to check whether both the Strings are equal or not.
Code Snippet:
package strings_assisgnment;
import java.util.Arrays;
public class Programs {
public static void main(String[] args) {
int number[]={65,15,85,90};
int number1[]=new int[number.length];
for(int i=0;i
Screenshot:
8) Write a program on the reverse of the Array and store it in another Array using a for loop.
We will first reverse the array and then we will print both the Arrays sung Arrays.toString function.
Code Snippet:
package strings_assisgnment;
import java.util.Arrays;
public class Programs {
static int k=0;
public static void main(String[] args) {
int number[]={65,15,85,90};
int number1[]=new int[number.length];
for(int i=number.length-1;i>=0;i--)
{
number1[i]=number[k];
k++;
}
System.out.println(Arrays.toString(number1));
System.out.println(Arrays.toString(number));
}
}
Screenshot:
9) Write a program to check the given two strings are anagrams of each other.
 Anagram means a word or phrase that is made by arranging the letters of another word or phrase in different order. In this program first, we will use toCharArray to change Strings into Arrays and then we will do sorting by using the Arrays. sort method which will sort Arrays ascending and then we can print using the Arrays.toString method.
Code Snippet:
package strings_assisgnment;
import java.util.Arrays;
public class Programs {
public static void main(String[] args) {
String name="state";
String name1="taste";
char[] c= name.toCharArray();
char[] c1= name1.toCharArray();
Arrays.sort(c1);
Arrays.sort(c);
System.out.println(Arrays.toString(c1));
System.out.println(Arrays.toString(c));
boolean h=Arrays.equals(c, c1);
if(h==true)
{
System.out.println("the given two strings are anagram");
}
else
{
System.out.println("the given two strings are not anagram");
}
}
}
Screenshot:
10) Write a program to check the given two strings are not anagrams of each other.
To check whether two strings are anagrams or not by using two Strings with the same size but different characters.
Code Snippet:
package strings_assisgnment;
import java.util.Arrays;
public class Programs {
public static void main(String[] args) {
String name="waste";
String name1="taste";
char[] c= name.toCharArray();
char[] c1= name1.toCharArray();
Arrays.sort(c1);
Arrays.sort(c);
System.out.println(Arrays.toString(c1));
System.out.println(Arrays.toString(c));
boolean h=Arrays.equals(c, c1);
if(h==true)
{
System.out.println("the given two strings are anagram");
}
else
{
System.out.println("the given two strings are not anagram");
}
}
}
Screenshot:
11) Write a program on method overloading static method.
Method overloading in the static method is possible by using parametrized methods and we call methods directly in the main method.
Code Snippet:
package strings_assisgnment;
public class Methodoverloading {
public static void add()
{
int a=10;
int b=20;
int sum=a+b;
System.out.println(sum);
}
public static void add(int k)//Parameterized method
{
int c=20;
int d=10;
int sub=c-d;
System.out.println(sub);
}
public static void main(String[] args) {
add();
add(10);
}
}
Screenshot:
12) Write a program on method overloading nonstatic method.
Method overloading in the nonstatic method is possible by using parametrized methods and we invoke constructors and call nonstatic methods in the main method.
Code Snippet:
package strings_assisgnment;
public class methodoverloading {
public void add()
{
int a=10;
int b=20;
int sum=a+b;
System.out.println(sum);
}
public void add(int k)//Parameterized method
{
int c=20;
int d=10;
int sub=c-d;
System.out.println(sub);
}
public static void main(String[] args) {
Methodoverloading m1= new Methodoverloading();//creating object
m1.add();
m1.add(10);
}
}
Screenshot:
13) Write a program on constructor method overloading.
Method overloading in the constructor method is possible by using parametrized methods and we invoke constructors and call nonstatic methods in the main method. Constructors don’t have any return type. The constructor name will always be the same as the class name.
Code Snippet:
package overloading;
public class MethodOverloading {
MethodOverloading()
{
int a=50;
int b=40;
int sum=a+b;
System.out.println(sum);
}
MethodOverloading(int c)
{
int d=50;
int e=40;
int sub=d-e;
System.out.println(sub);
}
public static void main(String[] args) {
MethodOverloading m1= new MethodOverloading();// 1st way of creating an object
new MethodOverloading(10);// 2nd way of creating an object
}
}
Screenshot:
Conclusion:
In conclusion, by familiarizing yourself with these Java programs Interview Questions and Answers, you are better prepared to showcase your knowledge and skills to potential employers. Remember to practice, stay updated with the latest trends in Java with Selenium, and maintain a positive attitude throughout your interview process. With dedication and perseverance, you can ace any Java interview and secure your dream job.Â
Also Read Blogs On:
Consult Us