Bird
Raised Fist0
Selenium Javatesting~20 mins

Excel data reading (Apache POI) in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Excel Data Reading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Apache POI code snippet reading Excel?
Given the following Java code snippet using Apache POI to read an Excel file, what will be printed to the console?
Selenium Java
FileInputStream fis = new FileInputStream("data.xlsx");
Workbook workbook = WorkbookFactory.create(fis);
Sheet sheet = workbook.getSheetAt(0);
Row row = sheet.getRow(1);
Cell cell = row.getCell(2);
System.out.println(cell.getStringCellValue());
workbook.close();
fis.close();
ANullPointerException at getCell
BFileNotFoundException
CNumberFormatException
D"TestValue"
Attempts:
2 left
💡 Hint
Check the cell type and the row/column indices carefully.
❓ assertion
intermediate
1:30remaining
Which assertion correctly verifies the Excel cell value?
You read a cell value from Excel using Apache POI and want to assert it equals "Pass" in a Selenium Java test. Which assertion is correct?
Selenium Java
String actualValue = sheet.getRow(0).getCell(1).getStringCellValue();
AassertTrue(actualValue == "Pass");
BassertEquals("Pass", actualValue);
CassertFalse(actualValue.equals("Pass"));
DassertNull(actualValue);
Attempts:
2 left
💡 Hint
Use the correct assertion method for string equality in Java.
🔧 Debug
advanced
2:30remaining
Why does this Apache POI code throw NullPointerException?
This code throws NullPointerException at runtime. What is the most likely cause?
Selenium Java
Workbook wb = WorkbookFactory.create(new FileInputStream("test.xlsx"));
Sheet sh = wb.getSheetAt(0);
Row r = sh.getRow(5);
Cell c = r.getCell(3);
String val = c.getStringCellValue();
System.out.println(val);
ARow 5 does not exist in the sheet, so getRow(5) returns null.
BCell 3 is numeric, so getStringCellValue() throws NullPointerException.
CWorkbookFactory.create throws NullPointerException if file missing.
DFileInputStream is not closed causing NullPointerException.
Attempts:
2 left
💡 Hint
Check which object is null before calling methods on it.
❓ locator
advanced
2:00remaining
Which locator is best to find the Excel file for reading in Selenium Java?
You want to load an Excel file named "data.xlsx" located in the "resources" folder inside your project root. Which code correctly locates the file for Apache POI reading?
AFileInputStream fis = new FileInputStream(System.getProperty("user.dir") + "/resources/data.xlsx");
BFileInputStream fis = new FileInputStream("resources/data.xlsx");
CFileInputStream fis = new FileInputStream("src/resources/data.xlsx");
DFileInputStream fis = new FileInputStream("/resources/data.xlsx");
Attempts:
2 left
💡 Hint
Use absolute path based on project root for reliable file access.
❓ framework
expert
3:00remaining
How to integrate Apache POI Excel reading in a Selenium TestNG framework for data-driven tests?
Which approach correctly integrates Apache POI to provide test data to a TestNG Selenium test method?
AHardcode Excel data inside @Test method and ignore Apache POI.
BRead Excel inside @Test method and use static variables to share data across tests.
CUse @DataProvider method that reads Excel with Apache POI and returns Object[][] for test method parameters.
DUse Selenium WebDriver to open Excel file and read cells via UI automation.
Attempts:
2 left
💡 Hint
TestNG supports data-driven tests via @DataProvider.

Practice

(1/5)
1. What is the main purpose of using Apache POI in Selenium Java testing?
easy
A. To read and write Excel files for test data management
B. To locate web elements on a webpage
C. To execute JavaScript code in the browser
D. To generate HTML reports for test results

Solution

  1. Step 1: Understand Apache POI's role

    Apache POI is a library used to handle Microsoft Office files, especially Excel, in Java programs.
  2. Step 2: Connect Apache POI with Selenium testing

    In Selenium tests, Apache POI helps read and write Excel files to manage test data efficiently.
  3. Final Answer:

    To read and write Excel files for test data management -> Option A
  4. Quick Check:

    Apache POI = Excel file handling [OK]
Hint: Apache POI = Excel data handling in Java tests [OK]
Common Mistakes:
  • Confusing Apache POI with Selenium WebDriver functions
  • Thinking Apache POI is for web element locating
  • Assuming Apache POI generates test reports
2. Which of the following is the correct way to open an Excel workbook using Apache POI in Java?
easy
A. Workbook workbook = new Workbook("file.xlsx");
B. Workbook workbook = WorkbookFactory.create(new FileInputStream("file.xlsx"));
C. Workbook workbook = Workbook.open("file.xlsx");
D. Workbook workbook = new FileInputStream("file.xlsx");

