In this article
  1. Java basics for testers
  2. Selenium
  3. Test frameworks and design
  4. Behavior and closing
  5. Follow-up questions
  6. Sources

Java basics for testers

Why is Java used for test automation?

Java is a statically typed, object-oriented language with a large library and mature build and test tools, and Selenium can be driven from it. Say why that suits a team: compile-time checking catches typing mistakes, good editors refactor test code safely, and many developers already know the language, so tests can live beside the application code. Mention that the choice is a trade-off and name the alternative your team considered.

Why it is asked: interviewers want a reasoned choice, not a habit. See the coding interviews guide for how language questions fit into a wider round.

What are the four principles of object-oriented programming, and where do they show up in a test framework?

Object-oriented programming (OOP) rests on encapsulation, inheritance, polymorphism and abstraction. In a framework, encapsulation hides locators inside page classes, inheritance lets a base test class hold shared set-up, polymorphism lets one method call work with different browser drivers, and abstraction exposes what a page does instead of how. Give one real example from a framework you built. More practice questions are in the Java object-oriented interview questions.

Why it is asked: it checks whether you apply the ideas, not just recite them.

What is the difference between == and equals() for strings?

The operator == compares whether two references point to the same object, while equals() compares the characters. The Java Standard Edition (SE) 21 documentation says equals() is true if and only if the argument is a String that represents the same sequence of characters (Oracle, Java SE 21). Run on OpenJDK 23.0.2, a literal compared with another equal literal gave true for ==, while the same text built with new String gave false for == and true for equals().1 In tests, always use equals() or an assertion method.

Why it is asked: it is a classic source of intermittent test failures.

String a = "login";
String c = new String("login");
System.out.println(a == c);        // false: different objects
System.out.println(a.equals(c));   // true: same characters

Why are Strings immutable, and when do you use StringBuilder?

A string is immutable, meaning its value cannot change after it is created (Oracle, Java SE 21). The reason is sharing: the same page says "Because String objects are immutable they can be shared", which is why two equal literals were one object (a == b was true) in the run.1 It also makes strings thread-safe and hash codes reusable as HashMap keys. Use StringBuilder when you join many pieces in a loop.

Why it is asked: it shows you understand objects and memory, not only syntax.

What is the difference between an abstract class and an interface?

An abstract class can hold state and ordinary methods and a class can extend only one; an interface describes behavior and a class can implement many. The Oracle tutorial says to prefer an abstract class to share code among closely related classes, and an interface when unrelated classes should share a behavior (Oracle tutorial on abstract classes). In a framework, a base page class is often abstract, and a small interface such as a retry policy suits an interface.

Why it is asked: it tests design judgement, not memorised lists.

What is the difference between checked and unchecked exceptions?

A checked exception is an exceptional condition a well-written application should anticipate, and code that might throw one must catch it or declare it, or it will not compile (Oracle tutorial on exceptions). Errors and runtime exceptions are unchecked: the tutorial says runtime exceptions usually indicate programming bugs. In tests, a NullPointerException is unchecked, while reading a file raises a checked one. Do not swallow exceptions silently; fail with a clear message.

Why it is asked: test code must report failures clearly rather than hide them.

What is the difference between final and finally?

The keyword final marks a variable that cannot be reassigned, a method that cannot be overridden or a class that cannot be extended. The block finally belongs to try and catch, and it runs whether or not an exception occurred, which makes it the place to close resources. In the run, the finally block printed after the exception was caught.1 In automation, finally or a teardown method is where you quit the browser.

Why it is asked: candidates often mix up the three similar words.

When do you use a List, a Set and a Map in test code?

A List keeps order and allows duplicates, a Set keeps only unique items and a Map stores key-value pairs. Use a List for the rows of a table in the order shown, a Set to check that all values are unique, and a Map for test data by name. In the run, a list of three ids had two duplicates, so its size was three and the set's size was two, and a missing map key returned null unless getOrDefault was used.1

Why it is asked: choosing the right collection shows practical judgement.

Why does static state cause problems when tests run in parallel?

A static field is shared by every thread in the program, so two tests running together can overwrite each other's browser driver or data. Give each test thread its own driver, for example through a thread-local holder or a framework feature, and avoid shared mutable state. See concurrency interview questions for more on threads.

Why it is asked: parallel runs are common and shared state is the usual cause of confusing failures.

Selenium

How do you choose a locator?

Prefer the most stable and readable locator: an id when it is unique, then a name or a short CSS selector, and XPath when you must move up or across the tree. Avoid locators that depend on layout, such as long positional paths, because they break on small page changes. Ask developers to add stable test attributes where needed. A good answer names one locator you replaced and why.

