8. What is the difference between List and Set in Java Collections?
Answer
In Java Collections, List and Set are two of the most commonly used interfaces, but they serve different purposes:
Feature | List | Set |
Order | Maintains insertion order | May not maintain insertion order |
Duplicates | Allows duplicates | Does not allow duplicates |
Implementations | ArrayList, LinkedList | HashSet, LinkedHashSet, TreeSet |
Index Access | Supports index-based access | No index-based access |
When to Use in Automation Testing:
- Use a List<WebElement> when locating multiple elements on a page:
java
List<WebElement> allLinks = driver.findElements(By.tagName(“a”));
- Use Set<String> when collecting unique values such as dropdown options or removing duplicate test data:
java
Set<String> uniqueOptions = new HashSet<>();
for (WebElement option : dropdownOptions) {
uniqueOptions.add(option.getText());
}
List is ideal for ordered data, while Set is preferred for uniqueness.
9. Write a Java program to demonstrate Inheritance.
Answer
Inheritance is a fundamental concept in OOP that allows one class (child) to inherit fields and methods from another class (parent).
🔹 Java Program:
java
class Animal {
void sound() {
System.out.println(“Animal makes a sound”);
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println(“Dog barks”);
}
}
public class TestInheritance {
public static void main(String[] args) {
Animal obj = new Dog(); // Upcasting
obj.sound(); // Output: Dog barks
}
}
Application in Selenium Framework:
Inheritance is used to reuse test setup/teardown methods:
java
public class BaseTest {
WebDriver driver;
@BeforeMethod
public void setup() {
driver = new ChromeDriver();
}
}
public class LoginTest extends BaseTest {
@Test
public void verifyLogin() {
driver.get(“https://example.com”);
}
}
10. What is an Iterator and how is it used in Collections?
Answer
An Iterator is an interface in Java that allows traversing through a Collection (like List, Set) one element at a time in a safe way (i.e., supports removing elements during iteration).
🔹 Syntax:
java
Iterator<String> it = myList.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
Key Methods:
- hasNext(): Checks if more elements exist
- next(): Returns next element
- remove(): Removes current element from collection
Selenium Use Case:
When working with a list of elements:
java
List<WebElement> elements = driver.findElements(By.className(“items”));
Iterator<WebElement> iterator = elements.iterator();
while (iterator.hasNext()) {
WebElement el = iterator.next();
System.out.println(el.getText());
}
Iterator ensures safe traversal and is especially helpful when modifying the collection during iteration.
11. What is Typecasting and why is it important in Java programming?
Answer
Typecasting is converting a variable from one data type to another. There are two types:
Implicit (Automatic) – widening conversion
java
int a = 10;
double b = a; // int to double
Explicit (Manual) – narrowing conversion
java
double d = 10.5;
int i = (int) d; // double to int
Why Important in Selenium/Test Automation:
- When retrieving numeric test data from external files (like Excel), you may receive data as String and need to convert it:
java
String ageText = “30”;
int age = Integer.parseInt(ageText);
- While interacting with JavaScript or third-party libraries, you often cast object types:
java
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(“window.scrollBy(0,300)”);
Typecasting improves code flexibility and supports interfacing with APIs or data processing logic.
12. Explain the use of this and super keywords in Java.
Answer
🔹 this keyword:
- Refers to the current object of the class.
- Resolves naming conflicts between instance variables and parameters.
java
public class LoginPage {
WebDriver driver;
public LoginPage(WebDriver driver) {
this.driver = driver; // disambiguates instance vs parameter
}
}
🔹 super keyword:
- Refers to the parent class object.
- Used to call parent class constructors and methods.
java
class BaseTest {
public BaseTest() {
System.out.println(“BaseTest setup”);
}
}
class LoginTest extends BaseTest {
public LoginTest() {
super(); // Calls BaseTest constructor
System.out.println(“LoginTest setup”);
}
}
Use in Selenium:
- this.driver = driver; is commonly used in Page Object Constructors.
- super() is used when extending a base class in a framework.
13. Why are Wrapper Classes used in Java? Give practical use cases.
Answer
Wrapper classes wrap primitive data types into objects so they can be used in situations that require objects.
Primitive | Wrapper |
int | Integer |
double | Double |
char | Character |
boolean | Boolean |
Why Needed:
Collections (like List, Set) work only with objects, not primitives.
java
List<Integer> numbers = new ArrayList<>();
numbers.add(10); // int is auto-boxed into Integer
Useful in Selenium wait conditions:
java
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.numberOfElementsToBeMoreThan(By.tagName(“option”), 2));
- Required for null handling and method overloading (e.g., overloaded methods for Integer vs int).
Wrapper classes provide the ability to work with null values, object methods, and integrate with Java generics.
14. What is the difference between final, finally, and finalize in Java?
Answer
Keyword | Description |
final | Used to declare constants, prevent method overriding, or prevent class inheritance |
finally | A block that always executes after try-catch, used for cleanup like closing files |
finalize() | A method invoked by Garbage Collector before destroying the object (deprecated since Java 9) |
🔹 Example: final
java
final int MAX_TIMEOUT = 10; // Constant value
🔹 Example: finally
java
try {
int x = 10 / 2;
} catch (ArithmeticException e) {
System.out.println(“Error”);
} finally {
System.out.println(“This block always executes”);
}
🔹 Example: finalize() (Not recommended now)
java
@Override
protected void finalize() {
System.out.println(“Object is garbage collected”);
}
Selenium Context:
- final is used in constants for wait times or locator strings.
- finally is used to ensure WebDriver quits even if an exception occurs.
The Next 7 Questions -3: JAVA COMMANDS