Java Module 13
Files, I/O and NIO.2 interview questions
Interview questions on files, I/O and NIO.2 from Java: short warm-ups, then what each harder question tests, the first step of a good answer and its follow-ups.
Warm-up questions
Short questions with short answers, each answered in full.
-
Fresher
Why does line.split(",") fail on real CSV files, and what may a quoted field contain under RFC 4180?
Questions with model answers
What each question tests, the first step of a good answer and the follow-ups to expect.
-
Mid to senior
A nightly job imports CSV statements from three banks. Some records come out with shifted columns, and one night 4,000 transactions were missing although the file was complete. How do you find the causes, and how do you make the import robust?
- What it tests
- How quoted commas, line breaks inside quotes, byte order marks and unclosed quotes break CSV readers, and an import that reports bad records with positions instead of shifting or losing data.
- First step
- Treat the two symptoms as two bugs. Count the fields of every record with a real CSV parser: records with more fields than the header were split at commas inside quotes, such as Indian-grouped amounts. Then compare the bank's transaction ids with the stored ones to find where the missing run starts.
- Follow-ups
-
- One bank's file starts with a byte order mark. What breaks, and where do you fix it?
- Should the import stop at the first bad record or skip it?
- How would you test the import so that the same failure cannot return?
From the lesson CSV, properties and structured text
Model answer, Pro. Model answers open with any pass; prices are in the notice on this page.Opening the model answers with your pass…
Model answers open with any MySmartCoPilot pass.
- Pro pass · 1 week₹79$2.99
- 30 days₹179$5.99
- 1 year₹1,699$45
One payment; nothing renews unless you choose to. The price shown is the total you pay: no fees or charges are added at checkout.
Passes can’t be bought right now, so the model answers can’t be opened at the moment. If you have a pass, enter your licence key.
It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).
In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Fresher to mid
A Java service loads Hindi labels with Properties.load(new FileInputStream(file)). The file looks right in the editor, but the screen shows text like नम. Explain the cause, and fix it so that it cannot come back.
- What it tests
- How load(InputStream) decodes bytes as ISO 8859-1, why UTF-8 text turns into this pattern, and the choice between a UTF-8 Reader, escapes and a ResourceBundle.
- First step
- The file is fine; the way it is read is wrong. load(InputStream) is specified to decode every byte as one ISO 8859-1 character, and each Devanagari character is three bytes in UTF-8, so it becomes three Latin-1 characters, which is exactly the ठpattern on the screen.
- Follow-ups
-
- The labels move into a ResourceBundle. Does the problem stay?
- A build step writes the file with store(). What does the file look like?
- How do you stop the bug from returning in another module?
From the lesson CSV, properties and structured text
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
Your team adds a "Download as CSV" button for transaction reports that users open in spreadsheets, and a nightly CSV feed for a partner's system. Do you write the CSV code yourself or use a library? Defend the choice and say what each file must handle.
- What it tests
- Weighing hand-written code against a library, RFC 4180 quoting, formula injection in spreadsheet exports, encodings and line endings, and the difference between files for people and files for programs.
- First step
- Separate the two files by their reader: a program reads the feed, people read the download in a spreadsheet. List what each needs, quoting for both, a formula guard only for the download, streaming for large reports, then decide which option covers that list with the fewest risks.
- Follow-ups
-
- A customer's narration starts with = and the spreadsheet shows a computed value. What do you change?
- The partner's parser rejects quoted fields. What do you do?
- A report has 20 lakh rows. What changes in the implementation?
From the lesson CSV, properties and structured text
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes