Collection in .Net - placement questions
Q.1 Trace the output of given below code.
ArrayList obj = new ArrayList();
obj.Add("A");
Console.WriteLine("Capacity = "+obj.Capacity);
A) Capacity=1
B) Capacity=2
C) Capacity=3
D) Capacity=4
View Answer / Hide AnswerANSWER: D
Explanation:
ArrayList expands at run time. When you add first item in the ArrayList its capacity automatically increased to 4. When you add fifth element in the ArrayList its capacity will be 8 and so on.
Q.2 Choose the correct option about ArrayList.
Statement 1: In ArrayList you can add any type of object.
Statement 2: In ArrayList you can add only string type of object.
A) Only statement 1 is correct.
B) Only statement 2 is correct.
C) Both statements 1 are correct.
D) None of the above.
View Answer / Hide AnswerANSWER: A
Explanation:
In ArrayList you can add any type of object by using its Add method. This method has one input parameter and its type is Object. All input types are converted into Object in ArrayList.
Example:
ArrayList obj = new ArrayList();
obj.Add("A");
obj.Add(1);
obj.Add(DateTime.Today.Date);
foreach (object list in obj)
{
Console.WriteLine(list);
}
Q.3 If you want to add an array in ArrayList then which method of ArrayList will be used?
A) Add
B) AddRange
C) AddArray
D) None of the above.
View Answer / Hide AnswerANSWER: B
Example:
ArrayList list = new ArrayList();
string[] days ={ "Sun", "Mon", "Tue", "Wed", "Thru", "Fri", "Sat" };
list.AddRange(days);
foreach (object ob in list)
{
Console.Write(ob + " ");
}
Q.4 Trace the output of given below code.
int[] arr = {1,2,3,4,5 };
ArrayList obj = new ArrayList();
obj.Add("A");
obj.Add("B");
obj.AddRange(arr);
foreach (object list in obj)
{
Console.Write(list+" ");
}
A) A B 1 2 3 4 5
B) Compile time error
C) Run time error
D) None of the above
View Answer / Hide AnswerQ.5 Which of the following ArrayList methods can be used to determine whether an item exists in the collection?
A) Count
B) Contains
C) Remove
D) None of the above.
View Answer / Hide AnswerQ.6 Trace the output of given below code.
Hashtable ht = new Hashtable();
ht["EmpId"] = "1";
ht["EmpName"] = "XYZ";
foreach (object ob in ht)
{
Console.WriteLine(ob);
}
A) EmpId=1
EmpName=XYZ
B) Compile Time Error
C) 1, XYZ
D) None of the above.
View Answer / Hide AnswerANSWER: D
Explanation:
When you run the above code you will see the output on console like this:
System.Collections.DictionaryEntry
The result is not like what you expected because HashTable stores the data in key value pair. It is a dictionary object. If you want the output as key and value separately then use
DictionaryEntry.
foreach (DictionaryEntry ob in ht)
{
Console.WriteLine(ob.Key+" : "+ob.Value);
}
Q.7 What does the Dequeue method of the Queue class do?
A) Retrieves an item from the front of the collection
B) Removes the first item from the collection
C) Option A and B are correct.
D) None of the above.
View Answer / Hide AnswerANSWER: C
Explanation:
Retrieves an item from the front of the queue and it also removing it at the same time
Q.8 In what order does a Stack retrieve items as you use its Pop method?
A) Random order
B) First-in, first-out
C) Last-in, first-out
D) Last-in, last-out
View Answer / Hide AnswerQ.9 Trace the output of the following code
Hashtable ht = new Hashtable();
ht["EmpId"] = "1";
ht["EmpId"] = "Pune";
foreach (DictionaryEntry ob in ht)
{
Console.WriteLine(ob.Key+" = "+ob.Value);
}
Console.WriteLine("Count ="+ht.Count);
A) EmpId = Pune
Count =1
B) EmpId = Pune
EmpId = Pune
Count =2
C) EmpId = Pune
Count =2
D) None of the above.
View Answer / Hide AnswerANSWER: A
Explanation:
If you try to store the same key twice, the second call replaces the first call. If you want different values then keys must be unique, it should not duplicate.
Q.10 ListDictionary, HybridDictionary, and OrderedDictionary class is available in which Namespace?
A) System.Collections
B) System.Collections.Specialized
C) System
D) All of the above.
View Answer / Hide AnswerQ.11 Which class is efficient for small collections of items(Less than 10 items)?
A) SmallList
B) HashTable
C) ListDictionary
D) None of the above.
View Answer / Hide AnswerANSWER: C
Explanation:
The Hashtable class is a very efficient collection but if the number of items is less then HashTable gives performance issue. HashTable is very efficient for large collection. ListDictionary is very efficient for small collections of items.
Q.12 if you do not know how large your collection is, then which class you will use to store the data?
A) ListDictionary
B) HybridDictionary
C) HashTable
D) None of the above.
Ans. B
View Answer / Hide AnswerANSWER: B
Explanation:
If you do not know how large your collection is, then HybridDictionary class should be used. Firstly it is implemented as a ListDictionary and only when the list becomes too large it converts automatically into a Hashtable internally. The HybridDictionary is best used in situations where some lists are small and others are very large.
Q.13 you want the functionality of the Hashtable but you need to control the order of the elements in the collection. In this situation which collection class you will use?
A) HybridDictionary
B) ArrayList
C) OrderedDictionary
D) HashTable
View Answer / Hide AnswerQ.14 When adding a key to a Hashtable, what methods can be called on the key to determine whether the key is unique?
A) GetType
B) GetHashCode
C) Equals
D) Option B and C are correct.
View Answer / Hide AnswerQ.15 Which collection is same as ArryList but can store only string type value?
A) StringDictionary.
B) StringCollection
C) StringArray.
D) None of the above.
View Answer / Hide AnswerQ.16 What types of objects can stored as a Value in StringDictionary?
A) Strings
B) Objects
C) Arrays of strings
D) All of the above.
View Answer / Hide AnswerQ.17 Where can you add items to a LinkedList?
A) At the beginning of the LinkedList
B) Before any specific node
C) After any specific node
D) At the end of the LinkedList
E) All of the above.
View Answer / Hide AnswerQ.18 Read the given below code. What you will write at the place of question marks in line
foreach (??????<int, String> item in obj) ?
Dictionary<int, String> obj = new Dictionary<int, String>();
obj[1] = "CareerRide.com";
obj[2] = "XYZ.com";
obj[3] = "PQR.com";
foreach (??????<int, String> item in obj)
{
int id = item.Key;
string website = item.Value;
Console.WriteLine(" {0} = {1}", id, website);
}
A. Value
B. KeyValuePair
C. Key
D. None of the above
View Answer / Hide Answer