Why it is asked: locator choice decides how often your tests break.

What is the difference between implicit and explicit waits?

An explicit wait is a loop that polls the application for one specific condition and fails with a timeout if it is not met; an implicit wait is a global setting that applies to every element lookup for the session (Selenium documentation, last modified 16 September 2026). The documentation also warns: do not mix implicit and explicit waits, because it can cause unpredictable wait times. Prefer explicit waits on the condition you need, such as the element being clickable.

Why it is asked: waits are the main cause of flaky tests.

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement submit = wait.until(ExpectedConditions.elementToBeClickable(By.id("submit")));
submit.click();
An explicit wait targets one condition and an implicit wait is globalA table comparing implicit and explicit waits by scope, what they poll for and the advice on mixing them.FeatureImplicit waitExplicit waitScopeWhole sessionOne conditionWhat it doesRetries everylookupPolls until trueOn failureNo such elementTimeout errorMixing bothAvoidAvoidDiagram: Interview Clozo, CC BY 4.0 ·interview.clozo.ai/blog/java-interview-questions-for-automation-testing.html
An explicit wait targets one condition; an implicit wait is a global setting, and the two should not be mixed.CC BY 4.0 — source: Java interview questions for automation testing
Text description of the diagram
  • An explicit wait polls the application for one specific condition and fails with a timeout if it is not met.
  • An implicit wait is a global setting that applies to every element lookup for the session, and the two should not be mixed.

What causes a StaleElementReferenceException, and how do you fix it?

The exception means the page changed after you found the element, so your reference no longer points at anything on the page. It often follows a refresh or a script that rebuilds part of the page. Fix it by finding the element again after the change, and by waiting for the new state before acting. Do not wrap everything in a blind retry, which hides real faults.

Why it is asked: it separates people who have debugged real pages from those who have read about them.

What is the Page Object Model, and why use it?

The Page Object Model is a design pattern in which each page or part of a page is a class whose methods are the actions a user can take. The Selenium documentation says the aim is that if the user interface changes, the fix goes in the page object and the tests do not need to change, and that only one place in the suite should know the structure of a page's HTML (Selenium page-object guide, last modified 16 September 2026). It adds that page objects should not make assertions; the tests should.

Why it is asked: it is the standard way to keep a suite maintainable.

Page objects sit between tests and the browser driverFour boxes in order: a test class calls a page object, the page object drives the browser through WebDriver, and WebDriver operates the web application.Test classassertionsPage objectlocators andactionsWebDriverdrives thebrowserWeb apppage under testDiagram: Interview Clozo, CC BY 4.0 ·interview.clozo.ai/blog/java-interview-questions-for-automation-testing.html
Only the page object knows the page's structure, so a UI change is fixed in one place.CC BY 4.0 — source: Java interview questions for automation testing
Text description of the diagram
  • Only one place in the suite should know the structure of a page.
  • If the user interface changes, the fix goes in the page object and the tests do not need to change.

How do you handle iframes, alerts and new windows?

Switch the driver's context before acting, and switch back afterwards. Use the frame switch to enter an iframe by element, name or index, the alert interface to accept or dismiss a dialog, and the window handles to move between tabs. Wait for the frame or window to be available before switching, and say how you check that you are on the page you expect.

Why it is asked: real pages mix several contexts, and tests fail when the driver is in the wrong one.

Knowing it is half the job. Saying it is the other half.

Interview Clozo is an AI interview copilot for real interview rounds: it hears the question and puts a strong answer on your screen, and you say it in your own words. See the features or how it works.

Start Free Trial →

Test frameworks and design

What do the TestNG annotations do, and in what order do they run?

TestNG annotations mark set-up and test methods. The order is BeforeSuite, BeforeTest (before the classes inside a testng.xml test tag), BeforeClass, BeforeMethod, the @Test method, AfterMethod, AfterClass, AfterTest, then AfterSuite (TestNG, last updated 31 August 2026). Before methods in a superclass run before those in a subclass. A sensible split: start the browser in a before-method, quit it in an after-method, and keep suite-wide set-up, such as reading configuration, at the suite level.

Why it is asked: set-up and teardown placement decides test isolation.

How do you run the same test with different data?

Use a data provider. In TestNG, a method marked DataProvider returns a two-dimensional array of objects, each inner array holding the parameters for one run, and a test refers to it by name (TestNG, last updated 31 August 2026). Keep the data in a file or a method and the steps in the test. Name each case so a failure says which row failed.

Why it is asked: data-driven tests remove duplicate code.

Should tests depend on each other or use priorities?

