DevLabs Alliance - WhatsApp
DevLabs Alliance Logo

Home / Blogs /Top 10 Java Program...

Top 10 Java Programming Interview Questions for SDET - 2024

John Doe

2024-03-11

DevLabs Alliance Blogs

0 mins read

Q1. 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

Q2. 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

Q3. 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

Q4. 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

Q5. 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

Q6. 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

Q7. 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

Q8. Write a Java program to count the number of words in a string using Hash Map?

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}

Q9. 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

Q10. What is the difference between SDET and manual Tester?

SDET is a highly skilled resource, having both development and testing skills, whereas manual testers have limited programming skills and can only prepare and execute the test cases.


SDET can develop test automation tools and can use it, but testers are not expected to develop the automation tools, they can only use the various automation tools.

Know Our Author

DevLabs Alliance Author Bio

John Doe


DevLabs Alliance TwitterDevLabs Alliance LinkedInDevLabs Alliance Instagram

Author Bio

John Doe is a seasoned software engineer with over a decade of experience specializing in Java programming. He holds a Bachelor's degree in Computer Science. John is also passionate about sharing his knowledge and regularly contributes to online forums and blogs on Java programming topics.

INQUIRY

Want To Know More


Email is valid



Phone


By tapping continuing, you agree to our Privacy Policy and Terms & Conditions

“ The hands-on projects helped our team put theory into practice. Thanks to this training, we've achieved seamless collaboration, faster releases, and a more resilient infrastructure. ”
DevLabs Alliance Blogs Page Review
Vijay Saxena

SkillAhead Solutions

DevLabs Alliance Footer section
DevLabs Alliance LinkedIn ProfileDevLabs Alliance Twitter ProfileDevLabs Alliance Facebook ProfileDevLabs Alliance Facebook Profile
DevLabs Alliance Logo

USA

1603, Capitol Avenue, Suite 413A, 2659, Cheyenne, WY 82001, USA

DevLabs Alliance ISO 9001

DevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer Section

`Copyright © DevLabs Alliance. All rights Reserved`

|

Refund & Reschedule Policy

Privacy Policy

Terms of Use