1.2 8-Collections and Generics Interview Questions PDF
1.2 8-Collections and Generics Interview Questions PDF
1
C# Language-Windows programming Collections and Generics
2
C# Language-Windows programming Collections and Generics
3
C# Language-Windows programming Collections and Generics
8. Which namespace has to be added for using generic list (List<T>, where T is datatype)in C# ?
a) System.Generic.Collections
b) System.Collections.Generic
c) System.Collections
d) All the above
Ans: b
Explanation: The System.Collections.Generic namespace contains interfaces and classes that define generic
collections, which allow users to create strongly typed collections that provide better type safety and
performance than non-generic strongly typed collections.
9. How to Serialize the generic types ?
a) Use GenericSerialize attribute
b) Use Serialize attribute
c) Use Obsolete attribute
d) None of the above
Ans: b
Explanation: By using the attribute: [Serializable]
[Serializable] public class TestClass { }
10. You are creating an undo buffer that stores data modifications. You need to ensure that the undo functionality
undoes the most recent data modifications first. You also need to ensure that the undo buffer permits storage
of strings only. Which code segment should you use?
a) Stack<string> undoBuffer = new Stack<string>();
b) Stack undoBuffer = new Stack();
c) Queue<string> undoBuffer = new Queue<string>();
d) Queue undoBuffer = new Queue();
Ans: a
Explanation: This is a Last-In-First-Out requirement that requires a strongly typed collection. Each of the
members in the collection will be of string type.
11. When you create an instance of a nested type, you must specify __________ for all enclosing generic types.
Ans: Type arguments
Explanation: A type that is nested in a generic type can depend on the type parameters of the enclosing generic
type. The common language runtime considers nested types to be generic, even if they do not have generic type
parameters of their own. When you create an instance of a nested type, you must specify type arguments for all
enclosing generic types.
12. What is the drawback of arraylist compared with Generic list?
4
C# Language-Windows programming Collections and Generics
Ans: In an Arraylist, as Each item is stored as an object, during Sorting and traversing the list, it must be typecasted
at runtime, and then compared for sorting/searching where as Generic list has all items typecasted at compile
time, which saves a lot of workload during sorting and searching.
13. We can increase the capacity of array list
a) We can decrease the capacity of array list
b) This is used to add another array to the arraylist
c) We can increase the capacity of array list
d) None of the above
Ans: a
Explanation: Using TrimToSize we can decrease the capacity of array List i.e., we can set capacity to the actual
number of elements in the array List.
14. Default capacity of an array list is ___
a) 0
b) 1
c) 2
d) 4
Ans: a
Explanation: Default capacity is 0. When the 1st element is added, the capacity will be 4 . Again when the 5th
element is added the capacity will be 8 and so on.
15. Can we sort the items in a hash table?
a) Yes
b) No
Ans: b
Explanation: We can not set items in hash table but, implementing the IComparable interface , which is
having the CompareTo method we can do. Sort method of hashtable invokes the CompareTo internally and
the items will be sorted.
16. What is the main difference between accessing the members of an arraylist and a hastable?
I. In arraylist retrieval of data is done on Index
II. in arraylist the retrieval of data is based on <key>
III. in Hash table the retrieval of data is based on <key>
IV. In Hash table retrieval of data is done on Index
a) I and III
b) II and IV
c) II and III
d) III and IV
5
C# Language-Windows programming Collections and Generics
Ans: a
Explanation: ArrayList implements the Icollection and Hashtable implemets the Idictionary . Both are used to
store objects. Mmostly iIn AarraylList elements are accessed using index where as in Hashtable elements are
accessed using keys
17. Can an arraylist accept duplicate elements?
a) Yes
b) No
Ans: a
Explanation: Number of elements it can hold is the capacity of the arraylist . As the elements are added to the
arraylist the capacity is automatically increased . It accepts the null reference as avalid value and also accepts
the duplicate elements
18. In a Hashtable _______ must be unique, but ______ need not be unique.
Ans: Key, Value
19. We can override the ToString() method
a) Yes
b) No
Ans: b
Explanation: The ToString() method is by default a Virtual method in the Object class and we can override it
and return any other value according to our requirement
20. IDictionary interface iss a ________ type of collection of key/value pairs.
a) Generic
b) Nongeneric
c) Both A and B
d) None of the above
Ans: c
Explanation: There are two versions of this interface. Generic(System.Collections.Generic) and non-
Generic(System.Collections).
21. The following are the IDictionary methods:
a) Add
b) Clear
c) GetEnumerator
d) All the above
Ans: d
Explanation: Add - Adds an element with the provided key and value to the IDictionary object.
Clear - Removes all elements from the IDictionary object.
6
C# Language-Windows programming Collections and Generics
GetEnumerator - Overloaded.
22. Is dynamic "insertion" and "deletion" possible using Array?
a) Yes
b) No
Ans: b
Explanation: Array does not allow insertion and deletion of items which is possible using ArrayList which can
dynamically grow in size and every item in an Arraylist is identified by <index>.
23. Can we disable connection pooling or is it a built in property like that can only be used?
a) Yes
b) No
Ans: a
Explanation: But if we explicitly want to enable or disable connection pooling we should set Pooling=false in
your connection string for disabling connection and enable by setting set Pooling=true.
By default it is always enabled.
24. Every Collection is implemented from the Interface _______________
Ans: IEnumerable
25. How is IEnumerable and IEnumerator related?
a) IEnumerable does not have any relation with IEnumerator.
b) IEnumerator has a GetEnumerator method that returns an IEnumerable
c) IEnumerable has a GetEnumerator method that returns an IEnumerator
d) None of the above
Ans: c
26. What would be major difference between different types of collections?
Ans: Every collection is different from other collections in the way the items are retrieved back from the collection.
Based on how we want to retrieve an item we have to select an appropriate collection class. Some of the
collections differ in the way a collection is stored. Ex: Dictionary, List, etc.
27. The __________ namespace contains interfaces and classes that defines various collections of objects such as
lists, queues, etc
Ans: System.Collections
Explanation: Collection classes are defined as part of the System.Collections or system.collections.Generic
namespace.
These classes provide support for stacks, queues, lists, and hash tables.
28. A ___________ is an object which would manage other objects i.e array list, hash table, etc.
Ans: Collection
7
C# Language-Windows programming Collections and Generics
Explanation: The .NET provides specialized classes called Collections for purpose of data storage and retrieval.
These classes provide support for stacks, queues, lists, and hash tables.
29. ___________ implements the IList interface
Ans: Arraylist
Explanation: ArrayList implements the Ilist Interface whose size is increased as reequired . Ilist represents a non
generic collection of the objects that can be accessed by the Index.
30. Elements in arraylist can be accessed using an_____________
a) Integer index
b) Character index
c) Stack index
d) Decimal index
Ans: a
Explanation: ArrayList implements the Ilist Interface whose size is increased as reequired . Ilist represents a
non generic collection of the objects that can be accessed by the Index.
31. __________ represents a collection of key/ value pairs.
Ans: Hashtable
Explanation: Hashtable represents the collection of the key value pair that are organized based on the hash code
of the key.
32. A Generic class is ______________ to any particular data type
a) Specific
b) not specific
c) depends on whether the class is public or private
d) can't say
Ans: b
Explanation: A Generic Class is not specific to any particular Data Type. But the instance of such a class would
be specific to a given Data Type mentioned at the time of creating the instance/object that class.
33. Generic types enforce type compliance at ____________
Ans: compile time
34. What is the location of generic namespace API?
Ans: The System.Collections.Generic namespace contains interfaces and classes that define generic collections,
which allow users to create strongly typed collections that provide better type safety and performance than non-
generic strongly typed collections.
35. What is the use of constraints in Generics?
Ans: If we want to examine an item in a generic list whether it is a valid or not , or to compare it with some other
item . The compiler must have the guarantee that the operator or method it has to call will be supported by any
8
C# Language-Windows programming Collections and Generics
type argument that might be specified by client code. This guarantee is obtained by applying one or more
constraints to your generic class definition.
36. What are the types of constraints in Generics?
a) Derivation constraint
b) Constructor constraint
c) Reference/Value type constraint
d) All the above
Ans: d
37. What is a derivation constraint in Generic?
a) It is used with a “where” reserved keyword with a colon and particular interface
b) It is used with a “where” reserved keyword with a colon and new() default constructor
c) It is used with a “where” reserved keyword with a colon and a value type
d) None of the above
Ans: a