Java Coding Test Interview Questions

Introduction: Why Java Is Needed for Automation Testing

Java is one of the most frequently tested languages in coding interviews, especially for automation testing and SDET roles. In most companies, java coding test interview questions focus on two things:

  1. Problem-solving ability using Core Java
  2. Applying Java logic to automation testing scenarios

Java is widely used in automation testing because it:

  • Supports object-oriented programming, enabling reusable automation code
  • Integrates seamlessly with Selenium WebDriver
  • Works well with JUnit, TestNG, Maven, Jenkins
  • Handles API testing, database validation, and file handling
  • Scales efficiently for enterprise automation frameworks

Coding tests often include output-based questions, logic problems, and small automation-related programs.


Core Java Topics for Testing (Coding Test Focus)

1. Object-Oriented Programming (OOP)

  • Encapsulation for reusable methods
  • Inheritance for base classes
  • Polymorphism with WebDriver
  • Abstraction using interfaces

2. Java Collections

  • List, Set, Map operations
  • HashMap for test data
  • Iteration and filtering logic

3. Multithreading

  • Thread vs Runnable
  • Parallel execution concepts
  • Thread safety basics

4. Exception Handling

  • Checked vs unchecked exceptions
  • try-catch-finally
  • Custom exceptions

5. Java Streams

  • filter(), map(), reduce()
  • Data validation
  • Clean and concise logic

Java Coding Test Interview Questions & Detailed Answers

Core Java Coding Questions

1. Reverse a String

String s = “Java”;

String rev = “”;

for(int i = s.length() – 1; i >= 0; i–) {

    rev = rev + s.charAt(i);

}

System.out.println(rev);

Output:
avaJ


2. Check Palindrome String

String s = “madam”;

String rev = new StringBuilder(s).reverse().toString();

System.out.println(s.equals(rev));

Output:
true


3. Find Factorial of a Number

int n = 5;

int fact = 1;

for(int i = 1; i <= n; i++) {

    fact *= i;

}

System.out.println(fact);

Output:
120


4. Print Fibonacci Series

int a = 0, b = 1;

for(int i = 1; i <= 5; i++) {

    System.out.print(a + ” “);

    int c = a + b;

    a = b;

    b = c;

}

Output:
0 1 1 2 3


5. Find Largest Number in Array

int[] arr = {10, 45, 3, 99};

int max = arr[0];

for(int n : arr) {

    if(n > max) max = n;

}

System.out.println(max);

Output:
99


6. Count Duplicate Characters

String s = “automation”;

Map<Character, Integer> map = new HashMap<>();

for(char c : s.toCharArray()) {

    map.put(c, map.getOrDefault(c, 0) + 1);

}

System.out.println(map);

Output (example):
{a=2, u=1, t=2, o=2, m=1, i=1, n=1}


7. Remove Duplicate Elements from List

List<Integer> list = Arrays.asList(1,2,2,3,3);

Set<Integer> set = new HashSet<>(list);

System.out.println(set);

Output:
[1, 2, 3]


8. Difference Between == and equals()

String a = new String(“Java”);

String b = new String(“Java”);

System.out.println(a == b);

System.out.println(a.equals(b));

Output:

false

true


9. Exception Handling Example

try {

    int a = 10 / 0;

} catch (ArithmeticException e) {

    System.out.println(“Exception handled”);

}

Output:
Exception handled


10. Read Data from File

File file = new File(“test.txt”);

Scanner sc = new Scanner(file);

while(sc.hasNextLine()) {

    System.out.println(sc.nextLine());

}


Java Coding Test Questions (Automation Context)

11. Launch Browser and Print Title

WebDriver driver = new ChromeDriver();

driver.get(“https://example.com”);

System.out.println(driver.getTitle());

driver.quit();

Expected Output:
Page title printed


12. Click Login Button Using CSS Selector

driver.findElement(By.cssSelector(“button.login”)).click();


13. Explicit Wait Example

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(“username”)));


Java Selenium Coding Challenges

Locators

14. Types of Selenium Locators

  • ID
  • Name
  • ClassName
  • XPath
  • CSS Selector

Dynamic XPath

//input[contains(@id,’email’)]


Waits Difference

Implicit WaitExplicit Wait
GlobalElement-specific
Less controlMore reliable

Real-Time Interview Scenarios

Scenario 1: Page Object Model (POM)

Problem:
Design a login page using POM.

public class LoginPage {

    WebDriver driver;

    By username = By.id(“user”);

    By password = By.id(“pass”);

    public LoginPage(WebDriver driver) {

        this.driver = driver;

    }

    public void login(String u, String p) {

        driver.findElement(username).sendKeys(u);

        driver.findElement(password).sendKeys(p);

    }

}


Scenario 2: API + Database Validation

Approach:

  1. Call API using RestAssured
  2. Validate status code
  3. Fetch data from DB using JDBC
  4. Compare API and DB values

JUnit Coding Test Interview Questions

15. Simple JUnit Test

@Test

public void testAdd() {

    assertEquals(5, 2 + 3);

}


Common JUnit Annotations

AnnotationPurpose
@TestTest case
@BeforeEachRuns before test
@AfterEachRuns after test

TestNG Coding Test Questions

16. TestNG Priority Example

@Test(priority = 1)

public void loginTest() {}


DataProvider Example

@DataProvider

public Object[][] data() {

    return new Object[][] {{“admin”,”pass”}};

}


Selenium + Java + API Practical Example

Response response = RestAssured.get(“/users/1”);

Assert.assertEquals(response.getStatusCode(), 200);


Framework Design Coding Test Questions

17. What is Hybrid Framework?

Combination of:

  • Page Object Model
  • Data-Driven framework
  • Keyword-Driven framework

18. CI/CD Tools Used

  • Git
  • Maven
  • Jenkins
  • GitHub Actions

19. Role of Cucumber

  • BDD approach
  • Feature files
  • Step definitions

Common Mistakes in Java Coding Interviews

  • Weak Core Java fundamentals
  • Ignoring edge cases
  • Overusing Thread.sleep()
  • Poor exception handling
  • No clarity on framework design

1-Page Java Coding Revision Notes

TopicFocus
Java BasicsLoops, Strings
CollectionsList, Map
SeleniumLocators, Waits
TestingJUnit, TestNG
FrameworkPOM, Hybrid

FAQs – Java Coding Test Interview Questions

Q1. Are Java coding tests difficult?
No, if fundamentals and logic are strong.

Q2. What type of questions are asked most?
String manipulation, arrays, collections, and automation basics.

Q3. Is Selenium coding mandatory?
Yes, for automation roles.

Q4. How many programs should I practice?
At least 200+ java coding test interview questions.

Leave a Comment

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