Java OCA OCP Practice Question 528

Question

What does the following code fragment print out at line 9?

1. FileOutputStream fos = new FileOutputStream("xx"); 
2. for (byte b=10; b<50; b++) 
3.   fos.write(b); 
4. fos.close(); 
5. RandomAccessFile raf = new RandomAccessFile("xx", "r"); 
6. raf.seek(10); 
7. int i = raf.read(); 
8. raf.close() 
9. System.out.println("i = " + i); 
  • A. The output is i = 30.
  • B. The output is i = 20.
  • C. The output is i = 10.
  • D. There is no output because the code throws an exception at line 1.
  • E. There is no output because the code throws an exception at line 5.


B.

Note

All the code is perfectly legal, so no exceptions are thrown.

The first byte in the file is 10, the next byte is 11, the next is 12, and so on.

The byte at file position 10 is 20, so the output is i = 20.




PreviousNext

Related