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
Step 1: Check resource management
The code opens FileInputStream but does not close it, which can cause resource leaks.
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.
Final Answer:
FileInputStream should be closed after use -> Option C
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
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.
Step 2: Access first column cell and get string value
Get cell at index 0 (first column), call getStringCellValue(), and add to list.
Final Answer:
Use a for loop from 1 to sheet.getLastRowNum(), get each row's cell(0) string value, add to list -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.