[go: up one dir, main page]

0% found this document useful (0 votes)
27 views9 pages

1.2 8-Collections and Generics Interview Questions PDF

Uploaded by

mosalah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views9 pages

1.2 8-Collections and Generics Interview Questions PDF

Uploaded by

mosalah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

C# Language-Windows programming Collections and Generics

1. Generics in C#.Net is used to_________


a) Solve the problem of type safety
b) Reuse the code
c) Increase the performance
d) All the above
Ans: d
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.
Generic classes and methods combine reusability, type safety and efficiency in a way that their non-generic
counterparts cannot. Generic collection classes are like Templates in C++.
Code Reusability: if we write a Generic Class or method once we can use that throughout our program. hence
it is code reuse
Performance By using Generics performance increases as we are not using the concept of boxing/unboxing or
casting the variables
Generic methods can often be called without employing any special syntax by using a feature called type
inference.
2. The built-in collections found in the System.Collections namespace are________
a) Ordered collections
b) Indexed collections
c) Keyed collections
d) All the above
Ans: d
Explanation: Ordered collections:
Collections that implement only the ICollection interface are usually distinguished by the fact that insertion
order controls the order in which objects may be retrieved from the collection. The System.Collections.Stack
and System.Collections.Queue classes are both examples of ICollection collections.
Indexed collections:
IList-implementing collections are distinguished by the fact that their contents can be retrieved via a zero-
based numeric index, like an array. The System.Collections.ArrayList object is one example of an indexed
collection.
Keyed collections:
Collections implementing the IDictionary interface contain items that can be retrieved by an associated key
value of some kind. The contents of IDictionary collections are also usually sorted in some fashion based on
the key value and can be retrieved in sorted order by enumeration. The System.Collections.HashTable class
implements the IDictionary interface.

1
C# Language-Windows programming Collections and Generics

3. How many types of collections are available in .net?


a) 2
b) 3
c) 4
d) 1
Ans: a
Explanation: 1.Non-Generic Collections in System.Collections namespace : These are not type safe.
ArrayList
SortedList
Stack
Queue
HashTable
BitArray
2.Generic Collections in System.Collections.Generic: these are type safe.
List
Dictionary
Linked List
Stack
Queue
4. How to use Dictionary class in Systems.Collections.Generic?
a) Create a Dictionary object and populate it and enumerate it to display key value pairs.
b) Create collections object and populate them with values and create adictinary object.
c) Both A and B
d) None of the above
Ans: a
Explanation: For example, to create a Dictionary object that has integer as key and string as value, we can
create dictionary object in the following way:
Dictionary<int,string> dictNames =new Dictionary<int,string>();
dictNames.Add(1,”Raj”);
dictNames.Add(3,”Ravi”);
dictNames.Add(5,”Kevin”);
In order to retrieve above value enumerate it i.e use foreach loop and use KeyValuePair object as the
elements are retrieved as KeyValuePair objects
foreach(KeyValuePair<int,string> kvpName in dictNames)
Console.WriteLine(kvpName.Key+” “+kvpName.Value);

2
C# Language-Windows programming Collections and Generics

5. Differences between hashtable and arraylist are:


a) Retrieving the data in arraylist is faster than retrieving the data in hashtable
b) Array list is a collection of objects and hashtable is a collection that takes key and corresponding values.
c) ArrayList Class implements the Icollection Interface where as Hashtable implemets the Idictinary
Interface
d) All the above
Ans: c
Explanation: Both are used to store collection of objects of a similar type. In arraylist elements are accessed
using index wheareas in Hashtable elements are accessed using keys.
6. _____________is the compiler's ability to infer which type arguments to use with a generic method, without
specifying explicitly.
a) Multiple inference
b) Static type inference
c) Generic type inference
d) All the above
Ans: c
Explanation:
Ex:
public class MyClass
{
public void MyInstanceMethod<T>(T t)
{...}
public static void MyStaticMethod<T>(T t)
{...}
}
7. Constraints in generics are used to__________
a) Add contextual information to type parameters
b) To limit the range of types
c) Add information about type arguments
d) All the above
Ans: d
Explanation: If we want to check an item in generic list whether it is a valid or if if it is to be compared it with
the other item, the compiler should have the guarantee that the method or operator that is used to call is
supported by the any types of the arguments which might be specified by the client code. This kind of
guarantee will be provided by applying one or more constraints to the generic class definition.

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

You might also like