Home » Software Testing » Automation Testing » Selenium » Commonly asked java programs in Selenium Interview

Commonly asked java programs in Selenium Interview

In the recent past, I visited a couple of companies for the interviews for automation engineer posts and there I was asked to write small programs which I have learnt in graduation days…

Here is the list of interview question.

1. Print below pattern: This is one of the best programs to learn loop (you can try any among all provided java loops but here I am going to use for loop)

[the_ad_placement id=”incontent”]here is the program which will print above pattern

 

2. Print below pattern: This program is again going to use the same logic but in place of digits, * would be used

here is the java program

[the_ad_placement id=”incontent”]

3. Program to read from file line by line:
bufferedReader class provides readLine method to be used to read the file line by line.

public static void readFile() throws FileNotFoundException {
FileReader fr = new FileReader(“C:\\Users\\…\\Desktop\\unused.txt”);
BufferedReader br = new BufferedReader(fr);
StringBuffer str = new StringBuffer();
try {
while (br.readLine()!= null){
str.append(br.readLine());
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(str);
}

4. Reverse a string without using reverse function

public static void reverse() {
String str = “I use selenium webdriver. selenium is a tool for web applications.”;
int i = str.length();
StringBuffer strb = new StringBuffer();
for( int j=i-1; j>=0; j–){
strb = strb.append(str.charAt(j));
}
System.out.println(strb);
}

If you want to learn more about string related interview question then read 5 Commonly Asked Java String Question in Selenium Interview

5. Replace substring with another string in a string

public static void replace() {
String str = “I use selenium webdriver. selenium is a tool for web applications automation.”;
String toBeReplaced = “selenium”;
String toReplacedWith = “Firefox”;
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);

}

There could have been more ways to create the above programs. I would like to invite you all to find out those.

Along with this, I would suggest you, take this quiz to check your selenium interview preparation

Selenium WebDriver Quiz

Read Similar Posts



19 thoughts on “Commonly asked java programs in Selenium Interview”

    1. Yes, could you please provide more selenium and java programming questions which is asked in interviews. I have 1.5 years of experience in manual testing and recently started to work in selenium 1 month later. Your above post is very useful. Thanks for this….

  1. Kindly share some more java programmes for selenium experience tester and frequently asked questions in selenium interview

  2. 2. Print below pattern: This program is one of the best to understand the for… loop
    *
    ***
    *****
    *******
    *********

    public class StringPractice {
    public static void main(String args[])
    {
    for(int i=0;i<5;i++)
    {

    for(int j=1;j<=1+(i*2);j++)
    {

    System.out.print("*");
    }
    System.out.println();
    }
    }

    }

  3. Thanks, that’s really valuable.. Tried 3 & 5 program worked absolutely fine..

    In program 3 if we can place str.append(“\n”); also in the while loop, the file will be printed as it is. Otherwise multiple lines files comes in a single line

  4. Program 2

    public static void main(String[] args) {
    for(int i=1;i<=10;i=i+2){
    for(int j=1;j=0;i–){

    System.out.print(str.charAt(i));
    }
    }

  5. Program 3

    public static void main(String[] args) {

    String str = “I use selenium webdriver. selenium is a tool for web applications.”;
    int len = str.length();
    for(int i=len-1;i>=0;i–){

    System.out.print(str.charAt(i));
    }
    }

  6. In the fifth Program, if the String to be replaced is the last word, then the word would not be replaced.

  7. I am currently giving interviews for Selenium automation Testing, where I have been asked the below programs.
    1. Write a program to concatenate two sorted array into a single array, and sort it
    2. Reverse words in a sentence . eg: Be a good human —> eB a doog namuh.
    3. Replace a special character with %20. eg: http://www.amazon.com—-&gt; www%20amazon%20com.

    1. 1. Write a program to concatenate two sorted array into a single array, and sort it

      public static void main(String[] args) {
      int arr1[]={10,20,80};
      int arr2[]={2,5,40,60};
      int arr1length=arr1.length;
      int arr2length=arr2.length;
      int arr3Length=arr1length+arr2length;
      int[] arr3=new int[arr3Length];
      int count=0;
      for(int i=0;i<=arr1length-1;i++)
      {
      arr3[i]=arr1[i];
      count++;
      }
      for(int j=0;j<=arr2length-1;j++)
      {
      arr3[count]=arr2[j];
      count++;
      }
      Arrays.sort(arr3);
      for(int k=0;k eB a doog namuh.

      Write below method and call from main class

      public static void reverseWords(String sentence)
      {
      String[] words=sentence.split(” “);
      int wordCount=words.length;
      for(int i=0;i<wordCount;i++)
      {
      StringBuilder strbuilder=new StringBuilder();
      strbuilder.append(words[i]);
      String revWord=strbuilder.reverse().toString();
      System.out.print(revWord+ " ");
      }
      System.out.println();
      }
      3. Replace a special character with %20. eg: http://www.amazon.com—-&gt; www%20amazon%20com.

      public static void replaceSpecialChar(String str)
      {

      String newstr=str.replaceAll("[[email protected]#$%^&*.|?]", "%20");
      System.out.println(newstr);
      }

      1. 1. Write a program to concatenate two sorted array into a single array, and sort it

        public static void main(String[] args) {
        int arr1[]={10,20,80};
        int arr2[]={2,5,40,60};
        int arr1length=arr1.length;
        int arr2length=arr2.length;
        int arr3Length=arr1length+arr2length;
        int[] arr3=new int[arr3Length];
        int count=0;
        for(int i=0;i<=arr1length-1;i++)
        {
        arr3[i]=arr1[i];
        count++;
        }
        for(int j=0;j<=arr2length-1;j++)
        {
        arr3[count]=arr2[j];
        count++;
        }
        Arrays.sort(arr3);
        for(int k=0;k<=arr3.length-1;k++){
        System.out.println(arr3[k]+" ");

        }

  8. Hi will share string programs in java for interview
    Are strings thread-safe in Java?
    yes String thread safe in java, because

    String objects are immutable in nature.
    It can be shared between multiple threads without external synchronization.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.