My Report

C# Generic Test – 2


Correct Answer: 2 points | Wrong: -1 point
Grades: A* (100% score) | A (80%-99%) | B (60%-80%) | C (40%-60%) | D (0%-40%)
advertisement

1. Which of the given statements are valid about generics in .NET Framework?

2. Which of the following statements are valid in the following C# code snippet?

public class Generic<T>
{
    public T Field;
    public void testSub()
    {
        T i = Field + 1;
    }
}
class Program
{
    static void Main(string[] args)
    {
        Generic<int>g = new Generic<int>();
        g.testSub();
    }
}

3. In the following C# code, which statements are perfectly valid?

public class Csharp
{
    public void subject<S>(S arg)
    {
        Console.WriteLine(arg);
    }
}
class Program
{
    static Void Main(string[] args)
    {
        Csharp c = new Csharp();
        c.subject("hi");
        c.subject(20);
    }
}

4. Which of the following is a valid statement about generic procedures in C#.NET are?

5. Which among the given classes represents System.Collections.Generic namespace?

6. In the following C# code, which of the following statements are perfectly valid?

public class MyContainer<T> where T: class, IComparable
{
  /* insert code here */
} 

7. What will be the output of the following C# code?

 public class Generic<T>
 {
     public T Field;
 }
 class Program
 {
     static void Main(string[] args)
     {
         Generic<int> g2 = new Generic<int>();
         Generic<int> g3 = new Generic<int>();
         g2.Field = 8;
         g3.Field = 4;
         if (g2.Field % g3.Field == 0)
         {
             Console.WriteLine("A");
         }
         else
         Console.WriteLine("Prints nothing:");
         Console.ReadLine();
     }
 }

8. What will be the output of the following C# code snippet?

public class Generic<T>
{
    Stack<T> stk = new Stack<T>();
    public void push(T obj)
    {
        stk.Push(obj);
    }
    public T pop()
    {
        T obj = stk.Pop();
        return obj;
    }
}
class Program
{
    static void Main(string[] args)
    {
        Generic<string> g = new Generic<string>();
        g.push(30);
        Console.WriteLine(g.pop());
        Console.ReadLine();
    }
}

9. What will be the output of the following C# code snippet?

public class Generic<T>
{
    Stack<T> stk = new Stack<T>();
    public void push(T obj)
    {
        stk.Push(obj);
    }
    public T pop()
    {
        T obj = stk.Pop();
        return obj;
    }
}
class Program
{
    static void Main(string[] args)
    {
        Generic<int> g = new Generic<int>();
        g.push("Csharp");
        Console.WriteLine(g.pop());
        Console.ReadLine();
    }
}

10. What will be the output of the following C# code snippet?

public class Generic<T>
{
    Stack<T> stk = new Stack<T>();
    public void push(T obj)
    {
        stk.Push(obj);
    }
    public T pop()
    {
        T obj = stk.Pop();
        return obj;
    }
}
class Program
{
    static void Main(string[] args)
    {
        Generic<string> g = new Generic<string>();
        g.push("C++");
        Console.WriteLine(g.pop() + " ");
        Generic<int> g1 = new Generic<int>();
        g1.push(20);
        Console.WriteLine(g1.pop());
        Console.ReadLine();
    }
}

11. Which statement is valid for the following C# code snippet?

public class Generic<T>
{
    public T Field;
}
class Program
{
    static void Main(string[] args)
    {
        Generic<String> g = new Generic<String>();
        g.Field = "Hi";
        Console.WriteLine(g.Field);
    }
}

12. In the following C# code, which of the following statements are perfectly valid?

public class MyContainer<T> where T: IComparable
{
  /* insert code here */
} 

 

Start practicing “1000 MCQs on C#”, and once you are ready, you can take tests on all topics by attempting our “C# Test Series”.

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.