Sei sulla pagina 1di 18

Top 20 Java Programming Interview

Questions for SDET

By DevLabs Alliance

Visit us at: www.devlabsalliance.com


Email: training@devlabsalliance.com
Contact: +91 9717514555
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");
}
Programming Interview Questions for SDET

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;
}
Programming Interview Questions for SDET

/* 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


Programming Interview Questions for SDET

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
Programming Interview Questions for SDET

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;
}
Programming Interview Questions for SDET

System.out.println("The reversed string is: " + rev);


}
}

Output: The reversed string is: DevLabsAlliance in Succeed And Lead Learn
Programming Interview Questions for SDET

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
Programming Interview Questions for SDET

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;
}
}
}
Programming Interview Questions for SDET

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
Programming Interview Questions for SDET

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);
}
}
Programming Interview Questions for SDET

System.out.println(strb);
}
}

Ouput: Learn, Lead and Succeed with DevLabsAlliance


Programming Interview Questions for SDET

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


Programming Interview Questions for SDET

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);
}
Programming Interview Questions for SDET

else {
map.put(split[i], 1);
}
}
System.out.println(map);
}
}

Output: {Training=2, in=1, and=1, course=2, Devlabs=1, certification=1}


Programming Interview Questions for SDET

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
Programming Interview Questions for SDET

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

SDETs can develop test automation tools and can use it but testers are not expected to
develop the automations tools, they can only use the various automation tools.
Visit us at: www.devlabsalliance.com
Email: training@devlabsalliance.com
Contact: +91 9717514555

Potrebbero piacerti anche