Upload
fellowbuddycom
View
114
Download
0
Embed Size (px)
Citation preview
www.fellowbuddy.com
Simplifying Students Life
Serialization in .NET
Q.1 According to given below statements, choose the correct option regarding serialization.
Statement 1: Serialization is the process of converting information into a byte stream that can be
stored or transferred.
Statement 2: Deserialization is the process of converting a serialized sequence of bytes into an
object.
Statement 3: Serialization is the process of converting a serialized sequence of bytes into an
object.
Statement 4: DeSerialization is the process of converting information into a byte stream that can
be stored or transferred.
A) Statement 1 and 2 are correct.
B) Statement 3 and 4 are correct.
C) Statement 1 and 4 are correct.
D) Statement 2 and 3 are correct.
Q.2 What are the steps to serialize an object?
A)
- Create a stream object.
- Create a BinaryFormatter Object.
- Call the BinaryFormatter.Serialize method.
B)
- Create a BinaryFormatter Object.
- Call the BinaryFormatter.Serialize method.
C)
- Create a stream object.
- Create a BinaryFormatter Object.
- Call the BinaryFormatter.Deserialize method.
D) None of the above.
www.fellowbuddy.com
Simplifying Students Life
Q.3 Which of the following attributes should you add to a class to enable it to be serialized?
A) ISerializable
B) Serializable
C) SoapInclude
D) OnDeserialization
Q.4 Which of the following interfaces should you implement to enable you to run a method
after an instance of your class is serialized?
A) IFormatter
B) ISerializable
C) IDeserializationCallback
D) IObjectReference
Q.5 How many types of Serialization .NET supports?
A) Binary Serialization
B) SOAP Serialization
C) XML Serialization
D) All of the above.
Q.6 According to given below statements, choose the correct option regarding XML
serialization.
Statement 1: XML serialization provides the interoperability to communicate with different
platforms.
Statement 2: XML serialization cannot be used to serialize private data.
Statement 3: XML serialization can be used to serialize private data.
Statement 4: XML serialization does not provides the interoperability to communicate with
different platforms.
A) Only statement 1 is correct.
B) Statement 1 and 2 are correct.
C) Statement 3 and 4 are correct.
D) Only statement 4 is correct.
Q.7 Which of the following attributes would you use to cause a member to be serialized as
an attribute, rather than an element?
A) XmlAnyAttribute
www.fellowbuddy.com
Simplifying Students Life
B) XMLType
C) XMLElement
D) XMLAttribute
Q.8 Which of the following attributes should you add to a member to prevent it from being
serialized by BinaryFormatter?
A) NonSerialized
B) Serializable
C) SerializationException
D) SoapIgnore
Ans. A
Q.9 Which of the following attributes should you add to a member to prevent it from being
serialized by XML serialization?
A) XMLType
B) XMLIgnore
C) XMLElement
D) XMLAttribute
Q.10 Which of the following are requirements for a class to be serialized with XML
serialization?
A) The class must be public.
B) The class must be private.
C) The class must have a parameterless constructor.
D) Option A and C are correct.
Q.11 What is/are the advantages of XML serialization? Choose the correct option.
A) Objects serialized by using XML are self-describing and easily processed.
B) An object that is serialized by using XML can be easily processed by an application written
for a different operating system in a different technology.
C) Option A and B both are correct.
D) None of the above.
Q.12 What are the steps to serialize an object by using XML Serialization?
A)
www.fellowbuddy.com
Simplifying Students Life
- Create an XmlSerializer object by passing it the type of object you plan to serialize.
- Call the XmlSerializer.Serialize method to serialize the object.
B)
- Create a stream (TextWriter, or XmlWriter) object to hold the serialized output.
- Create an XmlSerializer object by passing it the type of object you plan to serialize.
- Call the XmlSerializer.Serialize method to serialize the object.
C)
- Create a stream (TextWriter, or XmlWriter) object to hold the serialized output.
- Call the XmlSerializer.Serialize method to serialize the object.
D) None of the above.
Q.13 Trace the output of given below code.
[XmlRoot ("CartItem")]
public class ShoppingCart
{
[XmlAttribute] public Int32 productId;
public decimal price;
public Int32 quantity;
[XmlIgnore] public decimal total;
public ShoppingCart()
{
}
}
Assume that productID=100, price=100.25 quantity=20.
A)
<?xml version="1.0" ?>
<CartItem productId="100">
<price>100.25</price>
<quantity>20</quantity>
www.fellowbuddy.com
Simplifying Students Life
</CartItem>
B)
<?xml version="1.0" ?>
< ShoppingCart productId="100">
<price>100.25</price>
<quantity>20</quantity>
</ ShoppingCart >
C)
<?xml version="1.0" ?>
<CartItem>
< productId="100">
<price>100.25</price>
<quantity>20</quantity>
</CartItem>
D) None of the above.
Answers
1. ANSWER: A
2. ANSWER: A
Example:
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
class Program
{
static void Main(string[] args)
{
string data = "Welcome to the CareerRide.com .";
// Create file to save the data
FileStream fs = new FileStream("c:\\data.txt", FileMode.Create);
// Create a BinaryFormatter object to perform the serialization process
BinaryFormatter obj = new BinaryFormatter();
www.fellowbuddy.com
Simplifying Students Life
// Use the BinaryFormatter object to serialize the data to the file
obj.Serialize(fs, data);
// Close the file
fs.Close();
}
}
If you run the above code and open the Data.txt file, you will see the contents of the string
you stored surrounded with binary information to describe the data for the deserialization.
3. ANSWER: B
Explanation:
If you apply Serializable attribute to a type, it indicate that instances of this type can
be serialized. If you do not want a field within your class to be serializable, apply the
NonSerializedAttribute attribute.
4. ANSWER: C
5. ANSWER: D
6. ANSWER: B
7. ANSWER: D
8. ANSWER: A
Explanation:
If you want that particular field should not be serialized then use NonSerialized
attribute.
[Serializable]
public class Employee
{
public int EmpID;
[NonSerialized] public int TotalSalary;
public String EmpName;
}
9. ANSWER: B
Explanation:
Use the XMLIgnore attribute to prevent a member from being serialized during
XML serialization.
10. ANSWER: D
11. ANSWER: C
12. ANSWER: B
Explanation:
www.fellowbuddy.com
Simplifying Students Life
using System;
using System.IO;
using System.Xml.Serialization;
class Program
{
static void Main(string[] args)
{
FileStream fs = new FileStream("C:\\Date.XML", FileMode.Create);
// Create an XmlSerializer object to perform the serialization
XmlSerializer XMLObj = new XmlSerializer(typeof(DateTime));
// Use the XmlSerializer object to serialize the data.
XMLObj.Serialize(fs, System.DateTime.Now);
// Close the file
fs.Close();
}
}
13. ANSWER: A