Exercise: Generics
Questions for: Generics
Which of the following statements is valid about advantages of generics?
A:
Generics shift the burden of type safety to the programmer rather than compiler.
B:
Generics require use of explicit type casting.
C:
Generics provide type safety without the overhead of multiple implementations.
D:
Generics eliminate the possibility of run-time errors.
Answer: C
No answer description is available. Let's discuss.
For the code snippet given below, which of the following statements are valid?
public class MyContainer<T> where T: class, IComparable
{
//Insert code here
}
- Class MyContainer requires that it's type argument must implement IComparable interface.
- Compiler will report an error for this block of code.
- There are multiple constraints on type argument to MyContainer class.
- Class MyContainer requires that its type argument must be a reference type and it must implement IComparable interface.
A:
1 and 2 Only
B:
3 and 4 Only
C:
2 and 3 Only
D:
All of the above
Answer: B
No answer description is available. Let's discuss.
Discuss About this Question.
For the code snippet given below, which of the following statements are valid?
public class MyContainer<T> where T: IComparabte
{
// Insert code here
}
- Class MyContainer requires that it's type argument must implement IComparabte interface.
- Type argument of class MyContainer must be IComparabte.
- Compiler will report an error for this block of code.
- This requirement on type argument is called as constraint.
A:
1 and 2 Only
B:
1, 2 and 3 Only
C:
1 and 4 Only
D:
All of the above
Answer: C
No answer description is available. Let's discuss.
Discuss About this Question.
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);
}
}
A:
It will print string "Hello" on the console.
B:
Name Generic cannot be used as a class name because it's a keyword.
C:
Compiler will give an error.
D:
Member Field of class Generic is not accessible directly.
Answer: A
No answer description is available. Let's discuss.
Discuss About this Question.
For the code snippet shown below, which of the following statements are valid?
public class TestExamAdept
{
public void TestSub<M> (M arg)
{
Console.Write(arg);
}
}
class MyProgram
{
static void Main(string[] args)
{
TestExamAdept bix = new TestExamAdept();
bix.TestSub("ExamAdept ");
bix.TestSub(4.2f);
}
}
A:
Program will compile and on execution will print: ExamAdept 4.2
B:
A non generic class Hello cannot have generic subroutine.
C:
Compiler will generate an error.
D:
Program will generate a run-time exception.
Answer: A
No answer description is available. Let's discuss.
Discuss About this Question.
Ad Slot (Above Pagination)
Discuss About this Question.