csharp Generics Questions & Answers

Question 1: Which of the following can be defined generic?

Question 2:  When will be T will be replaced with the actual type in the following program?

class ValueProcessor<T>
{
    // Implementation 
}

Question 3: What will be the output of the following program?

class Program
{
    static void Main(string[] args)
    {
        DataProcessor<int> vp = new DataProcessor<int>();
        vp.Process("TEST");
        vp.Process<double>(3.5D);
    } 
}

class DataProcessor<T>
{
    public void Process<T>(T value) {
        Console.Write(value.GetType().Name + “ ”);
    }
}
ADVERTISEMENT

Question 4: What will be the output of the following program?

class Program
{
    static void Main(string[] args)
    {
        DataProcessor<int> vp = new DataProcessor<int>();
        vp.Process("TEST");
    }
       
}

class DataProcessor<T>
{
    public void Process(T value) {
        Console.Write(value.GetType().Name);
    }
}

Question 5: What will be the output of the following program?

class Program
{
    static void Main(string[] args)
    {            
        Processor<int> p1 = new DataProcessor<int>();
        p1.Process(100);

        DataProcessor<string> p2 = new DataProcessor<string>();
        p2.Process("TEST");
    }

}

class Processor<T>
{
    public void Process(T value)
    {
        Console.Write(value.GetType().Name + “ “);
    }
}

class DataProcessor<U> : Processor<U>
{

}