Saturday, 15 March 2014

Interview Questions And Answers Of Arrays and Collections in C#

1.   What is Jagged Array?

Answer:- Jagged array has elements of type array is called jagged array. The elements can be of different dimensions and sizes. jagged array also called as Array of arrays.

2.Briefly describe the two kinds of multidimensional arrays in .NET.

There are two types of multidimensional arrays:

Rectangular arrays

These multidimensional arrays have all the sub-arrays with a particular dimension of the same length. You need to use a single set of square brackets for rectangular arrays.

Jagged arrays

These multidimensional arrays have each sub-array as independent arrays of different lengths. With these you need to use a separate set of square brackets for each dimension of the array.

3 .What is BitArray?

Answer: -The BitArray collection is a composite of bit values. It stores 1 or 0 where 1 is true and 0 is false. This collection provides an efficient means of storing and retrieving bit values.


4.What’s the difference between the System.Array.CopyTo() and System. Array. Clone()?

Answer: -Using Clone() method, we creates a new array object containing all the elements in the original array and using CopyTo() method, all the elements of existing array copies into another existing array. Both the methods perform a shallow copy.

5.How we can create an array with non-default values?

Answer: -We can create an array with non-default values using Enumerable. Repeat.

6.Differences Between Hashtable and Dictionary?

Answer: - Hashtable is threadsafe and while Dictionary is not.
·  Dictionary is type safe means that the values need not to boxing while Hashtable values need to be boxed or unboxed because it stored the values and keys as objects.

·      When you try to get the value of key which does not exists in the collection, the dictionary throws an exception of 'KeyNotFoundException' while hashtable returns null value.

·      When using large collection of key value pairs hashtable would be considered more efficient than dictionary.

·       When we retrieve the record in collection the hashtable does not maintain the order of entries while dictionary maintains the order of entries by which entries were added.

·         Dictionary relies on chaining whereas Hashtable relies on rehashing.

7.What is difference between ArrayList and vector?

Answer: -Synchronization - ArrayList is not thread-safe whereas Vector is thread-safe. In Vector class each method like add(), get(inti) is surrounded with a synchronized block and thus making Vector class thread-safe.

n    A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent.

8.What is an Enumerator?

Answer: -An Enumerator is an object that iterates in a forward direction readonly for a set of Items.

9.What is hybrid dictionary?

Answer: -A HybridDictionary will self-monitor itself. Initially, and while it remains small the HybridDictionary will internally keep itself as a ListDictionary. Once it exceeds a certain threshold, it will then convert its data into a HashTable. The nice part is you don’t have to care when, or what state it is currently in. All you have to know is the HybridDictionary is managing the memory most efficiently on your behalf.

10.How many types of dictionaries and for what?

Answer: - ListDictionary - Used for small collections, typically less than 10 items.

HybridDictionary - Used when the collection size is unknown (switches implementations depending on the size of the collection)

OrderedDictionary - The elements of an OrderedDictionary are not sorted by the key, unlike the elements of a SortedDictionary<TKey, TValue> class. You can access elements either by the key or by the index.

11.Can ArrayList be made of fixed size?

Answer: -yes,ArrayList. FixedSize() method.


12.Can ArrayList be made of Readonly?

Answer: -Yes,by ArrayList.ReadOnly().Overloaded method,2 overLoads.

13.In how many ways we Iterate in collections?

Answer: -foreach loop, and enumerators.

14.For an Enumerator Collection needs to be inherited from which Interface.

Answer: -A C# enumerator (not to be confused with a C# enum) is the most abstract form of an iterator. Any C# Collection that inherits the ICollection interface (which includes most of the collections) have an enumerator.


15.What is the specialty of system.Collections.Concurrent namespace?
or
Can we have a multi threaded dictionary?
or
Can we have a dictionary with read/write property?

Answer: -ConcurrentDictionary class present.
 Using Concurrent Dictionary class we can make multithreaded dictionary.

16.In a HashTable which one can be null key or value?

Answer: -key cannot be null but value can be.

17.What we have to do to access the members of ArrayList from Inner Class?

Answer: -To access members of ArrayList from the inner class, it is necessary to pass ArrayList class's reference to it.

18. Do we get values in a sequential order when we retrieve it from Hash Table?

Answer: -no, values are not retrieved in a sequential order because they are stored according to hash key generated by a hash function.

19.What is the use of GetEnumerator?

Answer: -This is a method that returns an object that implements the IEnumeratorinterface .

20.Which is faster for loop or foreach loop?

Answer: - For iterating an array of 1000+ strings - for is better than foreach.

For iterating over IList (non generic) strings - foreach is better than for.

It was potentially faster to iterate through a collection with a For loop rather than a For Each loop.

For each is slower for a number of reasons. One is it is using the IEnumerable interface, which requires some casting (assuming you aren't using a generic collection). My test above seemed to go along with that as well. A simple for/while/do loop is pretty much as simple as it gets.

21.How can you sort the elements of the array in descending order?

Array.Sort();

Array.reverse();

22.what is Difference between object.Equals() and object.ReferenceEquals()?

Answer: object.Equals(object objA, object objB)

This method does some testing for null on objA and objB and calls objA.Equals(objB). It returns true if objA and objB are null references or both are the same instance, or if objA.Equals(objB) returns true, otherwise it returns false.

object.ReferenceEquals(object objA, object objB)

This method returns true if objA is the same instance as objB or both have null reference, otherwise return false.

23.how to delete a particular element in collection?

Answer:

RemoveAt(int index)

It removes an item at the specified index. It throws ArgumentOutOfRangeException exception for invalid index in list and throws NotSupportedException exception for read only and fixed size Ilist.

24.what is Createinstance() Method?

Answer: Initializes a new instance of the Array class. There are 6 overloads available for this method.

25.what is MemberwiseClone() Method?

Answer: Creates a shallow copy of the current Object.

26.Can Enumarator method to use for writing Element into the collections?

Answer: No,it is only retrieving the data from the collection.

27.what is GetType() method?

Answer: It returns the type of Object of current instance.

28.what is GetHashcode() method?

Answer: it returns the hashcode of current object.

29.What are  collection interfaces?

Answer:  Ienumerable interface,Ilist interface,IDictionary interface,ICollection interface etc..

30.what are the classes avaiable in the system.collection namespace?

Answer: arraylist,array,CollectionBase,comparer,,Hashtable,Queue,ReadOnlyCollectionBase,SortedList ,Stack


No comments:

Post a Comment