0% found this document useful (0 votes)
16 views17 pages

1.1 8_Collections_and_Generics_Multiple_Choice_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)
16 views17 pages

1.1 8_Collections_and_Generics_Multiple_Choice_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/ 17

Deccansoft Software Services-MS.

NET Collections and Generics

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.

2. Which of the following are benefits of the Collection


a. Collection have builtin sorting facility.
b. They provide best performance because they are indexed.
c. We have to manage memory of collections as we require.
d. Collections cannot be readonly or of fixed size.
e. We can easily iterate through the collection.

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.

3. Every collection class must implement ____________ interface?


a. IEnumerator
b. IEnumerable
c. IList
d. IDictionary

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

6. Which of the following interface(s) are implemented by ArrayList


a) IList
b) ICollection
c) IDictionary
d) IEnumerator
e) IEnumerable

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.

7. ___________ implements the IList interface?

a. Stack
b. ArrayList
c. Queue
d. Hashtable

Ans:b

Explanation
ArrayList implements the IList interface.

8. 6)What would be the capacity of ArrayList with 1 item added to it.


a. 1
b. 2
c. 4
d. 16

Ans:c

9. What is the output of the following program as printed on console window


class Program
{
static void Main(string[] args)
{
ArrayList al = new ArrayList();
al.Add(1);
al.Add('1');
al.Add("1");
foreach (string s in al)
{
Console.Write(s);
}
}
}
a) Compilation Error
b) Runtime Exception
c) 1 1 1
d) 1 '1' "1"
Ans) b
Deccansoft Software Services-MS.NET Collections and Generics

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.

11. How can we decrease the Capacity of Collection


a) Calling TrimToSize method
b) Calling Clear method
c) Setting Capacity explicitly
d) Calling TrimExcess

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.

13. Which of the following are true about the ArrayList


a) ArrayList can dynamically grow in size
b) ArrayList are of fixed size
c) ArrayList is used for managing KeyValue pair
d) ArrayList can have zero Count
e) In ArrayList any type of item/element can be added.

Ans) a, d, e

14. Elements in ArrayList can be accessed using _____________


a. Only Key
b. Only Index
c. Either Key or Index
d. both Key and Index

Ans:b

Explanation
Deccansoft Software Services-MS.NET Collections and Generics

Arraylist elements organized based on index/value and also accessed based on index.

15. Which of the following collections operate based on Key


a) ArrayList
b) Hashtable
c) SortedList
d) Stack

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.

17. Which of the following are members of IEnumerator


a. Reset
b. Current
c. MoveNext
d. Read
e. GetEnumerator

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")

18. What will happen to existing item at index 2


a. It is overwritten with new item
b. It is slided to next position with index 3
c. New item is added after existing item at index 3
d. It gives error.
Ans:b
Explanation

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

20. Fill in the blank below.


21. string[] ar = (_______)al.ToArray(typeof(_______));
a. string[], string[]
b. string, string
c. string[], string
d. string, string[]

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.

22. Can we add duplicate elements(values) into ArrayList?


a) Yes
b) No

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.

24. What is the o/p of following


static void Main(string[] args)
{
ArrayList al = new ArrayList();
al.Add("One");
al.Add("Two");
al.Add("Three");
foreach (string s in al)
{
al.Add("Four");
Console.Write(s);
}
}

a) One Two Three


b) One Two Three Four
Deccansoft Software Services-MS.NET Collections and Generics

c) Compilation Error
d) Runtime Error

Ans) d
Explanation:
Collections cannot be modified while in foreach statement.

25. In Hashtable Key ________ be null and value __________ be null.


a. can be, can be
b. can't be , can be
c. can be, can't be
d. can't be, can't be

Ans:b
Explanation
Hashtable key should not be null based on key we are adding value.

26. In HashTable Key _______ unique, value _______ be unique


a. need not be, need not to be
b. must be, must be
c. need not be, must be
d. must be, need not be

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

29. Which of the following are valid methods of Hashtable


a. Add
b. RemoveAt
Deccansoft Software Services-MS.NET Collections and Generics

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.

32. Fill in the blank


Hashtable ht = new Hashtable();
ht.Add(101, "S1");
ht.Add(102, "S2");
foreach (________________ ob in ht)
{...}
a. String
b. int
c. DictionaryEntry
d. KeyValuePair

Ans:c
Explanation:
Each element is a key/value pair stored in a DictionaryEntry object.

