
Top 20 Java Programming Interview Questions for SDET
1. Write a Java program to find the second most frequent character in string “DevLabsAlliance”.
package com.dla; public class SecondFrequentCharacter { static final int NO_OF_CHARS = 256; public static void main(String[] args) { //String to find second frequent character String str = "DevLabsAlliance"; char res = getSecondMostFreq(str); if (res != '\0') System.out.println("Second most frequent char is " + res); else System.out.println("No second most frequent character"); } static char getSecondMostFreq(String str) { //count number of occurences of every character int[] count = new int[NO_OF_CHARS]; int i; for (i=0; i< str.length(); i++) (count[str.charAt(i)])++; // Traverse through the count[] and find second highest element. int first = 0, second = 0; for (i = 0; i < NO_OF_CHARS; i++) { /* If current element is smaller than first then update both first and second */ if (count[i] > count[first]) { second = first; first = i; } /* If count[i] is in between first and second then update second */ else if (count[i] > count[second] && count[i] != count[first]) second = i; } return (char)second; } }
// Output
Second most frequent char is L
2. Write a Java program to Reverse a String.
package com.dla; public class ReverseAString { public static void main(String[] args) { String s = "DevLabsAlliance"; String revS = ""; for(int i= s.length() - 1; i>=0; i-- ) { revS = revS + s.charAt(i); } System.out.println("The reversed string is "+ revS); } }
// Output
The reversed string is ecnaillAsbaLveD
3. Write a Java program to reverse the order of words in a string.
package com.dla; import java.util.regex.Pattern; public class ReverseWordsInString { public static void main(String[] args) { String str = "Learn Lead And Succeed in DevLabsAlliance"; Pattern p = Pattern.compile("\\s"); String[] temp = p.split(str); String rev = ""; for (int i = 0; i < temp.length; i++) { if (i == temp.length - 1) rev = temp[i] + rev; else rev = " " + temp[i] + rev; } System.out.println("The reversed string is: " + rev); } }
// Output
The reversed string is: DevLabsAlliance in Succeed And Lead Learn
4. Write a Java program to find the frequency of character in a string.
package com.dla; public class FrequencyOfCharacter { public static void main(String[] args) { String str = "DevLabs Alliance is awesome."; char ch = 'e'; int frequency = 0; for(int i = 0; i < str.length(); i++) { if(ch == str.charAt(i)) { ++frequency; } } System.out.println("Frequency of " + ch + " = " + frequency); } }
// Output
Frequency of e = 4
5. Write a Java program to sort Strings in Lexicographical(Dictionary) order.
package com.dla; public class SortStringInDictionaryOrder { public static void main(String[] args) { String[] words = { "Ruby", "C", "Python", "Java" }; for(int i = 0; i < 3; ++i) { for (int j = i + 1; j < 4; ++j) { if (words[i].compareTo(words[j]) > 0) { // swap words[i] with words[j] String temp = words[i]; words[i] = words[j]; words[j] = temp; } } } System.out.println("In lexicographical order:"); for(int i = 0; i < 4; i++) { System.out.println(words[i]); } } }
// Output
In lexicographical order:
C
Java
Python
Ruby
6. Write a Java program to replace the SubString with another string.
package com.dla; public class ReplaceSubStringWithAnotherString {\ public static void main(String[] args) { String str = "Learn, Lead and Succeed in DevLabsAlliance"; String toBeReplaced = "in"; String toReplacedWith = "with"; String[] astr = str.split(toBeReplaced); StringBuffer strb = new StringBuffer(); for ( int i = 0; i <= astr.length-1; i++ ) { strb = strb.append( astr[i] ); if (i != astr.length-1) { strb = strb.append(toReplacedWith); } } System.out.println(strb); } }
// Output
Learn, Lead and Succeed with DevLabsAlliance
7. Write a Java program to find the largest value from the given array.
package com.dla; public class LargestNumberInArray { public static void main(String[] args) { int[] arr={28,3,15,9,17,4,23,2}; int val=arr[0]; for(int i=0; i<arr.length; i++){ if(arr[i] > val){ val=arr[i]; } } System.out.println("Largest value in the Given Array is "+ val); } }
// Output
Largest value in the Given Array is 28
8. Write a Java program to count the number of words in a string using HashMap.
package com.dla; import java.util.HashMap; public class WordCount { public static void main(String[] args) { String str = "Training Training course and certification course in Devlabs Alliance"; String[] split = str.split(" "); HashMap<String, Integer> map = new HashMap<String, Integer>(); for (int i = 0; i < split.length - 1; i++) { if (map.containsKey(split[i])) { int count = map.get(split[i]); map.put(split[i], count + 1); } else { map.put(split[i], 1); } } System.out.println(map); } }
// Output
{Training=2, in=1, and=1, course=2, Devlabs=1, certification=1}
9. Write a Java program to find whether a string is palindrome or not.
package com.dla; public class Palindrome { public static void main(String[] args) { String original = "nitin", reverse = ""; int length; length = original.length(); for (int i = length - 1; i >= 0; i--) { reverse = reverse + original.charAt(i); } if (original.equals(reverse)) System.out.println("The string is palindrome"); else System.out.println("The string is not a palindrome"); } }
// Output
The string is palindrome
10. Write a java program to find the duplicate characters in a string.
package com.dla; public class DuplicateCharacters { public static void main(String[] args) { String str = new String("DevLabsAlliance"); int count = 0; char[] chars = str.toCharArray(); System.out.println("Duplicate characters are:"); for (int i = 0; i < str.length(); i++) { for (int j = i + 1; j < str.length(); j++) { if (chars[i] == chars[j]) { System.out.println(chars[j]); count++; break; } } } } }
11. Write a Java program to find the second highest number in an array.
package com.dla; public class SecondHighestNumberInArray { public static void main(String[] args) { int arr[] = { 14, 46, 47, 94, 94, 52, 86, 36, 94, 89 }; int largest = arr[0]; int secondLargest = arr[0]; for (int i = 0; i < arr.length; i++) { if (arr[i] > largest) { secondLargest = largest; largest = arr[i]; } else if (arr[i] > secondLargest && arr[i] != largest) { secondLargest = arr[i]; } } System.out.println("Second largest number in an array is:" + secondLargest); } }
// Output
Second largest number in an array is:89
12. Write a Java Program to create pyramid of numbers with 5 rows
package com.dla; public class PyramidWith5Rows { public static void main(String[] args) { int noOfRows = 5; // Initializing rowCount with 1 int rowCount = 1; System.out.println("Here Is Your Pyramid"); // Implementing the logic for (int i = noOfRows; i > 0; i--) { // Printing i spaces at the beginning of each row for (int j = 1; j <= i; j++) { System.out.print(" "); } // Printing 'rowCount' value 'rowCount' times at the end of each row for (int j = 1; j <= rowCount; j++) { System.out.print(rowCount + " "); } System.out.println(); // Incrementing the rowCount rowCount++; } } }
// Output
Here Is Your Pyramid
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
13. Write a Java program to count occurrences of each character in a string.
package com.dla; public class EachCharCountInString { static final int MAX_CHAR = 256; public static void main(String[] args) { // Create an array of size 256 i.e. ASCII_SIZE int count[] = new int[MAX_CHAR]; String str = "devlabsalliance"; int len = str.length(); // Initialize count array index for (int i = 0; i < len; i++) count[str.charAt(i)]++; // Create an array of given String size char ch[] = new char[str.length()]; for (int i = 0; i < len; i++) { ch[i] = str.charAt(i); int find = 0; for (int j = 0; j <= i; j++) { // If any matches found if (str.charAt(i) == ch[j]) find++; } if (find == 1) System.out.println("Number of Occurrence of " + str.charAt(i) + " is:" + count[str.charAt(i)]); } } }
// Output
Number of Occurrence of d is:1
Number of Occurrence of e is:2
Number of Occurrence of v is:1
Number of Occurrence of l is:3
Number of Occurrence of a is:3
Number of Occurrence of b is:1
Number of Occurrence of s is:1
Number of Occurrence of i is:1
Number of Occurrence of n is:1
Number of Occurrence of c is:1
14. Write a Java program to move all zeros to the end of array.
package com.dla; public class pushZerosToEnd { public static void main(String[] args) { int arr[] = { 1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9 }; int n = arr.length; int count = 0; // Count of non-zero elements // Traverse the array. If element encountered is // non-zero, then replace the element at index 'count' // with this element for (int i = 0; i < n; i++) if (arr[i] != 0) arr[count++] = arr[i]; // Now all non-zero elements have been shifted to // front and 'count' is set as index of first 0. // Make all elements 0 from count to end. while (count < n) arr[count++] = 0; System.out.println("Array after pushing zeros to the end: "); for (int i = 0; i < n; i++) System.out.print(arr[i] + " "); } }
// Output
Array after pushing zeros to the end:
1 9 8 4 2 7 6 9 0 0 0 0
15. Write a Java program to print Floyd’s triangle with 5 rows.
package com.dla; public class FloydTriangleWith5Rows { public static void main(String args[]) { int rows = 5; int number = 1; for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { System.out.print(number + " "); number++; } System.out.println(); } } }
// Output
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16. Write a Java program to print all prime factors of a given number.
package com.dla; public class AllPrimeFactors { public static void main(String[] args) { int n = 315; // Print the number of 2s that divide n while (n % 2 == 0) { System.out.print(2 + " "); n /= 2; } // n must be odd at this point. So we can // skip one element (Note i = i +2) for (int i = 3; i <= Math.sqrt(n); i += 2) { // While i divides n, print i and divide n while (n % i == 0) { System.out.print(i + " "); n /= i; } } // This condition is to handle the case when // n is a prime number greater than 2 if (n > 2) System.out.print(n); } }
// Output
3 3 5 7
17. Write a Java program to print all prime numbers till 15.
package com.dla; public class PrimeNumberTill15 { public static void main(String args[]) { int maxNum = 15; // printing primer numbers till the limit ( 1 to 100) System.out.println("Printing prime number from 1 to " + maxNum); for (int number = 2; number <= maxNum; number++) { // print prime numbers only if (isPrime(number)) { System.out.println(number); } } } /* * Prime number is not divisible by any number other than 1 and itself * * @return true if number is prime */ public static boolean isPrime(int number) { for (int i = 2; i < number; i++) { if (number % i == 0) { return false; // number is divisible so its not prime } } return true; // number is prime now } }
// Output
Printing prime number from 1 to 15
2
3
5
7
11
13
18. Write a Java program to reverse digits of an integer.
package com.dla; public class ReverseDigits { public static void main(String args[]) { int num = 3689; int rev_num = 0; while (num > 0) { rev_num = rev_num * 10 + num % 10; num = num / 10; } System.out.println("Reverse of no. is " + rev_num); } }
// Output
Reverse of no. is 9863
19. Write a Java program to remove duplicates from ArrayList.
package com.dla; import java.util.*; public class RemoveDuplicatesFromArrayList { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>( Arrays.asList("Dev", "Labs", "Alliance", "Dev", "Labs", "Dev", "DevLabsAlliance")); System.out.println("ArrayList with duplicates: " + list); // Creating a new ArrayList ArrayList<String> newList = new ArrayList<>(); // Traversing through the first list for (String element : list) { // If this element is not present in newList, then add it if (!newList.contains(element)) { newList.add(element); } } System.out.println("ArrayList with duplicates removed: " + newList); } }
// Output
ArrayList with duplicates: [Dev, Labs, Alliance, Dev, Labs, Dev, DevLabsAlliance]
ArrayList with duplicates removed: [Dev, Labs, Alliance, DevLabsAlliance]
20. Write a Java program to print all the permutations of a string.
package com.dla; public class PermutationOfString { // Function to print all the permutations of given string static void permutation(String s, String result) { // If string is empty if (s.length() == 0) { System.out.print(result + " "); return; } for (int i = 0; i < s.length(); i++) { // ith character of string char ch = s.charAt(i); // Remaining string after excluding the ith character String remainStr = s.substring(0, i) + s.substring(i + 1); // Recursive call permutation(remainStr, result + ch); } } public static void main(String[] args) { String d = "Dev"; permutation(d, ""); } }
// Output
Dev Dve eDv evD vDe veD