1 / 17

Java programs asked in interview for automation tester.

Here we have all the Java Programs for Automation Testing Interview and the solutions that are asked during Automation Testing Interview. We have mainly covered String, Factorial, Palindrome, Swap Number, Fibonacci Series, Prime number, Searching, Sorting, and Pattern Questions by which you can easily crack your Automation testing interview.

software14
Download Presentation

Java programs asked in interview for automation tester.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Software Quick Guide Knowledge is here where are you  Top 21 Java Programs for Automation Testing Interview Here we have all the Java Programs for Automation Testing Interview and the solutions that are asked during Automation Testing Interview. We have mainly covered String, Factorial, Palindrome, Swap Number, Fibonacci Series, Prime number, Searching, Sorting, and Pattern Questions by which you can easily crack your Automation testing interview. String Programs for Automation Testing Q1. How to reverse a string in java without using any function? Ans. Java 1 2 3 4 5 6 7 8 9 import java.util.Scanner; public class TestProgram { public static void main(String []ar){ Scanner scanner = new Scanner(System.in); System.out.println("Enter your String :-"); String st = scanner.nextLine(); int n = st.length(); System.out.print("Reverse Sting is :-"); for(int i=n-1;i>=0;i--){ System.out.print(st.charAt(i)); } } } 10 11 12 13 14 Output: Enter a String :- TESTING Reverse String is :- GNITSET Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com

  2. Q2. How to reverse a number in Java? Ans. Convert from int to string Java 1 2 3 4 5 6 7 8 9 import java.util.Scanner; public class TestProgram { public static void main(String []ar){ Scanner scanner = new Scanner(System.in); System.out.println("Enter your Number:-"); int num = scanner.nextInt(); String st = Integer.toString(num); int n = st.length(); System.out.print("Reverse Number is:-"); for(int i=n-1;i>=0;i--){ System.out.print(st.charAt(i)); } } } 10 11 12 13 14 15 Output: Enter a Number:-12345 Reverse Number is:-54321 using % operator Java 1 2 3 4 5 6 7 8 9 import java.util.Scanner; public class TestProgram { public static void main(String []ar){ Scanner scanner = new Scanner(System.in); System.out.println("Enter your Number :-"); int actualNumber = scanner.nextInt(); int reverse = 0; while(actualNumber!=0){ int rem = actualNumber % 10; reverse = reverse * 10 + rem; actualNumber = actualNumber/10; } System.out.println("Reverse Number is :-"+reverse); } } 10 11 12 13 14 15 16 Output: Enter your Number :-1234567 Reverse Number is :-7654321 Q3.Count the Occurrence of each character in a String Ans. Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com

  3. Java 1 2 3 4 5 6 7 8 9 Example 1 : class TestProgram { public static void main(String[] arg) { String str = "iamharry"; char arr[] = str.toCharArray(); HashMap<Character,Integer> hmap = new HashMap<Character,Integer>(); for(char cr:arr){ if(hmap.containsKey(cr)){ hmap.put(cr,hmap.get(cr)+1); }else{ hmap.put(cr,1); } } for(Map.Entry <Character,Integer>e:hmap.entrySet()){ System.out.println(e.getKey()+" : "+e.getValue()); } } } 10 11 12 13 14 15 16 17 18 19 20 21 22 Output: a : 2 i : 1 m : 1 h : 1 r : 2 y : 1 Example 2: import java.util.Scanner; public class EachCharacterCount { public static void main(String args[]) { String str; int i, len, counter[] = new int[256]; Scanner scanner = new Scanner(System.in); System.out.println("Please enter a string : "); str = scanner.nextLine(); len = str.length(); // loop through string and count frequency of every char and store it in counter array for (i = 0; i < len; i++) { counter[(int) str.charAt(i)]++; } // Print Frequency of characters for (i = 0; i < 256; i++) { if (counter[i] != 0) { System.out.println((char) i + " --> " + counter[i]); } } } } Output: Please enter a string : iamharry a : 2 i : 1 m : 1 h : 1 r : 2 y : 1 Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com

  4. Q4.Count the Occurrence of each word in a String Ans. class Demo{ public static void main(String[] args) { String str = "India is my country. India is very Nice and beautiful"; String arr[] = str.split(" "); HashMap hmap= new HashMap(); for(String st:arr){ if(hmap.containsKey(st)){ hmap.put(st,hmap.get(st)+1); }else{ hmap.put(st,1); } } for(Map.Entry e:hmap.entrySet()){ System.out.println(e.getKey()+" : "+e.getValue()); } } } Output: and : 1 beautiful : 1 very : 1 Nice : 1 is : 2 country. : 1 my : 1 India : 2 Q5.Remove duplicates from an integer array Ans. Example : class TestProgram { public static void main(String[] arg) { int[] arr={1,3,5,7,9,3,5,11,13,9}; List<Integer> ls = new ArrayList<Integer>(arr.length); for(int i:arr){ ls.add(i); } LinkedHashSet linkhs = new LinkedHashSet(ls); Object[] obj = linkhs.toArray(); System.out.println("Printing unique numbers:-"); for(Object o:obj){ System.out.print(o+ " "); } } } Output: Printing unique numbers:- 1 3 5 7 9 11 13 Example : class Demo { public static void main(String[] arg) { int[] arr = {1,3,5,7,9,11,13,3,5,7,15}; HashMap<Integer,Integer> hmap = new HashMap<Integer,Integer>(); Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com

  5. for(int arnum:arr){ if(hmap.containsKey(arnum)){ hmap.put(arnum,hmap.get(arnum)+1); }else{ hmap.put(arnum,1); } } for(Map.Entry <Integer,Integer> el:hmap.entrySet()){ if(el.getValue() < 2){ System.out.print(el.getKey()+" "); } } } } Output: 1 9 11 13 15 Q6. Remove duplicate string from string array Ans. import java.util.*; class TestProgram { public static void main(String[] args) { String[] arr = {"IND","SL","PAK","SL","NEP","PAK","BAN"}; HashMap<String,Integer> hm = new HashMap<String,Integer>(); for(String s:arr){ if(hm.containsKey(s)){ hm.put(s,hm.get(s)+1); }else{ hm.put(s,1); } } for(Map.Entry <String,Integer>e:hm.entrySet()){ if(e.getValue() < 2){ System.out.print(e.getKey()+" "); } } } } Output: NEP IND BAN Q7. Reverse words in a string array Ans. class TestDemo { public static void main(String[] args) { String[] arr = {"IND","SL","PAK","NEP","BAN"}; ArrayList<String> arrlist = new ArrayList<String>(arr.length); for(String str:arr){ arrlist.add(str); } Collections.reverse(arrlist); Object[] obj = arrlist.toArray(); for(Object o:obj){ System.out.print(o+" "); } } } Output: BAN NEP PAK SL IND Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com

  6. Q8. Reverse each word’s characters in string Ans. See alsoWhat Is Batch Testing In Software Testing | Full Detail Input: My Name is Sam | Output: yM emaN si maS public class TestProgram { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Original string : "); String originalStr = scanner.nextLine(); scanner.close(); String words[] = originalStr.split("\\s"); String reversedSt = ""; for (int i = 0; i < words.length; i++) { String w = words[i]; String reverseWord = ""; for (int j = w.length() - 1; j >= 0; j--) { reverseWord = reverseWord + w.charAt(j); } reversedSt = reversedSt + reverseWord + " "; } System.out.print("Reversed string : " + reversedSt); } } Output: Original string : My Name is Sam Reversed string : yM emaN si maS Q9. Reverse the words in string Ans. Input : java is very easy | Output : easy very is java public class Automation{ public static void main(String [] arg) { Scanner scanner = new Scanner(System.in); System.out.print("Original string : "); String originalStr = scanner.nextLine(); scanner.close(); String words[] = originalStr.split("\\s"); String reverseSt = ""; //Reverse each word's position for (int i = 0; i < words.length; i++) { if (i == words.length - 1) reverseSt = words[i] + reverseSt; else reverseSt = " " + words[i] + reverseSt; } System.out.print("Reversed string : " + reverseSt); } } Output: Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com

  7. Original string : java is very easy Reversed string : easy very is java Q10. Java program to count vowels and consonants in String Ans. public class TestProgram { public static void main(String[] arg) { Scanner scanner = new Scanner(System.in); System.out.print("Input string : "); String sn = scanner.nextLine(); scanner.close(); sn= sn.toLowerCase(); int vCount = 0, cCount = 0; for (int i = 0; i < sn.length(); i++) { if (sn.charAt(i) == 'a' || sn.charAt(i) == 'e' || sn.charAt(i) == 'i' || sn.charAt(i) == 'o' || sn.charAt(i) == 'u') { vCount++; } else if (sn.charAt(i) >= 'a' && sn.charAt(i) <= 'z') { cCount++; } } System.out.println("Number of vowels: " + vCount); System.out.println("Number of consonants: " + cCount); } } Output: Input string : Java Programming Number of vowels: 5 Number of consonants: 10 Q11. Remove extra white spaces between words Ans. Input: how to do in java. com Output: how to do in java. com public class TestClass{ public static void main(String[] ar) { String str= "how to do in java "; String nameWithSpacing = str.replaceAll("\\s+", " "); System.out.println( nameWithSpacing ); } Q12. Check if string is Palindrome or not Ans. See also70+ Top DevOps Interview Questions And Answers Using equals method Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com

  8. import java.util.*; class TestProgram { public static void main(String[] ar) { Scanner scanner = new Scanner(System.in); System.out.print("Please Enter your String:-"); String mainString = scanner.nextLine(); int n = mainString.length(); String reverse= ""; for(int i=n-1;i>=0;i--){ reverse=reverse+mainString.charAt(i); } if(mainString.equals(reverse)){ System.out.println("Given String is palindrome"); }else{ System.out.println("Given String is not palindrome"); } } } Output: Please Enter your String: dad String is palindrome Q13. Swap Number Ans. Swap two numbers without using third variable import java.util.Scanner; class TestProgram { public static void main(String[] ar) { int x=5,y=10; x=x+y; y=x-y; x=x-y; System.out.print("Values After Swapping two numbers= X:"+x+" Y:"+y); } } Output: Values After Swapping two numbers= X:10 Y:5 Swap two numbers using third variable import java.util.Scanner; class TestProgram { public static void main(String[] ar) { int x=5,y=10; int temp=x; x=y; y=temp; System.out.print("Values After Swapping two numbers= X:"+x+" Y:"+y); } } Output: Values After Swapping two numbers= X:10 Y:5 Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com

  9. Q14. Fibonacci Series Ans. Fibonacci series starting from 0 import java.util.Scanner; class TestProgram { public static void main(String[] args) { int a=0,b=1,c; for(int i=1;i<=10;i++){ System.out.print(a+" "); c=a+b; a=b; b=c; } } } Output: 0 1 1 2 3 5 8 13 21 34 Fibonacci series starting from 1 import java.util.Scanner; class TestProgram { public static void main(String[] ar) { int a=0,b=1,c; for(int i=1;i<=10;i++){ c=a+b; a=b; b=c; System.out.print(a+" "); } } } Output: 1 1 2 3 5 8 13 21 34 Q15.Factorial of a Number Ans. class TestProgram { public static void main(String[] ar) { Scanner sc = new Scanner(System.in); int fac=1; System.out.println("Enter Your Number:"); int num = sc.nextInt(); for(int i=1;i<=num;i++){ fac = fac*i; } System.out.print("Factorial of a Number:"+fac); } } Output: Enter Your Number:5 Factorial of a Number:120 Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com

  10. Q16.Check if a given number is prime number or not Ans. class TestProgram { public static void main(String[] ar) { Scanner sc = new Scanner(System.in); System.out.print("Enter Your Number:"); int number = sc.nextInt(); int m=number/2; int flag=0; if(number==0 || number==1){ System.out.print("Given number is not the prime number."); flag=1; }else{ for(int i=2;i<=m;i++){ if(number%i == 0){ System.out.print("Given number is not the prime number."); flag=1; break; } } } if(flag == 0){ System.out.print("Given number is a prime number."); } } } Output: Enter Your Number:23 Given number is a prime number. Q17. Write a Program to display the prime numbers from 1 to 100 Ans. public class TestProgram { public static void main(String[] args) { int a=1,b=100; for(int i=a;i<=b;i++){ if(checkPrime(i)){ System.out.print(i+" " ); } } } public static boolean checkPrime(int num){ // 0, 1 and negative numbers are not prime if(num<2){ return false; } else{ // no need to run loop till num-1 as for any number x the numbers in // the range(num/2 + 1, num) won't be divisible anyways. // Example 36 wont be divisible by anything b/w 19-35 int x= num/2; for(int i=2;i<x;i++){ if(num%i==0){ return false; } } } // the number would be prime if we reach here return true; } } Output: 2 3 4 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com

  11. Q18. Java Program to find Second Largest Number in an Array Ans. public class SecondLargestInArrayTest{ public static int getSecondLargest(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-2]; } public static void main(String args[]){ int a[]={13,24,54,62,52,21}; System.out.println("Second Largest: "+getSecondLargest(a,6)); } } Output: Second Largest: 54 Q19. Binary Search In Java: Ans. class BinarySearchExample{ public static void binarySearch(int arr[], int first, int last, int key){ int mid = (first + last)/2; while( first <= last ){ if ( arr[mid] < key ){ first = mid + 1; }else if ( arr[mid] == key ){ System.out.println("Element is found at index: " + mid); break; }else{ last = mid - 1; } mid = (first + last)/2; } if ( first > last ){ System.out.println("Element is not found!"); } } public static void main(String args[]){ int arr[] = {10,20,30,40,50,60}; int key = 40; int last=arr.length-1; binarySearch(arr,0,last,key); } } Output: Element is found at index: 3 Q20. Bubble Sort in Java Ans. Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com

  12. public class BubbleSortExample { static void bubbleSort(int[] arr) { int n = arr.length; int temp = 0; for(int i=0; i < n; i++){ for(int j=1; j < (n-i); j++){ if(arr[j-1] > arr[j]){ //swap elements temp = arr[j-1]; arr[j-1] = arr[j]; arr[j] = temp; } } } } public static void main(String[] args) { int arr[] ={3,60,35,2,45,320,5}; System.out.println("Array Before Bubble Sort"); for(int i=0; i < arr.length; i++){ System.out.print(arr[i] + " "); } System.out.println(); bubbleSort(arr);//sorting array elements using bubble sort System.out.println("Array After Bubble Sort"); for(int i=0; i < arr.length; i++){ System.out.print(arr[i] + " "); } } } Output: Array Before Bubble Sort 3 60 35 2 45 320 5 Array After Bubble Sort 2 3 5 35 45 60 320 Q21. Pattern Ans. See alsoTop 10 Automation Frameworks in 2023 Pattern Program-1 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 class TestProgram { public static void main(String[] args) { for(int i=1;i<=5;i++){ for(int j=1;j<=5;j++){ System.out.print(i+" "); } System.out.println(); } } } Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com

  13. Pattern Program-2 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 class TestProgram { public static void main(String[] args) { for(int i=1;i<=5;i++){ for(int j=1;j<=5;j++){ System.out.print(j+" "); } System.out.println(); } } } Pattern Program-3 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 class TestProgram { public static void main(String[] args) { for(int i=1;i<=5;i++){ for(int j=1;j<=i;j++){ System.out.print(j+" "); } System.out.println(); } } } Pattern Program-4 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 class TestProgram { public static void main(String[] args) { for(int i=1;i<=5;i++){ for(int j=1;j<=i;j++){ System.out.print(i+" "); } System.out.println(); } } } Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com

  14. Note: Thank You for Reading out the Content on Java Programs for Automation Testing. If you have any questions please feel free to comment below. PDF eBook Related Posts: Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com

  15. Manual Testing Interview Questions | Top Asked 32+ SAP Testing Interview Questions And Answers Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com

  16. Top 90 C++ Interview Questions And Answers 2023 10 Best Cross Browser Testing Tools of 2023 (Top Ranking) Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com

  17. List Of Interview Topics You Must Know Top 45+ WordPress Interview Questions And Answers  Search... Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com

More Related