Friday, 8 August 2014

Technical Test Paper 4 on C# with MCQ's

     Technical Test Paper 4 with MCQ's

(Serialization,Exception Handling,Reflection)

1.Select whichamong is NOT an exception?
a)stackOverflow
b)arithmeticOverflow or underflow
c)Incorrect Arithmetic Expression
d)All of the above mentioned

Ans:- c) Incorrect Arithmatic Expression

2.Select which among is NOT considered as .NET Exception class?
a)Exception
b)StackUnderflowException
c)File Found Exception
d)Divide By Zero Exception

Ans:- c) File Found Exception

3.Select the statements which describes correctly usage of exception handling over conventional error handling approaches?
a.As errors can be ingnored but exception cannot be ignored
b)Exception handling allows seperation of progaram's logic from error handling logic making software more reliable and maintainable.
c)try-catch-finally structure allows guaranteed cleanup in event of errors under all circumstances
d) All of the above mentionaed

Ans:- d) All of the above mentionaed

4.Select the correct statement about an Exception?
a)It occurs during loading of program
b)It occurs during Just-In-Time compilation
c)It occurs at run-time
d)all of the above mentioned

Ans:- c) It occures at run time.

5.Which of these keywords is used to manually throw an exception?
a)try
b)finally
c)throw
d)catch

Ans:-throw

6.Choose the correct for given set of code:
 class program
 {
     static void main(String []args)
     {
         int i=5;
         int v=40;
         int[]p=new int[4];
      try
       {
         p[i]=v;
        }
      catch(IndexOutOfRangeException e)
       {
         Console.WriteLine("Index out of bounds");
       }
         Console.WriteLine("Remaining program");
       }
  }


a)value 40 will be assigned to a[ 5 ]:
b)The output will be:
    Index out of bounds
    Remaining program
c) The output will be:
    Remaining program
d) None of the above mentioned

Ans:-   Index out of bounds
           Remaining program

7.Which is correct satement about exception handling in C#.NET?

a)finally clause is used to perform cleanup operatinos of closing network and database connections
b)a program can contain multiple finally clauses
c)The statements in final clause will get executed no matter whether an exception occurs or not
d)All of the above mentioned

Ans:- a & b

8.Coose the correct output for given set of code:
static void main(string[]args)
{
  try
   {
     Console.WriteLine("csharp"+""+1/Convert.ToInt32(0));
    }
  catch(ArithmeticException e)
   {
         Console.WriteLine("Java");
   }
 Console.ReadLine();
}

a)csharp
b)java
c)Run time error
d)csharp 0

Ans:- java


9.Which feature enables to obtain information about use and capabilities of runtime?

a)Runtime type ID
b)Reflection
c)Attributes
d)None of the mentionaed

Ans:- Reflection

10.Choose the namespace which consists of classes that are part of .NET Reflection API:

a)System.Text
b)Syatem.Name
c)Syatem.Reflection
d)None of the mentioned

  Ans:-System .Reflection

11.Choose the correct statement about System.Type namespace:

a)Core the reflection subsystem as encapsulates a type
b)Consist of many methods and properties taht can be obtain information about a type at run-time
c)Both a&b
d)Only b

Ans:- c) Both a&b

12.Choose the class From which the namespace 'System.Type' is derived:

a)Systems.Reflection
b)System.Reflection.MemberInfo
c) Both a&b
d)None of the mentioned

Ans:-b)System.Reflection.MemberInfo

13.What does the following deceleration specifies?
a)Returns an array of MethodInfo object
b)Returns a list of the public methods supported by the type by using Getmethods( )
c)Both a&b
d)None of the mentioned

Ans:-1.Returns an array of MethodInfo objects.
           2.Return a list of public methods supported by type using Getmethods( )

14.Explain Serialization and Deserialization in C#? 
Ans:-
  • Serialization:-It is the process of converting an object into stream of bytes.
  • Deserialization:-Deserialization is the process of converting stream of bytes into object.
  • There are 2 types of serialization.
           1. Binary Serialization.
           2. XML Serialization.


  • We need to use namespace:
             1. System.Runtime.Serialization
             2.System.Runtime.Serialization.

  • If we have to use string data, then there is no need  of serializable.
  • It is used in case of complex data.
Program :- To store object in file and

read it using serialization.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace Serialization_eg

{

class Program

{

static void Main(string[] args)

{

//Serialization

FileStream fs = new

FileStream("C:\\SDemo.txt",

FileMode.Create);

BinaryFormatter obj = new

BinaryFormatter();

obj.Serialize(fs, DateTime.Now);

fs.Close();

//DeSerialization

FileStream fs = new

FileStream("C:\\SDemo.txt",

FileMode.Open);

BinaryFormatter obj = new

BinaryFormatter();

DateTime dt =

(DateTime)(obj.Deserialize(fs));

Console.WriteLine(dt.Date);

}

}

}

Program :- To Serialize Our Own Class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace Serialize_own_class


[Serializable]


class Product
{

public string Name;
public double Price;
public int Quantity;
public double Total;
public Product(string n, double p, int q)
{


Name = n;
Price = p;
Quantity = q;
Total = Price + Quantity;
}

}

class Program
{
static void Main(string[] args)
{

// Serialization\\\\\\\\\

Product p = new Product("Comp", 1000, 5);

FileStream fs = new FileStream("C:\\SerDemo.txt",
FileMode.Create);

BinaryFormatter obj = new BinaryFormatter();

obj.Serialize(fs, p);
fs.Close();

//Deserialization\\\\\\

FileStream fs = new
FileStream("C:\\SerDemo.txt", FileMode.Open);

BinaryFormatter obj = new
BinaryFormatter();

Product p = (Product)
obj.Deserialize(fs);

Console.WriteLine(p.Name);
Console.WriteLine(p.Quantity);
Console.WriteLine(p.Total);
fs.Close();

  }
}
  • If we don’t want particular variable to be serialized then use [NonSerialized] .
  • IDeserialization CallBack is a method. It should be written in front of class name.
  • If we want to implement it, then  
\\\\\\\\\\\\\\\\\\\\\\useOnDeserialization\\\\\\\\\\\\

Program :- To make use of NonSerialize.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
usingSystem.Runtime.Serialization.Formatters.Binary;

namespace Serialize_Nonserialize

[Serializable]

class Product : IDeserializationCallback
//class Product
{

public string Name;
public double Price;
public int Quantity;


[NonSerialized]

public double Total;
public Product(string n, double p, int q)
{
Price = p;
Quantity = q;
}
public void OnDeserialization(object sender)
{
Total = Price * Quantity;
}
}

class Program
{
static void Main(string[] args)
{

\\\\\\\\\\\// Serialization\\\\\\\\\\\

Product p = new Product("Comp", 1000, 5);
FileStream fs = new
BinaryFormatter obj = new BinaryFormatter();
obj.Serialize(fs, p);
fs.Close();

\\\\\\\\\\\\\//Deserialization\\\\\\\\\\\\\\

FileStream fs = new
BinaryFormatter obj = new BinaryFormatter();
Product p = (Product)obj.Deserialize(fs);
Console.WriteLine(p.Name);
Console.WriteLine(p.Quantity);
Console.WriteLine(p.Total);
fs.Close();
        }
     }
}









No comments:

Post a Comment