MCQs on Collections and Generics | Visual Basic .NET (VB.NET)

Test your knowledge of VB.NET Collections and Generics with these expertly crafted MCQs. Explore Arrays, ArrayLists, Dictionaries, HashTables, and Generics to master intermediate-level programming concepts effectively.


Topic: Arrays and ArrayList

  1. Which of the following correctly declares an array in VB.NET?
    a) Dim arr(5) As String
    b) Dim arr As String[5]
    c) Dim arr{} As String
    d) Dim arr(5)
  2. What is the default index of the first element in a VB.NET array?
    a) 0
    b) 1
    c) -1
    d) Undefined
  3. What is the difference between an Array and an ArrayList in VB.NET?
    a) Array is dynamic; ArrayList is fixed-size
    b) ArrayList is dynamic; Array is fixed-size
    c) Both are dynamic
    d) Both are fixed-size
  4. How do you add an item to an ArrayList in VB.NET?
    a) AddItem()
    b) Add()
    c) Insert()
    d) Append()
  5. Which property of the ArrayList class gets the number of elements?
    a) Length
    b) Count
    c) Size
    d) Items
  6. Which method clears all elements of an ArrayList?
    a) ClearItems()
    b) Clear()
    c) Reset()
    d) RemoveAll()
  7. In VB.NET, what happens if you try to access an array index out of bounds?
    a) The array is resized
    b) A runtime exception occurs
    c) The program terminates
    d) It returns a default value
  8. How can you create a multidimensional array in VB.NET?
    a) Dim arr(2,3) As Integer
    b) Dim arr[2][3] As Integer
    c) Dim arr(3,2)
    d) Dim arr(3)(2) As Integer

Topic: Dictionaries and HashTables

  1. What is the primary difference between a Dictionary and a HashTable in VB.NET?
    a) Dictionary uses type-safe keys and values; HashTable does not
    b) HashTable is faster than Dictionary
    c) Dictionary allows duplicate keys; HashTable does not
    d) HashTable does not support keys
  2. Which method is used to add a key-value pair to a Dictionary in VB.NET?
    a) Append()
    b) Add()
    c) Insert()
    d) AddKey()
  3. In a HashTable, what happens if a duplicate key is added?
    a) It overwrites the existing value
    b) It throws an exception
    c) It is ignored
    d) It creates a new key
  4. How can you check if a specific key exists in a Dictionary?
    a) ContainsKey()
    b) KeyExists()
    c) FindKey()
    d) HasKey()
  5. What does the GetEnumerator() method in a Dictionary return?
    a) A list of keys
    b) A list of values
    c) A Dictionary enumerator
    d) Key-value pairs
  6. Which namespace is required for using Dictionary and HashTable in VB.NET?
    a) System.IO
    b) System.Collections.Generic
    c) System.Linq
    d) System.Net
  7. How do you remove an entry from a HashTable?
    a) RemoveKey()
    b) Delete()
    c) Remove()
    d) ClearKey()
  8. In VB.NET, what does the Keys property of a Dictionary return?
    a) A list of all keys
    b) A list of all values
    c) A Boolean indicating if keys exist
    d) Null

Topic: Introduction to Generics (List, Dictionary)

  1. What is the primary advantage of generics in VB.NET?
    a) Reduced memory usage
    b) Increased performance
    c) Type safety and flexibility
    d) Simplified syntax
  2. How do you declare a generic List in VB.NET?
    a) Dim list As New List(Of T)
    b) Dim list As New List<T>
    c) Dim list As List(Of T)
    d) Dim list = New List(T)
  3. Which method is used to add an item to a generic List?
    a) Append()
    b) Add()
    c) Insert()
    d) Push()
  4. Can a generic Dictionary have duplicate keys in VB.NET?
    a) Yes
    b) No
  5. What does the ContainsValue() method in a generic Dictionary do?
    a) Checks if a key exists
    b) Checks if a value exists
    c) Finds the key of a value
    d) Clears the dictionary
  6. How do you specify the type of a generic Dictionary?
    a) Dim dict As New Dictionary(Key, Value)
    b) Dim dict As New Dictionary(Of KeyType, ValueType)
    c) Dim dict As Dictionary<Key, Value>
    d) Dim dict As Dictionary(Of Key, Value)
  7. Which method removes an item from a generic List in VB.NET?
    a) RemoveAt()
    b) Delete()
    c) Clear()
    d) Remove()
  8. What is the default value for a generic type parameter in VB.NET?
    a) Null
    b) Zero
    c) The type’s default value
    d) Undefined
  9. Can generics be used with custom types in VB.NET?
    a) Yes
    b) No
  10. Which keyword is used to define a generic class in VB.NET?
    a) Generic
    b) Of
    c) Template
    d) ClassType
  11. What is the difference between a List and a generic Dictionary?
    a) List is key-value based; Dictionary is index-based
    b) Dictionary is key-value based; List is index-based
    c) Both are key-value based
    d) Both are index-based
  12. How do you sort a generic List in VB.NET?
    a) Sort() method
    b) Order() method
    c) Arrange() method
    d) Reorder() method
  13. Can a generic method have multiple type parameters?
    a) Yes
    b) No
  14. What is the correct way to declare a generic method in VB.NET?
    a) Sub Method(Of T)()
    b) Sub Method<T>()
    c) Sub Method<T, U>()
    d) Sub Method(T)()

Answers

QNoAnswer
1a) Dim arr(5) As String
2a) 0
3b) ArrayList is dynamic; Array is fixed-size
4b) Add()
5b) Count
6b) Clear()
7b) A runtime exception occurs
8a) Dim arr(2,3) As Integer
9a) Dictionary uses type-safe keys and values; HashTable does not
10b) Add()
11b) It throws an exception
12a) ContainsKey()
13d) Key-value pairs
14b) System.Collections.Generic
15c) Remove()
16a) A list of all keys
17c) Type safety and flexibility
18a) Dim list As New List(Of T)
19b) Add()
20b) No
21b) Checks if a value exists
22b) Dim dict As New Dictionary(Of KeyType, ValueType)
23a) RemoveAt()
24c) The type’s default value
25a) Yes
26b) Of
27b) Dictionary is key-value based; List is index-based
28a) Sort() method
29a) Yes
30a) Sub Method(Of T)()

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top