Sample questions
Question 1Easy
What happens when you read dict["x"] from a Dictionary<string,int> and the key is missing?
- It returns 0, the default value for an int
- KeyNotFoundException is thrown
- It returns null, like a missing value in a map
- The key is added with value 0
Answer: B. KeyNotFoundException is thrown
Explanation
The indexer getter throws KeyNotFoundException. Use TryGetValue to avoid the exception.
Step-by-step solution
- Indexer looks up the key.
- Not found.
- Exception raised.
Why the other options are wrong
- A. Missing keys do not give the default value through the indexer.
- C. An int cannot be null, and no value is returned.
- D. The indexer read never adds a key.
Tip: Prefer TryGetValue.
Question 2Medium
How many enumerators will exist if four threads are simultaneously working on an ArrayList object?
- 1
- 3
- 2
- 4
Answer: D. 4
Explanation
Each thread that enumerates the collection gets its own enumerator, so four threads give four enumerators.
Step-by-step solution
- Each thread that enumerates an ArrayList gets its own enumerator object.
- Four threads each get their own enumerator, so four enumerators exist.
- The correct answer 4 is correct.
Why the other options are wrong
- A. 1 would mean the threads share one enumerator, which they do not.
- B. 3 is one short; each of the four threads gets its own enumerator.
- C. 2 is too few; there are four threads.
Tip: A new enumerator is made per enumeration, so count the enumerating threads or loops.
Question 3Medium
Which of the following statements are correct about a HashTable collection?
I. It is a keyed collection.
II. It is a ordered collection.
III. It is an indexed collection.
IV. It implements a IDictionaryEnumerator interface in its inner class.
V. 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.
- 1 and 2 only
- 1, 2 and 3 only
- 4 and 5 only
- 1, 4 and 5 only
Answer: D. 1, 4 and 5 only
Explanation
A Hashtable is a collection that stores (Keys, Values) pairs. Here, the Keys are used to find the storage location and is immutable and cannot have duplicate entries in the Hashtable. The program displays all the DictionaryEntry objects returned from the enumerator
Step-by-step solution
- A Hashtable is a keyed collection, but not ordered or indexed.
- II and III are false.
- IV and V are true, about IDictionaryEnumerator.
- So option D is correct, which is 1, 4 and 5 only.
Why the other options are wrong
- A. It claims the Hashtable is ordered.
- B. It claims the Hashtable is ordered and indexed.
- C. It leaves out I, which is true.
Tip: A Hashtable is a key-value store with no order.
Question 4Medium
Which of the following statements are correct about the Collection Classes available in Framework Class Library?
- Elements of a collection cannot be transmitted over a network.
- Elements stored in a collection can be retrieved but cannot be modified.
- They use efficient algorithms to manage the collection, thereby improving the performance of the program
- Elements stored in a collection can be modified only if allelements are of similar types.
Answer: C. They use efficient algorithms to manage the collection, thereby improving the performance of the program
Explanation
The .NET Framework class library is a library of classes, interfaces, and value types that provide access to system functionality.
It is the foundation on which .NET Framework applications, components, and controls.
Step-by-step solution
- Collection classes in .NET use efficient algorithms to manage data, which improves program performance.
- Elements of a collection can be sent over a network, can be changed, and do not all need the same type.
- So options A, B and D are wrong.
- Only option C correctly describes the collection classes.
Why the other options are wrong
- A. Elements can be serialized and transmitted over a network using protocols like SOAP.
- B. Elements in collections can be modified if the collection allows it, such as List<T>.
Tip: Remember that Collection Classes focus on efficient data management and flexibility.
Question 5Medium
Which of the following statements are correct about an ArrayList collection that implements the IEnumerable interface?
I. The ArrayList class contains an inner class that implements the IEnumerator interface.
II. An ArrayList Collection cannot be accessed simultaneously by different threads.
III. The inner class of ArrayList can access ArrayList class's members.
IV. To access members of ArrayList from the inner class, it is necessary to pass ArrayList class's reference to it.
V. Enumerator's of ArrayList Collection can manipulate the array.
- 1 and 2 only
- 1 and 3 and 4 only
- 2 and 5 only
- All of the above
Answer: B. 1 and 3 and 4 only
Explanation
Initializes a new instance of the ArrayList class that contains elements copied from the specified collection and that has the same initial capacity as the number of elements copied. ArrayList, consider using List<T>. ArrayList may not always offer the best performance for a given task. See the "Performance Considerations" section in the List<T> reference topic for a discussionof the relative performance of these classes.
Step-by-step solution
- Statement I is true: ArrayList has a nested class that implements IEnumerator.
- Statement II is false: ArrayList is not thread-safe, but threads can still access it if they lock it.
- Statement III is true: the nested class can access the members of its outer class.
- Statement IV is true: the nested class needs a reference to the ArrayList to reach its members.
- Statement V is false: an enumerator only reads; it cannot change the array. So the answer is option B (1, 3 and 4 only).
Why the other options are wrong
- A. Statement 2 is false, so '1 and 2 only' is wrong.
- C. Statements 2 and 5 are both false, so '2 and 5 only' is wrong.
- D. Statements 2 and 5 are false, so 'all of the above' is wrong.
Tip: Check each statement of a list on its own; one false part makes an option wrong.
Question 6Medium
Which of the following statements are correct about the C#.NET code snippet given below?
Stack st = new Stack();
st.Push("hello");
st.Push(8.2);
st.Push(5);
st.Push('b');
st.Push(true);
- This is a perfectly workable code
- Boolean values can never be stored in Stack collection.
- In the fourth call to Push(), we should write "b" in place of 'b'.
- To store dissimilar elements in a Stack collection, a method PushAnyType() should be used in place of Push().
Answer: A. This is a perfectly workable code
Explanation
Push method take parameter as object. Hence any data type can be stored in the Stack object.
Step-by-step solution
- Stack in C# is a collection that stores objects.
- Push method accepts any object type as parameter.
- String, double, int, char, and bool are all objects in C#
- So all data types can be pushed into Stack.
- This makes the code perfectly workable.
Why the other options are wrong
- B. Boolean values can be stored in Stack as they are object type.
- C. 'b' is a char and is valid for Push method.
- D. No such method PushAnyType() exists in Stack class.
Tip: Remember, Stack in C# works with objects, so any data type can be stored.
Question 7Medium
In which of the following collections is the Input/Output based on a key?
I. Map.
II. Stack.
III. BitArray.
IV. HashTable.
V. SortedList.
- 1 and 2 only.
- 2 and 3 only.
- 1, 2 and 3 only.
- 4 and 5 only
Answer: D. 4 and 5 only
Explanation
Key cannot be null in Hash table. Because it is primary key.A key cannot be null, but a value can be. To distinguish between null that is returned because the specified key is not found and null that is returned because the value of the specified key is null, use the Contains method or the Contains Key method to determine if the key exists in the sortedlist.
Step-by-step solution
- A HashTable stores key/value pairs and looks items up by key (IV).
- A SortedList stores key/value pairs sorted by key, and looks items up by key (V).
- Stack and BitArray are looked up by position or index, not by key (II and III).
- Map (I) is not a standard .NET collection class here, so only IV and V are key-based: option D, 4 and 5 only.
Why the other options are wrong
- A. Includes Map (I) and Stack (II); neither is a key-based collection.
- B. Includes Stack (II), which is not key-based.
- C. Includes Map (I), Stack (II) and BitArray (III), none of which are key-based.
Tip: Key-based collections are HashTable, SortedList and Dictionary; Stack and BitArray are not key-based.
Question 8Medium
In a HashTable Key cannot be null, but Value can be.
- True
- False
Answer: A. True
Explanation
Key cannot be null in Hash table. Because it is primary key. A key cannot be null, but a value can be. To distinguish between null that is returned because the specified key is not found and null that is returned because the value of the specified key is null, use the Contains method or the Contains Key method to determine if the key exists in the list.
Step-by-step solution
- A Hashtable key identifies an entry, so it cannot be null.
- A value can be null.
- So the statement is True, and the correct answer True is correct.
Why the other options are wrong
- B. False is wrong; keys cannot be null, but values can.
Tip: Keys must be non-null; values may be null.
Question 9Medium
Which of the following statements are correct about the Stack collection?
- I. It can be used for evaluation of expressions.
- II. All elements in the Stack collection can be accessed using an enumerator.
- III. It is used to maintain a FIFO list.
- IV. All elements stored in a Stack collection must be of similar type.
- V. Top-most element of the Stack collection can be accessed using the Peek() method.
- 1 and 2 only.
- 3 and 4 only.
- 1, 2 and 5 only.
- All of the above
Answer: C. 1, 2 and 5 only.
Explanation
Peek does not remove the element from the Stack collection. It only gets the value"it "peeks" at the value. The Stack class overrides the
GetEnumerator() method by implementing the IEnumerable interface.
Step-by-step solution
- Statement I is true: a stack is used to evaluate expressions.
- Statement II is true: Stack implements IEnumerable, so its items can be enumerated.
- Statement III is false: a stack is LIFO (last in, first out), not FIFO.
- Statement IV is false: the non-generic Stack can hold items of mixed types.
- Statement V is true: Peek returns the top item without removing it. So the answer is option C (1, 2 and 5 only).
Why the other options are wrong
- A. Statement 5 is also correct, so '1 and 2 only' is incomplete.
- B. Statements 3 and 4 are false, so '3 and 4 only' is wrong.
- D. Statements 3 and 4 are false, so 'all of the above' is wrong.
Tip: Stack is LIFO; Peek reads the top without removing it, and Pop removes it.
Practise all 9 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →