Sample questions
Question 1Medium
Choose the statements which are valid for given 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();
}
}
- code run successfully print nothing
- code run successfully print 1
- Program will give run time error
- Compile time error
Answer: D. Compile time error
Explanation
compiler will give error as operator + is not defined for types T and int
Step-by-step solution
- Field is of type T, which is int in Main.
- T i = Field + 1; tries to add int and 1 using + operator.
- + operator is not defined for type T and int, so compiler throws error.
- Code fails at compile time due to missing operator definition.
- Final answer: Compile time error.
Why the other options are wrong
- A. Code does not print anything because it has a compile time error.
- B. Code does not print 1 because it fails to compile.
Tip: Always define operators or cast types explicitly when using generics with arithmetic operations.
Question 2Medium
For the code snippet shown below, which of the following statements are valid?
public class Generic<T>
{
public T Field;
public void TestSub()
{
T i = Field + 1;
}
}
Class MyProgram
{
static void Main(string[] args)
{
Generic<int> gen = new Generic<int>();
gen.TestSub();
}
}
- Addition will produce result 1.
- Result of addition is system-dependent.
- Program will generate run-time exception.
- Compiler will report an error: Operator '+' is not defined for types T and int.
Answer: D. Compiler will report an error: Operator '+' is not defined for types T and int.
Explanation
: It will show a compiler error because the statement
"Field + 1" is incorrect.
Operand + is applied to 2 different datatypes, int and T.
Operand + is defined only for same datatypes.
Step-by-step solution
- T is an unconstrained generic type, so the compiler does not know that T supports the + operator.
- Field + 1 adds a T and an int, and + is not defined for T and int.
- So the compiler reports an error, and the correct answer 3 is correct.
Why the other options are wrong
- A. The code does not compile, so no addition result is produced.
- B. No addition runs, so there is no system-dependent result.
- C. The error is found at compile time, so no run-time exception occurs.
Tip: An unconstrained generic type T supports no operators; add a constraint before using them.
Question 3Medium
What would be the output of the following code snippet?
using System;
using System.Collections;
class Program
{
static void Main()
{
ArrayList list = new ArrayList();
list.Add("string");
list.Add(2);
Console.WriteLine(list.Count);
list.Clear();
Console.WriteLine(list.Count);
Console.ReadLine();
}
}
- 2 2
- 2 0
- 2 1
- 0 0
Answer: B. 2 0
Explanation
ArrayList.Count gives the number of element in an ArrayList, And ArrayList.Clear removes all elements from the ArrayList.
So in the above program, first we added two objects(string and 2) in ArrayList ,Then we called the Count,Which will return 2 ,Then we called the Clear(), Which will remove all element from ArrayList.
and then Count called again, returning 0.
Step-by-step solution
- Two items are added to the ArrayList: 'string' and 2.
- Count is 2, so the first WriteLine prints 2.
- Clear() removes all items from the list.
- Count is now 0, so the second line prints 0. Output is 2 then 0: option B.
Why the other options are wrong
- A. The second Count is 0 after Clear(), not 2.
- C. Clear() removes all items, not just one, so the count is 0, not 1.
- D. The first Count is 2 before Clear() runs, not 0.
Tip: ArrayList.Count is the number of items right now; after Clear() it is 0.
Question 4Medium
What would be the output of the following code snippet?
Stack stack = new Stack();
stack.Push("string");
stack.Push(2);
stack.Push("c");
foreach (object o in stack)
{
Console.WriteLine(o);
}
- string 2 c
- c 2 string
- Error
- None of these
Answer: B. c 2 string
Explanation
A Stack is LIFO (last in, first out), so foreach shows the last pushed item first: c, then 2, then string. Option B gives exactly this output.
Step-by-step solution
- A Stack is LIFO: the item pushed last comes out first.
- Pushes happen in order: 'string', then 2, then 'c'.
- foreach over a Stack starts at the top: c, 2, string.
- Each value prints on its own line, so the order is c, 2, string: option B (its words are capitalised).
Why the other options are wrong
- A. String, 2, C is the insertion order, as in an ArrayList or queue, not Stack order.
- C. foreach works on a Stack, so there is no error.
- D. Option B matches the output, so 'none of these' is wrong.
Tip: A Stack gives items in reverse push order (last in, first out).
Question 5Medium
Which of the following are in System.Collections namespace?
I. ArrayList
II. Hash Table
III. List
IV. Dictionary
- I and III
- I and II
- I and IV
- III and IV
Answer: B. I and II
Explanation
ArrayList and Hashtable are in System.Collections, so I and II are correct. List<T> and Dictionary<TKey, TValue> are generic types and are in System.Collections.Generic.
Step-by-step solution
- ArrayList is defined in System.Collections namespace.
- HashTable is also defined in System.Collections namespace.
- List and Dictionary belong to System.Collections.Generic namespace.
- Thus the classes in System.Collections are I and II.
Why the other options are wrong
- A. List is in System.Collections.Generic, not System.Collections, so III cannot be in the answer.
- C. Dictionary is in System.Collections.Generic, not System.Collections, so IV is not included.
- D. Both List and Dictionary are generic-namespace types, so III and IV are both outside System.Collections.
Tip: Non‑generic collections use System.Collections; generic ones use System.Collections.Generic.
Question 6Medium
Which of the following statements are valid about generics in .NET Framework?
I. Generics is a language feature.
II. We can create a generic class, however, we cannot create a generic interface in C#.NET.
III. Generics delegates are not allowed in C#.NET.
IV. Generics are useful in collection classes in .NET framework.
V. None of the above
- 1 and 2 Only
- 1, 2 and 3 Only
- 1 and 4 Only
- All of the above
Answer: C. 1 and 4 Only
Explanation
A unified type system and simplifying the way that value and reference types are used by the language. By creating typed collections with generics, you can avoid the need to create specific versions of each class while retaining the performance benefits of doing so.
For example, your program can create one parameterized Stack class and avoid having to create an IntegerStack to store integers, a StringStack to store strings, or a CustomerStack to store Customer types.
Step-by-step solution
- I is true: generics are a language feature of C# and .NET.
- II is false: generic interfaces can be created, such as IEnumerable<T>.
- III is false: generic delegates can be created, such as Action<T> and Func<T, TResult>.
- IV is true: generics are useful in collection classes. So the correct set is I and IV: option C, 1 and 4 only.
Why the other options are wrong
- A. Includes II, but generic interfaces can be created, so it is wrong.
- B. Includes II and III, and both are false.
- D. All of the above includes II and III, and both are false.
Tip: C# allows generic classes, interfaces and delegates; generics help collections stay type safe without casting.
Question 7Medium
Which statement is correct about HashTable class?
- HashTable optimizes lookups. It computer a hash of each key you add. It then uses this hash code to look up the element very quickly.
- It is slower than the generic Dictionary type.
- Represents a collection of key/value pairs that are organized based on the hash code of the key.
- All the above.
Answer: D. All the above.
Explanation
HashTable is another class in system. Collection each element is a key/value pair stored in a Dictionary Entry object. A key cannot be null, but a value can be, the object used as keys by
HashTable are required to override the object. GetHashCode method and the object.equals method .the implementation of both method and interface must handle case sensitivity the same way;
otherwise ,the HashTable might behave incorrectly. Following are the example of the HashTable
Using System;
Using System.Collection;
Class program
{
Static void Main()
{
HashTable hash Table =new HashTable();
HashTable.Add(key1,value1);
hashTable.Add(key1,value1);
foreach(DictionaryEntry 0 in hash Table)
{
Console.Writeline(0.key.tostring()+0.value.tostring());
}
Console.ReadLine();
}
}
Step-by-step solution
- Option A is true: Hashtable computes a hash code for each key and uses it to find items quickly.
- Option B is also true: the generic Dictionary type is usually faster than Hashtable.
- Option C is true: Hashtable stores key/value pairs organised by the hash code of the key.
- All three statements are true, so the answer is option D, All the above.
Why the other options are wrong
- A. This is true, but it describes only one of the three true statements, so it is not the whole answer.
- B. This is also true, but on its own it does not cover the other true statements; D covers all of them.
- C. This is true too; it is one of the three true points, so this option alone is not complete.
Tip: For All of the above, check each statement; one false statement rules that option out.
Question 8Medium
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.
Answer: C. Generics provide type safety without the overhead of multiple implementations.
Explanation
Generics allow you to define type-safe classes without compromising type safety, performance, or productivity. You implement the server only once as a generic server, while at the same time you can declare and use it with any type. To do that, use the <and> brackets, enclosing a generic type parameter.
Step-by-step solution
- Generics let you write one type-safe class or method that works with any type.
- You do not need a separate copy of the code for each type.
- Option C states this: type safety without multiple implementations.
Why the other options are wrong
- A. Generics move type safety to the compiler, not the programmer.
- B. Generics remove the need for explicit casts; they do not require them.
- D. Generics reduce run-time type errors but cannot eliminate every run-time error.
Tip: Generics give type safety at compile time with one implementation.
Practise all 8 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →