Skip to content
TargetQA

Programming for Automation

Java Interview Questions & Answers

Java interview questions for automation testers: OOP in frameworks, collections, strings, exceptions, streams, TestNG/JUnit and common coding problems.

50 questions18 Beginner20 Intermediate12 Advanced

Interview Questions

Showing 15 of 50 questions

  1. Encapsulation, inheritance, polymorphism and abstraction. In a Selenium framework, page objects encapsulate locators, a BaseTest/BasePage provides inheritance, WebDriver references give polymorphism, and interfaces like WebDriver itself are abstraction.

  2. Overloading is the same method name with different parameter lists in the same class, resolved at compile time. Overriding is a subclass redefining a parent method with the same signature, resolved at runtime.

  3. String is immutable, so every modification creates a new object. StringBuilder is mutable and not synchronized (fast), StringBuffer is mutable and synchronized (thread-safe but slower).

  4. List (ArrayList) for ordered elements such as findElements results, Set (HashSet) to detect duplicates or store window handles, and Map (HashMap) for test data and config key-value pairs.

  5. Checked exceptions are verified at compile time and must be handled or declared (IOException). Unchecked exceptions extend RuntimeException and are not enforced by the compiler; all Selenium exceptions are unchecked.

  6. final is a keyword making variables constant, methods non-overridable and classes non-inheritable. finally is a block that always runs after try/catch, used for cleanup like driver.quit(). finalize() was a GC hook, deprecated since Java 9.

  7. static members belong to the class rather than an instance, so they are shared and accessible without creating an object. Frameworks use it for utility methods, constants and config loaders.

  8. An interface defines a contract a class can implement (many allowed), with default/static methods since Java 8. An abstract class can hold state, constructors and partial implementation, but a class can extend only one.

  9. Generics provide compile-time type safety by parameterizing classes and methods with types. They let you write reusable utilities, like a JSON deserializer or fluent page navigation, without casting.

  10. Streams let you filter, map and collect collections declaratively, and lambdas provide concise implementations of functional interfaces. They're great for processing lists of WebElements or API response data.

  11. Both are test frameworks. TestNG offers groups, dependsOnMethods, DataProvider and XML-driven parallel suites; JUnit 5 uses a modular Jupiter API with @ParameterizedTest, extensions and tags.

  12. A hard assert stops the test immediately on failure. A soft assert collects failures and reports them all when assertAll() is called, useful for validating many fields on one page.

  13. Maven is a build and dependency management tool driven by pom.xml. Tests run in the test phase via the Surefire plugin, so mvn test or mvn clean test executes the suite, typically in CI.

  14. HashMap stores entries in an array of buckets indexed by the key's hashCode. Collisions are chained in a linked list that becomes a balanced tree after 8 entries (Java 8+); equals() finds the exact key.

  15. Wrap the driver in ThreadLocal so each thread gets its own instance. A DriverManager sets it in @BeforeMethod, reads it via get(), and calls quit() then remove() in @AfterMethod.