http://www.technicalpage.net/search/label/SQL

selenium-Java-interview

Important Java Selenium Questions/Codes for Software Test Automation Learners, Part3

This page contains Java code for below questions:

1. Reverse characters

2. Create Palindrome

3. Find unique characters(Remove duplicate characters)

4. Count the repetition of characters(find duplicate characters) in a given string

5. Create Fibonacci sequence/number

6. Regular expression

Find the sum of all the numbers present in a given string containing alphanumeric and special characters. 

7. Sum all the characters present in your name

8. Find the second most repeated character in a given string


1. Reverse characters

     public static void reverseChar() {

           String str = "asdfghjkl";

           //lkjhgfdsa

           String expStr = "";

           int len = str.length();

           for(int i=len-1;i>=0;i--) {

                char ch = str.charAt(i);

                String strCh = Character.toString(ch);

                expStr = expStr+strCh;

           }

           System.out.println("The reverse string is : "+expStr);

     }

OUTPUT:
The reverse string is : lkjhgfdsa

2. Create Palindrome

     public static void palindrome() {

           String str = "level";

           //lkjhgfdsa

           String expStr = "";

           int len = str.length();

           for(int i=len-1;i>=0;i--) {

                char ch = str.charAt(i);

                String strCh = Character.toString(ch);

                expStr = expStr+strCh;

           }

           //System.out.println("The reverse string is : "+expStr);

          

           String strInSameOrder = "";

           for(int i=0;i<len;i++) {

                char ch = str.charAt(i);

                String strCh = Character.toString(ch);

                strInSameOrder = strInSameOrder+strCh;

           }

           //System.out.println("The string in right/same order is : "+strInSameOrder);

          

           if(expStr.equals(strInSameOrder)) {

                System.out.println("The given string is a Palindrome.");

           } else {

                System.out.println("The given string is NOT a Palindrome.");

           }

     }

OUTPUT:

The given string is a Palindrome.


3. Find unique characters(Remove duplicate characters)

     public static void uniqueChar() {

          

           String str = "asdfsgdfsdasderfvdera";

          

           ArrayList<String> alist = new ArrayList<>();

           char ch = str.charAt(0);

           String strCh = Character.toString(ch);

           alist.add(strCh);

           int len = str.length();

          

           for(int i=0;i<len;i++) {

                char chh = str.charAt(i);

                String strChh = Character.toString(chh);

                if(!alist.contains(strChh)) {

                      alist.add(strChh);

                }

           }

           System.out.println("The unique characters are : "+alist);

 

     }

OUTPUT:

The unique characters are : [a, s, d, f, g, e, r, v]


4. Count the repetition of characters(find duplicate characters) in a given string

     public static void duplicateChar() {

           String str = "asdfsgdfsdasderfvdera";

           int len = str.length();

          

           HashMap<String, Integer> map = new HashMap<>();

           for(int i=0;i<len;i++) {

                char ch = str.charAt(i);

                String strCh = Character.toString(ch);

                if(map.containsKey(strCh)) {

                      map.put(strCh, map.get(strCh)+1);

                } else {

                      map.put(strCh, 1);

                }

           }

           System.out.println("The duplicate chars are : "+map);

          

     }

 

OUTPUT:

The duplicate chars are : {a=3, r=2, s=4, d=5, e=2, f=3, v=1, g=1}


5. Create Fibonacci sequence/number

     public static void fibonacciSeq() {

           // 0,1,1,2,3,5,8,13,....

           ArrayList<Integer> alist = new ArrayList<>();

           alist.add(0);

           alist.add(1);

           for(int i=2;i<10;i++) {

                alist.add(alist.get(i-1)+alist.get(i-2));

           }

           System.out.println("The Fibonacci sequence is :"+alist);

     }

OUTPUT:

The Fibonacci sequence is :[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]


6. Regular expression

Find the sum of all the numbers present in a given string containing alphanumeric and special characters. 

     public static void reqExpressionSum() {

           String str = "as3df5cs@11we4zas#21$juyt";

           String strArr = str.replaceAll("[^0-9]", " ");

           System.out.println("strArr = "+strArr);

           String[] strSum = strArr.split(" ");

           System.out.println("Array of numbers = "+Arrays.toString(strSum));

          

           int len = strSum.length;

           int sum = 0;

           for(int i=0;i<len;i++) {

                if(!strSum[i].equals("")) {

                      int x = Integer.parseInt(strSum[i]);

                      sum = sum+x;

                }

           }

           System.out.println("Sum of the numbers present in the string = "+sum);

     }

    

OUTPUT:

strArr =   3  5   11  4    21    

Array of numbers = [, , 3, , 5, , , 11, , 4, , , , 21]

Sum of the numbers present in the string = 44



7. Sum all the characters present in your name

     public static void sumName() {

           String name = "John Smith";

           int nameSum = 0;

           for(int i=0;i<name.length();i++) {

                char nthchar = name.charAt(i);

                //System.out.println("nthchar = "+nthchar);

                int j = nthchar;

                System.out.println("nthchar, j = "+nthchar+" "+j);

                nameSum = nameSum+nthchar;

           }

           System.out.println("Summation of all characters in the name = "+nameSum);

     }

 

OUTPUT:

nthchar, j = J 74

nthchar, j = o 111

nthchar, j = h 104

nthchar, j = n 110

nthchar, j =   32

nthchar, j = S 83

nthchar, j = m 109

nthchar, j = i 105

nthchar, j = t 116

nthchar, j = h 104

Summation of all characters in the name = 948



8. Find the second most repeated character in a given string

     public void secMostRepChar() {

           String str = "asdfsgdfsdasdferfvdera";

           int len = str.length();

          

           HashMap<String, Integer> map = new HashMap<>();

           for(int i=0;i<len;i++) {

                char ch = str.charAt(i);

                String strCh = Character.toString(ch);

                if(map.containsKey(strCh)) {

                      map.put(strCh, map.get(strCh)+1);

                } else {

                      map.put(strCh, 1);

                }

           }

           System.out.println("The duplicate chars are : "+map);

          

           int mapLen = map.size();

           int map2ndVal = 0;

           ArrayList<Integer> alist = new ArrayList<>(map.values());

           ArrayList<String> blist = new ArrayList<>(map.keySet());

           System.out.println("value at index 0 of hashmap : "+alist.get(0));

           System.out.println("key at index 0 of hashmap : "+blist.get(0));

           for(int i=1;i<mapLen;i++) {

                if((alist.get(i-1))>map2ndVal && (alist.get(i-1)<alist.get(i))) {

                      map2ndVal=alist.get(i-1);

                }

           }

           System.out.println("The second highest repeated number : "+map2ndVal);

           //String key2nd = null;

           //key2nd = blist.get(alist.indexOf(map2ndVal));

           //System.out.println("The second highest repeated char : "+key2nd); //this is returning 1st value only

          

           //to get the values if more than one value matches the condition

           ArrayList<String> clist = new ArrayList<>();

           for(int i=0;i<alist.size(); i++) {

                if(alist.get(i)==map2ndVal) {

                     //clist.add(blist.get(i)+Integer.toString(map2ndVal)); OR

                     clist.add((blist.get(i)).concat(Integer.toString(map2ndVal)));

                }

           }

           System.out.println("The second highest repeated char : "+clist);

     }

OUTPUT:

The duplicate chars are : {a=3, r=2, s=4, d=5, e=2, f=4, v=1, g=1}

value at index 0 of hashmap : 3

key at index 0 of hashmap : a

The second highest repeated number : 4

The second highest repeated char : [s4, f4]