45+ Java Interview Questions for Selenium Professionals

We learn to earn bread and butter and the path to earning goes through screening. That screening we call as the interview. We have discussed interview questions on various topics like Selenium, TestNG & Cucumber. Today we are going to add another milestone to the interview segment of our blog. Therefore, we are going to discuss most asked Java Interview questions faced by Selenium professionals.

If you are a Selenium professional with Java programming language and want to go for the new job interview then these Java interview questions would be proved as the milestone. They will give you an idea what kind of Java interview questions interviewer is going to ask you.

Before you jump to these hot java interview questions, I would like to inform you that these questions are asked in an interview for various Selenium position in MNCs, so you can’t ignore them. I am pretty much sure that you will get a few questions from the given list of Java interview questions. So better look at them once.

Anyways, I have a collection of powerful interview questions as complimentary knowledge gift along with this Java interview questions. You are suggested to go through them once:

Core java interview questions

Let’s dig into some of the precious Java interview questions.

List of most asked Java Interview Questions

Question# 1:  Can we write WebDriver driver =new WebDriver()?

Answer: No, as we all know WebDriver is an interface so we cannot create the object of the interface in Java.

Question# 2: Can you create the object of object class?

Answer: Object is the superclass, hence, we can create the object of an object class.

Object obj=new Objcet();

Question# 3: How to get all the values from the dropdown?

Answer:  By using getOptions() method. Here is the sample code.

WebElement element = driver.findElement(By.id("inst_state"));

Select s = new Select(element);

List <WebElement> elementcount = s.getOptions();

System.out.println(elementcount.size());

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

{

String value = elementcount.get(i).getText();

System.out.println(value);

}

Question# 4: What is the parent of List interface?

Answer: Collection

Question# 5: Tell me the difference between Array and ArrayList?

Answer:

Array ArrayList
An array is a fixed size data structure. ArrayList is not a fixed size data structure
An array can contain both primitive data types as well as objects of a class depending on the definition of the array. However, ArrayList only supports object entries, not the primitive data types.

Question# 6: What are the differences between Set & List?

Answer:

List Set
The list is an ordered collection it maintains the insertion order. Set is an unordered collection, it doesn’t maintain any order.
A list allows duplicates elements. While Set doesn’t allow duplicate elements.
A list allows any number of null values. Set can have only a single null value at most.

Question# 7: Can you explain the architecture of WebDriver?

Answer:

java interview questions selenium architecture

Question# 8: What is constructor overloading?

Question# 9: Is abstraction possible without abstract class?

Answer: Yes, by using interface.

Question# 10:  Can we make the constructor private?

Answer: Yes. If it’s made private, then we can’t create an instance of that class outside the class.

Question# 11: What is ChromeDriver and its use?

Answer: It’s a fully implemented class and Its use to create a secure connection with the chrome browser.

Question# 12: Can you set the value without the sendKeys method?

Answer:

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("document.querySelector('attributeValue').value='new value'");

Question# 13: How to get the value from the textbox?

Answer:  driver.findElement(By.id(“someid”)).getAttribute(“value”); Question# 14: What is stale element reference exception?Answer: Stale Element means an old element or no longer available element. Let’s suppose there is an element which is found on a web page and we referenced it as a WebElement in WebDriver. In that case, if UI gets changed so the DOM will also be changed then the WebElement goes stale. If we try to interact with an element which is staled then the StaleElementReferenceException is thrown.

The two reasons for Stale element reference are

  • The element is not available anymore, might be deleted.
  • That element is no more attached to the DOM.

Solution:

Just refresh the page and try again for the same element to appear. If the element is not available to DOM then you can go with ‘try-catch block’ inside ‘for loop’. Here is the sample code:

for(int i=0; i<=2; i++){  
try{     
driver.findElement(By.xpath(“xpath”)).click();     
break;  
}catch(Exception e){     
System.out.println(e.getMessage());
}
}

Question# 15: Can we keep try block without catch block?

Answer: Yes, instead of catch we can use finally block.

Question# 16: What will be the output of the given statement?

try{

int a=10/0;

}catch(Exception e){ 

}catch(ArithmaticException e1){ 

}

Answer: It will throw a compile-time error since Exception is the super most class so it should be at the lower position which simply means that there should be catch block with specified Exception then the most generic one. The correct code would be:

try{
int a=10/0;

}catch(ArithmaticException e1){ 

}catch(Exception e){ 

}

Question# 17: How to run the same test case 10 times with TestNG?

Answer: Use invocation count annotation in the test annotation. Click here to know more about Invocation count.

Recommended: Run the same test multiple times in Selenium using TestNG

Question# 18: Which protocol REST API uses?

Answer: HTTPS protocol.

Question# 19: What is the name of the method used in HTTPS?

Answer: POST.

Question# 20: Where is memory allocated in the below statement?

String str=new String();

Answer: Heap memory.

Question# 21:  If, String str1=”abc”;

And, String str2=new String(“abc”);

Is this true, ((str1) == (str2))?

Answer: No, because object ref is different in both the cases.

We can want the comparison result to be true then we need to use intern() method. Here is the sample code:

str1.intern()==str2.intern();

intern() method puts memory ref in string pool.

