Microsoft Exam Questions

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”);
You need to ensure that these e-mail addresses are stored in the Email.dat file so that you can load them again
when the user restarts the application.
What should you do?

A.
Add the following code:
FileStream stream = new FileStream (“Email.dat”, FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter ();
formatter.Deserialize(stream, emailAddresses);

B.
Add the following code:
FileStream stream = new FileStream (“Email.dat”, FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter ();
formatter.Serialize(stream, emailAddresses);

C.
Add the following code:
FileStream stream = new FileStream (“Email.dat”, FileMode.Create);
stream.Serialize(emailAddresses);

D.
Add the following code:
FileStream stream = new FileStream (“Email.dat”, FileMode.Create);
stream.WriteObject(emailAddresses);

Explanation:
This code instantiates a file stream, instantiates a BinaryFormatter object, and serializes the emailAddresses object to the Email.dat file. The FileStream constructor takes a file path string and FileMode enumeration as arguments. The Serialize method of the BinaryFormatter class takes two arguments, a stream and the object to be serialized.
The Serialize method uses the stream to write the object to the destination.
Incorrect Answers:
A: You should not add the code that invokes the Deserialize method of the BinaryFormatter class because you must serialize the object first. C D: You should not add the code fragments that do not instantiate the BinaryFormatter object
because the WriteObject and Serialize methods do not exist in the FileStream class.