1.1 8_Collections_and_Generics_Multiple_Choice_Questions.pdf
1.1 8_Collections_and_Generics_Multiple_Choice_Questions.pdf
1. Closely related data can be handled more efficiently in memory when grouped together into a
___________
a) Collection
b) Database
c) File
d) Class
Ans) a
Collection is an object in memory and is used for managing other objects in memory.
Ans:a,b,e
Explanation: Collections have built in functions so that reducing code to sort the elements,whenever we
added elements to the collection object automatically indexed it gives best performance and easly
iterate through collection.
Ans:b
Explanation
IEnumerable is parent interface for all collection classes. It used to fetch an enumerator which can be
used for iterating through all the items in a collection.
4. Array is a collection?
a) True
b) False
Ans) a
Explanation:
Every array in .net is inherited from System.Array and thus all types of arrays in .net are collections.
5. foreach statement can be used only on those objects which have implemented _________ interface
a) ICollection
b) IEnumerable
c) IEnumerator
d) IList
Ans) b
Explanation:
When foreach is used on an object, it internally calls IEnumerable.GetEnumerator for iterating to all the
items in the collection.
Deccansoft Software Services-MS.NET Collections and Generics
Ans) a, b, e
Explanation:
IList is inherited from ICollection, ICollection is inherited from IEnumerable, thus ArrayList has to
implement ICollection and IEnumerable along with IList.
a. Stack
b. ArrayList
c. Queue
d. Hashtable
Ans:b
Explanation
ArrayList implements the IList interface.
Ans:c
Explanation:
In ArrayList any time of item can be added. This is because the parameter is of type object.
10. Numbers of items which can be added to ArrayList are fixed and decided by its Capacity which cannot be
changed (true/false)
Ans) False
Exp: Capacity doubles as and when required to accomodate more number of items in the collection.
Ans:a,c
Explanation
Arraylist implements the TrimToSize Method it is inherited from List Interface.
12. Suppose value of the Capacity property of ArrayList Collection is set to 4. What will be the capacity of
the Collection on adding fifth element to it?
A. 4
B. 5
C. 8
D. Runtime Exception
Ans) 8
Explanation
Capacity gets doubled.
Ans) a, d, e
Ans:b
Explanation
Deccansoft Software Services-MS.NET Collections and Generics
Arraylist elements organized based on index/value and also accessed based on index.
Ans) b, c
Explanation:
In Hashtable, while adding the item we need to mention both Key and Value
SortedList is special case of Hashtable where the collection is automatically sorted based on Key.
16. What are the 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 of keys and thier
corresponding values.
c. ArrayList class implements the IList interface where as Hashtable implemets
IDictinary interface
d. All the above
Ans:b,c
Explanation
Hashtable represents key/value pairs that are organized based on the hashcode of the key.
Arraylist represents based on index/value elements organized.
Ans:a,b,c
Explanation
IEnumerator collection contains Reset,Current,MoveNext members. In ArrayList if an Item is added using
Insert Method like below. al.Insert(2,"item")
19. Which of the following method can be used to Add an array of items into ArrayList.
a. Add
b. AddArray
c. AddItems
Deccansoft Software Services-MS.NET Collections and Generics
d. AddRange
Ans:d
Explanation
Ans:c
Explanation
ToArray return System.Array and that is parent of string[], hence the casting is required
ToArray parameter is typeof each element in the array.
Ans:a
Explanation
In ArrayList each element has unique index and two or more elements can have same value.
23. Only Objects which have implemented ___________ interface can be sorted using Sort method of an
ArrayList
a. IComparable
b. IComparer
c. ISortable
d. ICollection
Ans:a
Explanation
IComparble interface has CompareTo method it is used for sorting the array elements.
c) Compilation Error
d) Runtime Error
Ans) d
Explanation:
Collections cannot be modified while in foreach statement.
Ans:b
Explanation
Hashtable key should not be null based on key we are adding value.
Ans:d
Explanation:
Hashtable key should not be null based on key we are adding value.
27. In Hashtable which of the following method can be used to insert an item in between at a given index
a. Insert method
b. Add method
c. AddAtIndex
d. In Hashtable, items cannot be inserted at a given index
Ans: d
Explanation:
In Hashtable, items are inserted based on key.
28. Which of the following statements are correct about a HashTable collection?
a) It is a keyed collection.
b) It is a ordered collection.
c) It is an indexed collection.
d) It implements a IDictionaryEnumerator interface in its inner class.
e) The key value pairs present in a HashTable can be accessed using the Keys and Values
properties of the inner class that implements the IDictionaryEnumerator interface.
Ans) a, d, e
c. Remove
d. Insert
Ans:a,c
Explanation:
Insert and RemoveAt are based on index and items in ArrayList are not having index.
30. Which method can be used for sorting items in Hashtable in descending order of key
a. Sort()
b. SortDescending()
c. Sort().Reverse()
d. Items in Hashtable cannot be sorted
Ans:d
Explanation:
No built in function for sorting the elements in hash table.
31. SortedList is similar to the Hashtable class in that they implement the IDictionary interface, but they
maintain their elements in sort order by ________
a. Key
b. Value
c. Index
d. DictionaryEntry
Ans:a
Explanation:
Using Sortedlist collection we will sort the elements in hashtable by using key.
Ans:c
Explanation:
Each element is a key/value pair stored in a DictionaryEntry object.
a. Read
b. NextItem
c. MoveToNext
d. MoveNext
Ans:d
Explanation:
MoveNext method is used to move to next element in collection. It returns true if an element exists at
next position otherwise return false.
Ans) b
Explanation:
ArrayList implements IEnumerator and thus the method GetEnumerator().
Ans) d
ArrayList can Sort only those objects in the collection which have implemented IComparable. This is
because it internally calls IComparable.CompareTo method on each object in the collection.
alEmp.Sort(ec);
a. IComparer
b. IComparable
c. ISortable
d. IEmployee
Ans:a
Explanation:
EmployeeComparer inherited from IComparer interface.It returns EmployeeComparer.
Generics
Ans:a, b, d
Explanation:
38. 26)A Generic class is ______________ to any particular datatype. Instance of such a class would be
___________ to a given datatype.
a. specific, specific
b. not specific, not specific
c. not specific, specific
d. specific, not specifix
Ans:c
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.
Ans:a
Explanation:
The runtime generates another version of the generic type and substitutes a particular data type in the
appropriate locations in MSIL.
Ans) a, d
Ans) d
Ans) a, d
Explanation:
While creating the object the generic type defined is "int", hence T is "int".
Foo method can be called either with byte, short or int as argument because parameter now is of type
int.
43. For the code snippet given below, which of the following statements is valid?
public class Generic<T>
{
public T Field1;
}
class Program
{
static void Main(string[ ] args)
{
Generic<string> g = new Generic<string>();
Deccansoft Software Services-MS.NET Collections and Generics
g.Field1 = "Hello";
Console.WriteLine(g.Field1);
}
}
Ans) a
Ans) c
Explanation:
Compiler will report an error: Operator '+' is not defined for types T and int
Ans:d
Explanation:
When you define a generic class, you can apply restrictions to the kinds of types that client code can use
for type arguments when it instantiates your class.
Deccansoft Software Services-MS.NET Collections and Generics
Ans) a, d
Explanation:
Because of the constraint struct, only value types or structure type are allowed for type argument.
Ans) a
Ans:c
Explanation:
When constraint class is provided, only reference types can be used for type argument
Ans) d
Explanation:
where T : <base class name> The type argument must be or derive from the specified base class.
where T : <interface name> The type argument must be or implement the specified interface.
Ans) a, c
Explanation
a is valid because 10 is inferred to be of type int
b is invalid because null cannot be inferred
c is valid because 10 is int
d is invalid because 10.5 is double and required parameter is float.
GENERIC COLLECTIONS
Ans) a
System.Collections.Generic.List is generic collection equivalent of ArrayList
Ans:b,d
Deccansoft Software Services-MS.NET Collections and Generics
Explanation:
List is generic equivalent as the array list whose size is dynamically increased as required . It uses both
the equality and Ordering comparer. Elements in the list are used by an integer index . It allows
duplicate values.
}
}
In the above program, Elements of what datatype can be added to the list
a) int
b) short
c) long
d) double
e) char
Ans) a,b,e
Explanation:
Type argument required is "int", thus option a is valid
short and char can be implicitly casted to int, thus b and e is valid
Ans) d
Explanation:
Generic List version of ToArray returns the array of same type for which List is instantiated. Hence
casting is not required.
{
throw new NotImplementedException();
}
}
a) Student
b) Object
c) IComparable is not generic interface
d) string
Ans) a
Explanation:
Parameter of CompareTo method is supposed to be of same type as Generic argument mentioned for
IComparable.
56. 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 LastInFirstOut requirement that requires a strongly typed collection. Each of the members in the
collection will be of string type.
Ans:b
Explanation:
Dictionary is generic version of hashtable it is more type safety.
Ans:c
Deccansoft Software Services-MS.NET Collections and Generics
Explanation:
Dictionary object has overloaded parameters those must and should be key as int and value is object
type.
Ans:c
Explanation:
We created list object type as student so that must and should type as student.
Ans:c
Explanation:
We created list object type as Student, so that must and should type as Student.
{
int key = kv.Key;
string value = kv.Value;
Console.WriteLine(key + " " + value);
}
}
}
a) KeyValuePair
b) KeyValuePair<int, string>
c) DictionaryEntry
d) string
Ans:c
Explanation:
IComparer<> is implemented on a type that is capable of comparing two different objects.