Question# 22: Where is memory allocated in the below statement?

String str=”abc”;

Answer: String constant pool.

Question# 23: If we have more than one abstract method then what will you use for abstraction- interface or abstract class?

Answer: Abstract class has an abstract method as well as a concrete method but in an interface, all the methods are an abstract method for 100% abstraction. So interface is the best option to use.

Question# 24: Write a program to reverse any string without using any third variable.

Answer:

class ReverseString

{

public static void main(String[] args)

{

String input = "Sachin";

// convert String to character array

// by using toCharArray

char[] chars = input.toCharArray();

for (int i = chars.length-1; i>=0; i--) {

System.out.println(chars[i]);

}

}

Question# 25: Write a program to reverse an array using the collection.

Answer:

public class reversingArray {

public static void main(String[] args)

{

Integer [] arr = {10, 20, 30, 40, 50};

Collections.reverse(Arrays.asList(arr));

System.out.println(Arrays.asList(arr));    }

}

Question# 26: How does HashMap work?

Answer: Hashing is the principle behind working of the HashMap. In hashing, Key and Value of the HashMap is linked by using hash functions. We store object by calling put(key, value) method of HashMap and retrieve the stored objects by calling get(key) method. When put method is called then hashcode() method which is associated with the key object is called. With this process, a bucket location is assigned to store the value which is fetched by the hash function. The bucket location is the index position of an array, called a table. The internal mapping of the HashMap stored in the form of Map.Entry objects, which ultimately holds objects of the key and value pair. When you want to retrieve the object, you call the get() method and then pass the key object.

Ad

Question# 27: What is WebDriver, a class or an interface?

Answer: An Interface.

Question# 28: How to instantiate the chrome browser?

Answer: WebDriver driver=new ChromeDriver();

Here ChromeDriver has fully implemented a class which is used to establish a secure connection with the browser. We are creating the object and its type is WebDriver.

Question# 29: Can we use- ChromeDriver driver=new ChromeDriver();?

Answer: Yes, but driver instance would be available for chrome browser only.

Question# 30: Hashmap<Integer,Integer> hm=new Hashmap<Integer,Integer>();

If, hm.put(“abc”,10); then,

What will be the output?

Answer: It will throw a compile-time error.

Question# 31: Hashmap<Object,Object> hm=new Hashmap<Object,Object>();

If, hm.put(“abc”,10); then,

What will be the output?

Answer: As an object is the superclass so it will not throw any error. It does not check the object type before storing the object.

Question# 32: How can you restrict the method overriding?

Answer: By making the method static, final or private.

Question# 33: What is immutability In string?

Answer:

In java, string objects are immutable. Immutable simply means unmodifiable or unchangeable.

Once string object is created its data or state can’t be changed rather a new string object gets created; since java uses the concept of string literal. Suppose there are five reference variables, all refer to one object “avinash”. All those reference variables are connected, means one value gets changes then others will also be impacted and change will be triggered. This is why String objects are not mutable in Java.

Question# 34: What are the differences between String & StringBuffer?

Answer:

String StringBuffer
A string is not thread-safe. A string buffer is thread-safe.
Non-Mutable. Mutable.

Question# 35: How can you make a method thread-safe?

Answer:  Method should be synchronized.

public static synchronized int getCount() Question# 36: Differences between interface and abstract class

Answer:

Interface Abstract Class
Variables declared inside an interface is by default final. An abstract class may contain non-final variables.
Members of the Java interface are public by default. An abstract class can have a various combination of class members like private, protected, etc.
The interface is abstract, all the methods are abstract. But, in an abstract class abstract and concrete method can be there.
The interface supports multiple inheritances. Abstract class doesn’t support multiple inheritances.

Question# 37: Can we override the main method?

Answer: No, main method is static so a static method cannot be overridden.

Question# 38: What is the basic difference between Collection & Collections?

Answer: Collection is interface but collections are utility class.

Question# 39: Write a program to find out second largest element in an array using collections.

Answer:

Arr [] a={12,10,4,5,6};
List<Integer> list=Arrays.asList(a);
Collections.sort(list);
int element=list.get(list.size()-2);

Question# 40: Write a logic to swap two numbers without using the third variable.

Answer:

Int a,b,c;

a=a+b;
b=a-b;
a=a-b;

Or

a=a*b;
b=a/b;
a=a/b;

Question# 41: Write a program for the given series:

0,1,1,2,3,5,8,11

Answer:

class FibonacciExample1{

public static void main(String args[])

{

int n1=0,n2=1,n3,i,count=10;

System.out.print(n1+" "+n2);//printing 0 and 1

for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed

{

n3=n1+n2;

System.out.print(" "+n3);

n1=n2;

n2=n3;

}

}}

Question# 42: What is the command for Maven build?

Answer: mvn install

Miscellaneous Java Interview Questions

Question# 43: Differences between final, finally & finalize?

Question# 44: What is a static block?

Question# 45: Which is triggered first a static block or static main method?

Question# 46: What are the differences between Methods & Constructor?

Question# 47: Does constructor return any value?

These are all about some of the most important Java interview questions. The list does not end here; it will be updated as time goes on. You can post your interview experiences, questions, and tips in the comment section below

Join Inviul fb group

Leave a Reply