Avoid it where you can. TestNG allows a test to depend on other methods and to set a priority, and says lower priorities are scheduled first (TestNG, last updated 31 August 2026). But chained tests fail together and cannot run alone or in parallel. Make each test create its own data and leave a clean state, and use dependencies only for a genuine sequence, such as a set-up step.

Why it is asked: independent tests are easier to debug and run in parallel.

What is a flaky test, and how do you deal with one?

A flaky test is a test that sometimes passes and sometimes fails on the same code. Common causes are timing, shared data, order dependence and unstable environments. Reproduce it by running it many times, read the logs and screenshots, fix the cause, for example by replacing a fixed sleep with an explicit wait, and quarantine it while you work so it does not hide real failures. A retry is a last resort.

Why it is asked: flaky suites lose the team's trust.

How do you test an application programming interface (API) in the same framework?

Send a request with a Hypertext Transfer Protocol (HTTP) client library, check the status code, the headers and the body fields that matter, and keep application programming interface (API) checks separate from user-interface checks. Use API calls to set up data for browser tests, which is faster and steadier than clicking through screens. For isolating a component, use test doubles, which are stand-ins for real services. See the representational state transfer (REST) API interview questions for more.

Why it is asked: modern suites mix layers.

How do you decide what to automate?

Automate checks that are repeated often, stable, important and costly to do by hand, such as login, checkout or a regression pack. Leave one-off checks, rapidly changing screens and exploratory testing to people. A simple method is to rank candidates by how often they run, how much risk they cover and how stable the screen is, and start from the top.

Why it is asked: automating everything wastes effort.

Behavior and closing

Tell me about a framework you built or improved.

Describe the problem, such as slow, flaky tests, your design choices, such as page objects and explicit waits, and the result with numbers, such as run time or failure rate before and after. Use the situation, task, action, result (STAR) structure from the STAR method page, and be clear about which part you did yourself.

Why it is asked: it shows ownership and the ability to measure your own work.

What is your greatest weakness?

Name a real, bounded weakness and the habit you built around it, such as writing the test before checking whether the requirement is clear, and now asking first. Do not name something the job depends on, such as accuracy. See the greatest weakness page.

Why it is asked: it shows self-awareness and whether you act on feedback.

What questions do you have for us?

Ask what the suite covers, how long a full run takes, how failures are triaged, how flaky tests are handled and how testers and developers work together. See questions to ask the interviewer for more.

Why it is asked: it shows you care about the health of the test system.

Follow-up questions

How do I prepare for a Java automation testing interview?

Revise the Java basics above, build a small Selenium project with page objects and explicit waits, and be ready to explain every choice in it. Rehearse the answers out loud. The interview loops guide shows how rounds are arranged, and Java related interview questions gives more practice.

What coding questions are asked in automation testing interviews?

Expect to write code, not only explain it. Typical tasks are reversing a string, finding duplicates in a list, counting characters, or writing a short test with a wait and an assertion. Say your plan before you type, write small steps, and test with an example. If you get stuck, say what you would try next.

How are Python Selenium interview questions different from Java ones?

The Selenium parts (waits, locators, page objects) are the same ideas in either language; what changes is the language basics, such as Java's checked exceptions, static types and collections. Prepare the language the job posting names, and if you know another, show you can read and write the required one.

What system design questions do SDETs get?

SDET stands for software development engineer in test, a tester who writes code. System design for the role usually means sketching a test framework or a test pipeline: how tests are structured, run in parallel, reported and kept stable. Talk through page objects, waits, data set-up and reporting from the answers above.

Key takeaways
  • Java automation interviews test language basics, Selenium habits and framework design.
  • Use equals() for strings and explicit waits for one condition, and do not mix explicit and implicit waits.
  • Put locators and page structure in page objects, and keep assertions in tests.
  • Make each test independent, and treat a flaky test as a bug to fix.
  • For behavioral questions, give one real framework story with before and after numbers.

Sources

  1. Waits, WebDriver documentation — Selenium project (last modified 16 September 2026), 16 Sep 2026
  2. Page object models, Selenium test practices — Selenium project (last modified 16 September 2026), 16 Sep 2026
  3. TestNG annotations — TestNG project (last updated 31 August 2026), 31 Aug 2026
  4. String (Java SE 21 and JDK 21) — Oracle (undated page, read 2026-10-24), 24 Oct 2026
  5. The Catch or Specify Requirement, The Java Tutorials — Oracle (tutorial for JDK 8, copyright 2024), 1 Jan 2024
  6. Abstract Methods and Classes, The Java Tutorials — Oracle (tutorial for JDK 8, copyright 2024), 1 Jan 2024