Tuesday, 23 September 2014

Most Common Java Programs for Testers in Interviews

1.How can you show the duplicate elements from an array by avoiding distinct elements.

     ArrayList<String>list = new ArrayList<String>();
    //list of numbers added in from 0-9
   
        for (int i = 0; i < 10; i++) {
            list.add(String.valueOf(i));
        }
        //another set of numbers from 0-5
        for (int i = 0; i < 5; i++) {
            list.add(String.valueOf(i));
        }

        System.out.println("My List : " + list);
        System.out.println("\nHere are the duplicate elements from list : " + findDuplicates(list));

      For the full code Please ClickHere

2.How can you reverse a number 

        int reverse = 0;
        while(number != 0){
            reverse = (reverse*10)+(number%10);
            number = number/10;
        }
        return reverse;
   
For the full code Please ClickHere

3.How can you sort an collection of array of strings

String[] strArray = { "Abhishek", "selenium", "Automation" };

  displayArray(strArray);

   Arrays.sort(strArray);

   displayArray(strArray);

Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER);

  displayArray(strArray);

  System.out.println("---------------");

  List<String> strList = new ArrayList<String>();
  strList.add("Abhishek");
  strList.add("Webdriver");
  strList.add("Automation");
  displayList(strList);

 For the full code Please ClickHere

4.Can you reverse a String with and Without using StringBuffer

   With String Buffer
     StringBuffer buffer = new StringBuffer(str);
      buffer.reverse();
     return buffer.toString();

   Without String Buffer
    int length = str.length();
    String original = str;
    String reverse = "";
    for(int i = length-1; i>=0; i--){
    reverse = reverse + original.charAt(i);
    }
   return reverse;

For Full Code Please ClickHere

5.How can you say a number is Prime or not According to user input

    if (isPrime(n)) {
            System.out.println(n + " is a prime number");
        } else {
            System.out.println(n + " is not a prime number");
        }

    if (n <= 1) {
            return false;
        }
        for (int i = 2; i < Math.sqrt(n); i++) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;

For Full Code Please ClickHere

6.How can you a number is Palindrome or not According to user input

for (int i=0; i<=num; i++){
   int r=num%10;
   num=num/10;
   rev=rev*10+r;
   i=0;
   }

For Full Code Please ClickHere

7.How can you show the maximum numbers from the given array

  int maxOne = 0;
  int maxTwo = 0;
  for(int n:nums){
   if(maxOne<n){
    maxTwo = maxOne;
    maxOne = n;
   }else if(maxTwo<n){
    maxTwo = n;
   }
 
  }

For the Full Code Please ClickHere

8.Can you the Longest Substring Without Repetition of Characters in the string

  char[] inputCharArr = input.toCharArray();
for (int i = 0; i < inputCharArr.length; i++) {
char c = inputCharArr[i];
if (flag[c]) {
extractSubString(inputCharArr,j,i);
for (int k = j; k < i; k++) {
if (inputCharArr[k] == c) {
j = k + 1;
break;
}
flag[inputCharArr[k]] = false;
}
} else {
flag[c] = true;
}
}
extractSubString(inputCharArr,j,inputCharArr.length);
return subStrList;

For Full Code Please ClickHere

9. Swapping of Two Numbers Without having a temp Variable

       x = x+y;
        y=x-y;
        x=x-y;
For the Full Code Please ClickHere

10.How can you remove the multiple spaces of String

package Logs;
import java.util.StringTokenizer;

public class Mutiplespaces {
    public static void main(String a[]){
        String str = "Selenium    Webdriver Automation      Testing";
        StringTokenizer st = new StringTokenizer(str, " ");
        StringBuffer sb = new StringBuffer();
        while(st.hasMoreElements()){
            sb.append(st.nextElement()).append(" ");
        }
        System.out.println(sb.toString().trim());
    }
}

Output
Selenium Webdriver Automation Testing

To know FAQ's on Selenium Please ClickHere
To know SQL's Queries Required For Testers Please ClickHere

No comments:

Post a Comment