PrepAway - Latest Free Exam Questions & Answers

What should you do?

You have recently written the code shown below:
Hashtable emailAddresses = new Hashtable ();
emailAddresses.Add (“Mia”, “mia@ Domain.com”);
emailAddresses.Add (“Andy”, “andy@ Domain.com”);
emailAddresses.Add (“Kara”, “kara@ Domain.com”);
FileStream stream = new FileStream (“Email.dat”, FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter ();
formatter.Serialize (stream, emailAddresses);
You need to ensure that you are able to load the emailAddresses object from the Email.dat file into your application.
What should you do?

PrepAway - Latest Free Exam Questions & Answers

A.
Use the following code:
FileStream readStream = new FileStream(“Email.dat”, FileMode.Open);
HashTable loadEmails = readStream.Deserialize();

B.
Use the following code:
FileStream readStream = new FileStream (“Email.dat”, FileMode.Open);
BinaryFormatter readFormatter = new BinaryFormatter();
HashTable loadEmails = readFormatter.Deserialize(readStream);

C.
Use the following code:
FileStream readStream = new FileStream(“Email.dat”, FileMode.Open);
BinaryFormatter readFormatter = new BinaryFormatter ();
HashTable loadEmails = (HashTable)readFormatter.Deserialize(readStream);

D.
Use the following code:
FileStream readStream = new FileStream (“Email.dat”, FileMode.Open);
HashTable loadEmails = (HashTable)readFormatter.ReadObject ();

Explanation:
This instantiates a BinaryFormatter object, and deserializes the emailAddresses object from the Email.dat file.
The FileStream constructor takes a file path string and FileMode enumeration value as arguments.
The Deserialize method of the BinaryFormatter class takes the stream of the object to be deserialized and returns a generic object.
This generic object must be cast or converted to the HashTable data type.
Incorrect Answers:
A, D: You should not use the code fragments that do not instantiate the BinaryFormatter object
because the ReadObject and Deserialize methods do not exist in the FileStream class.
B: You should not use the code that does not cast or convert the return value of the Deserialize method because the Deserialize method returns a generic object.

One Comment on “What should you do?


Leave a Reply