33. Fill in the blank


ArrayList al = new ArrayList()
...
IEnumerator en = al.GetEnumerator();
while (en.________())
{
Employee e = (Employee)en.Current;
Console.WriteLine(e);
}
Deccansoft Software Services-MS.NET Collections and Generics

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.

34. Fill in the blank:


class Program
{
static void Main(string[] args)
{
ArrayList al = new ArrayList();
al.Add(1);
al.Add('1');
al.Add("1");
IEnumerator en = al._________();
while (en.MoveNext())
{
object ob = en.Current;
}
}
}
a) IEnumerator
b) GetEnumerator
c) GetEnumerable
d) GetItems

Ans) b
Explanation:
ArrayList implements IEnumerator and thus the method GetEnumerator().

35. ArrayList alEmp = new ArrayList();


alEmp.Add(new Employee() { Id = 4, Name = "E3" });
alEmp.Add(new Employee() { Id = 5, Name = "E4" });
alEmp.Sort();
If Sort method should not through a runtime exception, which interface must be implemented by
Employee class
a) IEnumerable
b) ISortable
c) IComparer
d) IComparable

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.

36. Fill in the blank


ArrayList alEmp = new ArrayList();
alEmp.Add(new Employee() { Id = 4, Name = "E3" });
alEmp.Add(new Employee() { Id = 5, Name = "E4" });
___________ ec = new EmployeeComparer();
Deccansoft Software Services-MS.NET Collections and Generics

alEmp.Sort(ec);
a. IComparer
b. IComparable
c. ISortable
d. IEmployee

Ans:a

Explanation:
EmployeeComparer inherited from IComparer interface.It returns EmployeeComparer.

Generics

37. Which of the following are advantages of Generics


a. Type safety
b. Reusablity of Code
c. Scalablity
d. High Performance
e. Thread Safety

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.

39. Generic types enforce type compliance at ____________


a. Compiletime
b. Runtime
c. Developmentime
d. Are not type compliant

Ans:a

Explanation:
The runtime generates another version of the generic type and substitutes a particular data type in the
appropriate locations in MSIL.

40. Which of the following statements are valid about generics


a) Generic is a language feature
b) Structures cannot be Generic
c) Interface cannot be Generic
Deccansoft Software Services-MS.NET Collections and Generics

d) Generic types can have constraints

Ans) a, d

41. class Demo<T>


{
public void Foo(T a)
{}
}
Which of the following is valid syntax for object creation?
a) Demo d = new Demo();
b) Demo<int> d = new Demo();
c) Demo d = new Demo<int>();
d) Demo<int> d = new Demo<int>();

Ans) d

42. using System;


class Demo<T>
{
public void Foo(T a)
{}
}
class Program
{
static void Main(string[] args)
{
Demo<int> d = new Demo<int>();
X a = default(X);
d.Foo(a);
}
}
In the above code which of the following is valid for placeholder X
a) int
b) double
c) T
d) short

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);
}
}

A. It will print string "Hello" on the console.


B. Name Generic cannot be used as a class name because it's a keyword.
C. Compiler will give an error.
D. Member Field1 of class Generic is not accessible directly

Ans) a

44. What is the output of the following code:


using System;
class Demo<T>
{
public T Add(T t1, T t2)
{
return t1 + t1;
}
}
class Program
{
static void Main(string[] args)
{
int n1 = 10; int n2 = 20 ;
Demo<int> d = new Demo<int>();
int res = d.Add(n1,n2);
Console.WriteLine(res);
}
}
a) 20
b) 30
c) Compilation Error
d) Runtime Exception

Ans) c
Explanation:
Compiler will report an error: Operator '+' is not defined for types T and int

45. 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:
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

46. class Demo<T> where T:struct


{
public void Foo(T a)
{}
}
Which of the following are valid
a) Demo<int> d = new Demo<int>();
b) Demo<string> d = new Demo<string>();
c) Demo d = new Demo();
d) Demo<bool> d = new Demo<bool>

Ans) a, d
Explanation:
Because of the constraint struct, only value types or structure type are allowed for type argument.

47. class Demo<T> where T:new()


{
...
}
Which of the following is valid statements?
a) Demo class must have parameterless public constructor
b) Demo class must have atleast one form of constructor
c) Demo class have parameterless constructor which can be either public or private
d) Demo class cannot be instantiated.

Ans) a

48. If the class is declared as below


