What will be the output after execution of the following commands on a file, consider 'f' as the file pointer?
f.seek(10)
f.read(1)
f.read(1)
f.tell()
What will be the output after execution of the following commands on a file, consider 'f' as the file pointer?
f.seek(10)
f.read(1)
f.read(1)
f.tell()
Solution
f.seek(10) moves the file pointer to position 10. Each f.read(1) reads one character and moves the pointer forward by 1, so after two reads it is at . f.tell() returns the current pointer position, which is 12.
Option 1: this is the position right after seek(10), before any reads.
Option 2: this is the position after only one read(1).
Option 4: this comes from counting from position 11 or adding an extra read; there are only two reads.
tell() returns the position of the next byte to be read, not the last byte read.
Related questions:
2025: 14 May Shift 1
2025: 30 May Shift 2
2025: 20 May Shift 1