C# Programming - Generics

Exercise : Generics - General Questions
  • Generics - General Questions
6.
For the code snippet given below, which of the following statements is valid?
public class Generic<T>
{
    public T Field;
}
class Program
{
    static void Main(string[ ] args)
    {
        Generic<String> g = new Generic<String>();
        g.Field = "Hello";
        Console.WriteLine(g.Field);
    }
}
It will print string "Hello" on the console.
Name Generic cannot be used as a class name because it's a keyword.
Compiler will give an error.
Member Field of class Generic is not accessible directly.
None of the above.
Answer: Option
Explanation:
No answer description is available. Let's discuss.

7.
For the code snippet given below, which of the following statements are valid?
public class MyContainer<T> where T: IComparabte
{
    // Insert code here
}
  1. Class MyContainer requires that it's type argument must implement IComparabte interface.
  2. Type argument of class MyContainer must be IComparabte.
  3. Compiler will report an error for this block of code.
  4. This requirement on type argument is called as constraint.
1 and 2 Only
1, 2 and 3 Only
1 and 4 Only
All of the above
None of the above
Answer: Option
Explanation:
No answer description is available. Let's discuss.

8.
For the code snippet given below, which of the following statements are valid?
public class MyContainer<T> where T: class, IComparable
{
    //Insert code here
}
  1. Class MyContainer requires that it's type argument must implement IComparable interface.
  2. Compiler will report an error for this block of code.
  3. There are multiple constraints on type argument to MyContainer class.
  4. Class MyContainer requires that its type argument must be a reference type and it must implement IComparable interface.
1 and 2 Only
3 and 4 Only
2 and 3 Only
All of the above
None of the above
Answer: Option
Explanation:
No answer description is available. Let's discuss.

9.
Which of the following statements is valid about advantages of generics?
Generics shift the burden of type safety to the programmer rather than compiler.
Generics require use of explicit type casting.
Generics provide type safety without the overhead of multiple implementations.
Generics eliminate the possibility of run-time errors.
None of the above.
Answer: Option
Explanation:
No answer description is available. Let's discuss.