class Stack where T: class
{}
Which of the following are valid for instantiating the object of Stack.
a. Stack<T> s1 = new Stack<T>();
b. Stack<int> s1 = new Stack<int>();
c. Stack<object> s1 = new Stack<object>();
d. Stack s1 = new Stack

Ans:c

Explanation:
When constraint class is provided, only reference types can be used for type argument

49. class Demo<T> where T: C1, I1


{
...
}
Which of the following are valid statements?
a) The type argument must be or derive from C1 class.
b) The type argument must be or implement the interface I1.
c) The type argument must be either derived from C1 or implement I1
d) Both a and b
Deccansoft Software Services-MS.NET Collections and Generics

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.

50. class Demo


{
public T Foo<T>(T t)
{
return t;
}
}
class Program
{
static void Main(string[] args)
{
Demo d = new Demo();
d.Foo(10);
}
}
Which of the following statements are valid calls
a) d.Foo(10);
b) d.Foo(null);
c) d.Foo<int>(10);
d) d.Foo<float>(10.5);

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

51. Which is the generic collection equivalent of ArrayList


a) List
b) Dictionary
c) GenericList
d) GenericArray

Ans) a
System.Collections.Generic.List is generic collection equivalent of ArrayList

52. What is a List?


a. It is a collection of key value pair.
b. It is an index based collection
c. List is a nongeneric form of ArrayList
d. List is a generic form 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.

53. class Program


{
static void Main(string[] args)
{
List<int> lst = new List<int>();

}
}
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

54. class Program


{
static void Main(string[] args)
{
List<string> lst = new List<string>();
lst.Add("s1");
lst.Add("s2");
lst.Add("s3");
string[] strs = (____________) lst.ToArray();
}
}
Fill in the blank above
a) object
b) string []
c) Array
d) Blank Explicit casting is not required.

Ans) d
Explanation:
Generic List version of ToArray returns the array of same type for which List is instantiated. Hence
casting is not required.

55. Fill in the blank:


class Student : IComparable<Student>
{
public int CompareTo(__________ other)
Deccansoft Software Services-MS.NET Collections and Generics

{
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.

57. Which is the equivalent of Generic version of Hashtable


a. List
b. Dictionary
c. GenericHashtable
d. GenericDictionary

Ans:b

Explanation:
Dictionary is generic version of hashtable it is more type safety.

58. Based on the below sample code, fill in the blanks


Dictionary<______,_________> dict = new Dictionary<______,_________>();
dict.Add(101, new Student(1, "A1"));
dict.Add(102, new Student(2, "A2"));
dict.Add(103, new Student(3, "A3"));
dict.Add(104, new Student(4, "A4"));
a. int, string
b. string, int
c. int, Student
d. string, Student

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.

59. Fill in the blank


List<Student> lst = new List<Student>();
lst.Add(new Student(2, "A2"));
lst.Add(new Student(1, "A1"));
foreach (__________ s in lst)
Console.WriteLine(s.Id + " " + s.Name);
a. int
b. string
c. Student
d. Object

Ans:c

Explanation:
We created list object type as student so that must and should type as student.

60. Fill In the blank:


List<Student> lst = new List<Student>();
lst.Add(new Student(2, "A2"));
lst.Add(new Student(1, "A1"));
IEnumerator en = lst.GetEnumerator();
while (en.MoveNext())
{
________ s = en.Current;
Console.WriteLine(s.Id + " " + s.Name);
}
a. int
b. string
c. Student
d. Object

Ans:c

Explanation:
We created list object type as Student, so that must and should type as Student.

61. Fill in the blank


class Program
{
static void Main(string[] args)
{
Dictionary<int, string> dic = new Dictionary<int, string>();
dic.Add(101, "Student1");
dic.Add(102, "Student2");
dic.Add(103, "Student3");
dic.Add(104, "Student4");
dic.Remove(102);
foreach (___________ kv in dic)
Deccansoft Software Services-MS.NET Collections and Generics

{
int key = kv.Key;
string value = kv.Value;
Console.WriteLine(key + " " + value);
}
}
}

a) KeyValuePair
b) KeyValuePair<int, string>
c) DictionaryEntry
d) string

class Student : IComparer<_______>


{
//code goes here...
}
Which of the following is valid if Student must be sortable by ID when added to List
a. int
b. List
c. Student
d. string

Ans:c

Explanation:
IComparer<> is implemented on a type that is capable of comparing two different objects.

You might also like