Java Interview Written Test Questions

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?

ComponentPurpose
JVMExecutes bytecode
JREJVM + Libraries
JDKJRE + 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?

HashMapHashSet
Stores key-value pairsStores only values
Allows one null keyAllows one null value

Exception Handling

8. Checked vs Unchecked exceptions?

CheckedUnchecked
Compile-timeRuntime
IOExceptionNullPointerException

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?

ThreadRunnable
Extends ThreadImplements Runnable
Single inheritanceMultiple 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 WaitExplicit Wait
GlobalElement specific
Less controlMore 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:

  1. Call API (RestAssured)
  2. Fetch DB records (JDBC)
  3. 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 testRuns 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)

TopicKey Points
OOPEncapsulation, Inheritance
CollectionsList, Set, Map
SeleniumLocators, waits
TestNGPriority, Groups
JavaJVM, 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.

Leave a Comment

Your email address will not be published. Required fields are marked *