Solution

  1. Step 1: Identify correct Workbook creation method

    Apache POI uses WorkbookFactory.create(InputStream) to open Excel files safely.
  2. Step 2: Check each option's syntax

    Workbook workbook = WorkbookFactory.create(new FileInputStream("file.xlsx")); correctly uses WorkbookFactory.create with FileInputStream. Others are invalid or incorrect constructors.
  3. Final Answer:

    Workbook workbook = WorkbookFactory.create(new FileInputStream("file.xlsx")); -> Option B
  4. Quick Check:

    Use WorkbookFactory.create with FileInputStream [OK]
Hint: Use WorkbookFactory.create with FileInputStream to open Excel [OK]
Common Mistakes:
  • Trying to instantiate Workbook directly
  • Using wrong constructors or methods
  • Assigning FileInputStream to Workbook variable
3. Given the following code snippet, what will be printed if the Excel cell at row 0, column 1 contains the string "TestData"?
Workbook workbook = WorkbookFactory.create(new FileInputStream("data.xlsx"));
Sheet sheet = workbook.getSheetAt(0);
Row row = sheet.getRow(0);
Cell cell = row.getCell(1);
System.out.println(cell.getStringCellValue());
workbook.close();
medium
A. TestData
B. null
C. 0
D. Throws NullPointerException

Solution

  1. Step 1: Access the correct cell value

    The code accesses row 0, cell 1, which contains the string "TestData".
  2. Step 2: Print the string cell value

    Calling getStringCellValue() on that cell returns "TestData", which is printed.
  3. Final Answer:

    TestData -> Option A
  4. Quick Check:

    Cell value = "TestData" printed [OK]
Hint: getStringCellValue() returns cell text directly [OK]
Common Mistakes:
  • Assuming cell is null and causes exception
  • Using getNumericCellValue() on string cell
  • Confusing row and column indexes
4. Identify the error in the following code snippet that reads an Excel file using Apache POI:
FileInputStream fis = new FileInputStream("data.xlsx");
Workbook workbook = WorkbookFactory.create(fis);
Sheet sheet = workbook.getSheetAt(0);
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);
System.out.println(cell.getStringCellValue());
// Missing workbook.close()
medium
A. WorkbookFactory.create() cannot accept FileInputStream
B. getSheetAt(0) should be getSheet("Sheet1")
C. FileInputStream should be closed after use
D. getStringCellValue() cannot be called on numeric cells

Solution

  1. Step 1: Check resource management

    The code opens FileInputStream but does not close it, which can cause resource leaks.
  2. Step 2: Confirm correct usage of WorkbookFactory and methods

    WorkbookFactory.create accepts FileInputStream, getSheetAt(0) is valid, and getStringCellValue() is fine if cell is string.
  3. Final Answer:

    FileInputStream should be closed after use -> Option C
  4. Quick Check:

    Always close streams to avoid leaks [OK]
Hint: Always close FileInputStream and Workbook after use [OK]
Common Mistakes:
  • Forgetting to close FileInputStream
  • Assuming WorkbookFactory.create rejects streams
  • Confusing sheet access methods
5. You want to read multiple rows from an Excel sheet and store the first column values into a List<String>. Which approach correctly reads all rows except the header row using Apache POI?
hard
A. Use a for loop from 1 to sheet.getPhysicalNumberOfRows(), get each row's cell(1) string value, add to list
B. Use a for loop from 0 to sheet.getLastRowNum(), get each row's cell(0) numeric value, add to list
C. Use a while loop with sheet.getPhysicalNumberOfRows(), get cell(1) string value, add to list
D. Use a for loop from 1 to sheet.getLastRowNum(), get each row's cell(0) string value, add to list

Solution

  1. Step 1: Skip header row and loop through rows

    Start loop at 1 to skip header (row 0), loop until sheet.getLastRowNum() to cover all rows.
  2. Step 2: Access first column cell and get string value

    Get cell at index 0 (first column), call getStringCellValue(), and add to list.
  3. Final Answer:

    Use a for loop from 1 to sheet.getLastRowNum(), get each row's cell(0) string value, add to list -> Option D
  4. Quick Check:

    Loop rows from 1, get cell(0) string values [OK]
Hint: Start loop at 1 to skip header, get cell(0) string values [OK]
Common Mistakes:
  • Starting loop at 0 and including header
  • Using wrong cell index (1 instead of 0)
  • Using numeric value for string cells
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard