Java Programming Tasks
Filed under TCS
Free read 2 of 3Understand the exact requirements for file writing (e.g., append vs overwrite) and string manipulation helper methods.
The question
Write Java methods to reverse a given string, check if string b exists as a substring in string a, and write text content to a file.
Constraints
- Input strings may contain alphanumeric and special characters.
- File writing should handle standard IO exceptions securely.
Examples
- Input
- a = "helloworld", b = "world"
- Output
- Reversed: "dlrowolleh", Substring exists: true
- Why
- The substring 'world' is found starting at index 5 in 'helloworld'.
Approach
Understand the exact requirements for file writing (e.g., append vs overwrite) and string manipulation helper methods.
Use standard loops for reversal and indexOf for substring matching.
Use StringBuilder for efficient string reversal and containment checks, and FileWriter/BufferedWriter for file writing.
Null inputs, empty files, and permission denied scenarios for file writing.
Tips
- What they are testing: Test with empty strings, special characters, and non-existent file paths to ensure robust exception handling.
- Common mistake: Not properly closing file streams or ignoring IOException propagation during file operations.
Related TCS questions
Collected from public candidate posts and coding forums. Updated Nov 2026.