generics c programming 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);
}
}

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.
E.     None of the above.
Answer & Explanation

Answer: Option A

Explanation:

No answer description available for this question. Let us discuss.
View Answer Workspace Report Discuss in Forum
7.

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
E.     None of the above
Answer & Explanation

Answer: Option C

Explanation:

No answer description available for this question. Let us discuss.
View Answer Workspace Report Discuss in Forum
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
}

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
E.     None of the above
Answer & Explanation

Answer: Option B

Explanation:

No answer description available for this question. Let us discuss.
View Answer Workspace Report Discuss in Forum
9.

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.
E.     None of the above.
Answer & Explanation

Answer: Option C

Explanation:

No answer description available for this question.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>