GroTechMinds

java programs interview questions & answers

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:

j15

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:

j2

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<name1.length();i++)
		{
			boolean k1=Character.isDigit(c1[i]);
			boolean k2=Character.isLetter(c1[i]);
			boolean k3=Character.isSpaceChar(c1[i]);
			if(k1==true)
			{
				count++;
			}
			if(k2==true)
			{
				letter++;
			}
			if(k3==true)
			{
				spaces++;
			}
			}
		System.out.println(count);
		System.out.println(letter);
		System.out.println(spaces);
		int special_characters=name1.length()-(count+letter+spaces);
		System.out.println(special_characters);
		}
}

				
			

Screenshot:

j3

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:

j4

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<number.length;i++)
		{
			if(check_value==number[i])
			{
				System.out.println("number is present " +i);
			}
		}
		
}
}

				
			

Screenshot:

j5

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<number.length;i++)
		{
			number1[i]=number[i];
		}
		System.out.println(Arrays.toString(number1));
		System.out.println(Arrays.toString(number));
		
}
}

				
			

Screenshot:

j6

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<number.length;i++)
		{
			number1[i]=number[i];
		}
		System.out.println(Arrays.toString(number1));
		System.out.println(Arrays.toString(number));
		boolean k=Arrays.equals(number, number1);
		if(k==true)
			{
			System.out.println("they are equal");
		}
		
}
}

				
			

Screenshot:

j7

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:

j8

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:

j9

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:

j10

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:

j11

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:

j12

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:

j14

13) Write a program on the third largest number.

In this program, we find the third largest number first by sorting elements, and the formula return a[total-3] we can find the largest number. In this formula total is the length of the elements and the number 3 will give us the third largest number.This way, we can see the largest number in the program.

Code Snippet:

				
					package practise;
public class ThirdLargestNumber {
	
public static int Thirdlargest(int [] a, int total)
	{
	int temp;
		for(int i=0; i<total;i++)
		{
			for(int j=i+1; j<total;j++)
			{
				if(a[i]>a[j])
				{
					temp = a[i];
					a[i] = a[j];
					a[j] = temp;
				}
			}
		}
		return a[total-3];//3rd largest number
	}
	public static void main(String[] args) {
		
		int [] a= {12,67,3,8,19,44,33};
		System.out.println(Thirdlargest(a,7));
		}
}

				
			

Screenshot:

j16

14) Write a program on the third smallest number.

In this program, we find the third smallest number first by sorting elements, and the formula return a[3-1]we can find the smallest number. This way, we can see the smallest number in the program.

Code Snippet:

				
					package practise;
public class ThirdSmallestNumber {
	public static int Thirdlargest(int [] a, int total)
	{
	int temp;
		for(int i=0; i<total;i++)
		{
			for(int j=i+1; j<total;j++)
			{
				if(a[i]>a[j])
				{
					temp = a[i];
					a[i] = a[j];
					a[j] = temp;
				}
			}
		}
		return a[3-1];//3rd smallest number
	}
	public static void main(String[] args) {
		
		int [] a= {12,67,3,8,19,44,33};
		System.out.println(Thirdlargest(a,7));
	}
}

				
			

Screenshot:

j15

15) Write a program on the Fibonacci Series.

In this program, we find the Fibonacci Series. The Fibonacci Sequence is a series of numbers where each number is the sum of the two preceding ones. In other words, in a Fibonacci series, the next number is the sum of the previous two numbers. It usually starts with 0 and 1. The sequence goes like this: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, and so on.

Code Snippet:

				
					package practise;
public class fibnoacci {
	public static void main(String[] args) {
		
		
		int n1=0;
		int n2=1;
		int n3;
		//System.out.println(n1 + " " +n2);
		for(int i=2;i<=10;i++)
		{
			
			n3=n1+n2;
			System.out.println(" " +n3);
			n1=n2;
			n2=n3;
		}
	}
}

				
			

Screenshot:

j15

16) Write a program where we find out all the substrings are present.

In this program, all the subsets of the string need to be printed. The subset of a string is the character or the group of characters that are present inside the string. For example, all possible subsets of the string “INDIA” will be I,In,Ind,Indi,India,n,nd,ndi,ndia,d,di,dia,i,ia,a.

Code Snippet:

				
					package practise;
public class SubStringsarepresent {
	public static void main(String[] args) {
		String name="India";
		
		for(int i=0;i<name.length();i++)
		{
			for (int j=i+1;j<=name.length();j++)
			{
				System.out.println(name.substring(i, j));
			}
		}
	}
}

				
			

Screenshot:

j16

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:

Upskill Yourself
Consult Us