Introduction: Why Java Is Needed for Automation Testing
Java is one of the most widely used programming languages for automation testing, especially in Selenium-based frameworks. In Java interview written test questions, candidates are evaluated on:
- Core Java fundamentals
- Object-Oriented Programming (OOP)
- Collections & Exception handling
- Multithreading concepts
- Selenium WebDriver integration
- Test frameworks like TestNG and JUnit
- Real-time automation scenarios
Most written tests are logic-oriented, focusing on output-based questions, code snippets, and scenario reasoning, not just theory.
If you master Java fundamentals, automation frameworks become easy to design, debug, and scale.
Core Java Topics for Testing (Must-Know Areas)
1. Object-Oriented Programming (OOP)
- Inheritance
- Polymorphism
- Encapsulation
- Abstraction
- Interface vs Abstract class
2. Java Collections
- List, Set, Map
- ArrayList vs LinkedList
- HashMap vs Hashtable
- Iterator vs ListIterator
3. Exception Handling
- Checked vs Unchecked
- try-catch-finally
- throw vs throws
- Custom exceptions
4. Multithreading
- Thread vs Runnable
- Synchronization
- Deadlock
- wait(), notify()
5. Java Streams & Lambda
- filter(), map(), reduce()
- Functional interfaces
- Method references
Java Interview Written Test Questions & Answers (Core + Automation)
Core Java – Fundamentals
1. What is JVM?
JVM (Java Virtual Machine) executes Java bytecode and provides platform independence.
2. Difference between JDK, JRE, and JVM?
| Component | Purpose |
| JVM | Executes bytecode |
| JRE | JVM + Libraries |
| JDK | JRE + Development tools |
3. What is platform independence in Java?
Java code is compiled into bytecode which runs on any JVM.
OOP Written Test Questions
4. What is abstraction?
Hiding implementation and showing only functionality.
abstract class Vehicle {
abstract void start();
}
class Car extends Vehicle {
void start() {
System.out.println(“Car started”);
}
}
Output:
Car started
5. Can we achieve multiple inheritance in Java?
Yes, using interfaces.
Collections – Output Based Questions
6. What will be the output?
List<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
list.add(10);
System.out.println(list);
Output:
[10, 20, 10]
(ArrayList allows duplicates)
7. Difference between HashMap and HashSet?
| HashMap | HashSet |
| Stores key-value pairs | Stores only values |
| Allows one null key | Allows one null value |
Exception Handling
8. Checked vs Unchecked exceptions?
| Checked | Unchecked |
| Compile-time | Runtime |
| IOException | NullPointerException |
9. What will be output?
try {
int a = 10 / 0;
} catch (Exception e) {
System.out.println(“Exception”);
} finally {
System.out.println(“Finally”);
}
Output:
Exception
Finally
Multithreading Written Test Questions
10. Thread vs Runnable?
| Thread | Runnable |
| Extends Thread | Implements Runnable |
| Single inheritance | Multiple inheritance possible |
11. What is synchronization?
Prevents multiple threads from accessing shared resources simultaneously.
Java Streams
12. Filter even numbers
List<Integer> nums = Arrays.asList(1,2,3,4);
nums.stream()
.filter(n -> n % 2 == 0)
.forEach(System.out::println);
Output:
2 4
Selenium WebDriver Java Coding Questions
Locators
13. Write XPath for dynamic element
//input[contains(@id,’email’)]
Implicit vs Explicit Wait
14. Difference?
| Implicit Wait | Explicit Wait |
| Global | Element specific |
| Less control | More control |
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(“login”)));
Selenium Output Based Question
15. What happens if element not found?
Throws NoSuchElementException.
Java Selenium Coding Challenges (Written Test Style)
16. Launch browser and open URL
WebDriver driver = new ChromeDriver();
driver.get(“https://example.com”);
System.out.println(driver.getTitle());
17. Click login button using CSS selector
driver.findElement(By.cssSelector(“button.login”)).click();
Real-Time Automation Interview Scenarios
Scenario 1: Page Object Model (POM)
Question: Why use POM?
Answer:
- Improves maintainability
- Separates UI and test logic
- Reusable components
public class LoginPage {
WebDriver driver;
By username = By.id(“user”);
By password = By.id(“pass”);
public void login(String u, String p) {
driver.findElement(username).sendKeys(u);
driver.findElement(password).sendKeys(p);
}
}
Scenario 2: API + Database Validation
Problem:
Validate API response data with database.
Approach:
- Call API (RestAssured)
- Fetch DB records (JDBC)
- Compare values using assertions
JUnit Interview Written Test Questions
18. What is @Test?
Marks method as test case.
19. Difference between @Before and @BeforeClass?
| @Before | @BeforeClass |
| Runs before each test | Runs once |
@Before
public void setup() {
System.out.println(“Before test”);
}
TestNG Interview Written Test Questions
20. Why TestNG over JUnit?
- Parallel execution
- Annotations
- DataProvider
- Grouping
21. Priority execution
@Test(priority = 1)
public void loginTest() {}
Selenium + Java + API Practical Example
Response response = RestAssured.get(“/users”);
Assert.assertEquals(response.getStatusCode(), 200);
Framework Design Interview Questions
22. What is Hybrid Framework?
Combination of:
- Data-Driven
- Keyword-Driven
- POM
23. CI/CD tools used?
- Jenkins
- GitHub Actions
- Maven
Common Mistakes in Java Interview Written Tests
- Ignoring output-based questions
- Weak OOP concepts
- Poor exception handling knowledge
- Confusing wait mechanisms
- No framework understanding
1-Page Java Revision Notes (Quick Table)
| Topic | Key Points |
| OOP | Encapsulation, Inheritance |
| Collections | List, Set, Map |
| Selenium | Locators, waits |
| TestNG | Priority, Groups |
| Java | JVM, Memory |
FAQs – Java Interview Written Test Questions
Q1. Are Java written tests difficult?
No, if fundamentals are strong.
Q2. Focus more on theory or coding?
Coding + output-based questions.
Q3. Is Selenium mandatory?
Yes for automation roles.
Q4. How many questions to practice?
At least 200+ Java interview written test questions.
