Java SDET Questions and Answers
1. What is the difference between `==` and `.equals()` in Java?
Answer:
- `==` checks reference (memory address) equality.
- `.equals()` checks content equality (overridden in classes like String, Integer,
etc.).
2. What is the difference between `ArrayList` and `LinkedList`?
Answer:
- `ArrayList` is backed by an array; faster for retrieving data (get, index-based
access).
- `LinkedList` is a doubly-linked list; better for inserting/deleting elements
frequently.
3. What is a constructor in Java?
Answer:
A constructor is a special method that initializes a newly created object. It has
the same name as the class and no return type.
4. What is the difference between `throw` and `throws`?
Answer:
- `throw` is used to manually throw an exception.
- `throws` is used to declare exceptions in the method signature.
5. What is an Exception?
Answer:
An exception is an event that disrupts the normal flow of the program. It is
handled using try-catch blocks.
6. What are the 4 pillars of OOPs?
Answer:
1. Encapsulation
2. Abstraction
3. Inheritance
4. Polymorphism
7. What is method overloading and overriding?
Answer:
- Overloading: Same method name, different parameters (compile-time).
- Overriding: Same method name and parameters in child class (runtime).
8. Can we override static methods?
Answer:
No, static methods are bound at compile time, so they cannot be overridden.
9. What is the use of `HashMap` in automation testing?
Answer:
`HashMap` is used to store key-value pairs like test data, environment variables,
and object locators.
10. How do you handle file reading in Java?
Answer:
Using `BufferedReader`, `FileReader`, `Scanner`, or `Files.readAllLines()` from
`java.nio.file`.
11. What is `StringBuilder` and how is it different from `String`?
Answer:
- `String` is immutable, every change creates a new object.
- `StringBuilder` is mutable and used for frequent string manipulations.
12. What are the major interfaces in the Java Collections Framework?
Answer:
- List
- Set
- Map
- Queue
13. Difference between `List` and `Set`?
Answer:
- List allows duplicates and maintains insertion order.
- Set does not allow duplicates and order is not guaranteed (unless using
LinkedHashSet).
14. How do you use Java in Selenium?
Answer:
- Write test scripts using Java syntax
- Use Java collections, exception handling, loops, OOPs concepts to design test
frameworks
15. How do you validate API response using Java?
Answer:
- Use libraries like RestAssured, HttpClient, or Retrofit
- Validate response using Java assertions or frameworks like TestNG/JUnit
16. What is the purpose of POM (Page Object Model)?
Answer:
To improve code maintainability by separating locators and actions in separate Java
classes representing each web page.
17. What are Lambda Expressions?
Answer:
Lambda expressions allow you to write anonymous functions—used often in streams and
functional interfaces (Java 8+).
Example:
List<String> names = Arrays.asList("John", "Jane");
names.forEach(name -> System.out.println(name));
18. How do you handle synchronization in Java?
Answer:
- Use `synchronized` keyword
- Locks from `java.util.concurrent.locks`
- Useful in multi-threaded test frameworks (e.g., parallel execution)
19. How to use assertions in Java TestNG?
Answer:
Assert.assertEquals(actual, expected);
Assert.assertTrue(condition);
20. How to pass data between steps in Cucumber using Java?
Answer:
- Use dependency injection with ScenarioContext or World object
- Store values in a shared class or use @Before hooks