What segment should you add?

You work as the application developer at Domain.com. Domain.com uses Visual Studio.NET 2005 as its application development platform.
You are developing a .NET Framework 2.0 text-processing application. You have access to an array of bytes named ckArray that contains your data.
You are busy writing code that will be used to write the contents of the array to a disk file.
If you are done with the write operation you also display the contents of the stream on the console to make sure that the write operation completes successfully.
The code segment to read and write from the stream is shown below and the line numbers are reference only:
01: Using fStream As FileStream = New FileStream(“ckFile.txt”, FileMode.Create)
02: For i As Integer = 0 To ckArray.Length
03: fStream.WriteByte(ckArray(i))
04: Next i
05: ‘Add code segment here
06: For i As Integer = 0 To fStream.Length
07: Console.WriteLine(fStream.ReadByte())
08: Next i
09: End Using
You add the appropriate code at line 05 to correctly print the contents of the stream.
What segment should you add?

You work as the application developer at Domain.com. Domain.com uses Visual Studio.NET 2005 as its application development platform.
You are developing a .NET Framework 2.0 text-processing application. You have access to an array of bytes named ckArray that contains your data.
You are busy writing code that will be used to write the contents of the array to a disk file.
If you are done with the write operation you also display the contents of the stream on the console to make sure that the write operation completes successfully.
The code segment to read and write from the stream is shown below and the line numbers are reference only:
01: Using fStream As FileStream = New FileStream(“ckFile.txt”, FileMode.Create)
02: For i As Integer = 0 To ckArray.Length
03: fStream.WriteByte(ckArray(i))
04: Next i
05: ‘Add code segment here
06: For i As Integer = 0 To fStream.Length
07: Console.WriteLine(fStream.ReadByte())
08: Next i
09: End Using
You add the appropriate code at line 05 to correctly print the contents of the stream.
What segment should you add?

A.
fStream.Seek(0, SeekOrigin.End)

B.
fStream.Position = fStream.Length

C.
fStream.Seek(0, SeekOrigin.Current)

D.
fStream.Seek(0, SeekOrigin.Begin)

Explanation:
Because after every write operation is completed you need to reposition the stream so that you can read the contents from the beginning which is done with the statement in the answer, the first parameter will specify the offset, the second the reference point for the seek operation and the value SeekOrigin.Begin indicates that the reader should be positioned at the beginning of the stream.
Incorrect Answers:
A, C: The method in the statement is incorrect because you are referencing the end or the current part of the stream which you are trying to manipulate.
B: This statement should not be used because it sets the current position of the stream to its length effectively referencing the end of the stream.



Leave a Reply 1

Your email address will not be published. Required fields are marked *


networkmanagers

networkmanagers

Correct answer is D