Ticker

6/recent/ticker-posts

Interview question and answer on C# Collection framework

Interview question and answer on C# Collection framework with full explanation and code example.




Here are the top 25 interview questions and answers on C# Collection framework, along with detailed explanations and code examples:
  1. Q: What is a collection in C#? A: A collection in C# is a group of related objects that can be accessed and manipulated as a single unit. It provides various data structures and algorithms to store and manage data efficiently.

  2. Q: What are the main types of collections in C#? A: The main types of collections in C# are:

    • Lists: Ordered collection of elements.
    • Arrays: Fixed-size collection of elements.
    • Dictionaries: Key-value pairs collection.
    • Queues: First-in, first-out (FIFO) collection.
    • Stacks: Last-in, first-out (LIFO) collection.
    • Sets: Unique collection of elements.
  3. Q: Explain the List collection in C#. A: List is an ordered collection that allows duplicate elements. It provides dynamic resizing, indexing, searching, sorting, and other operations. Here's an example:

    csharp
    List<int> numbers = new List<int>(); numbers.Add(1); numbers.Add(2); numbers.Add(3); Console.WriteLine(numbers[1]); // Output: 2
  4. Q: What is the difference between an array and a List? A: Arrays have a fixed size, while Lists can dynamically resize. Arrays use square brackets for indexing, while Lists use the Indexer property. Arrays require manual resizing, while Lists handle resizing automatically.

  5. Q: Explain the Dictionary collection in C#. A: Dictionary is a collection of key-value pairs. It provides fast lookup and retrieval of elements based on the key. Duplicate keys are not allowed. Here's an example:

    csharp
    Dictionary<string, int> ages = new Dictionary<string, int>(); ages.Add("John", 25); ages.Add("Alice", 30); Console.WriteLine(ages["John"]); // Output: 25
  6. Q: What is the difference between a Dictionary and a Hashtable? A: Both Dictionary and Hashtable store key-value pairs, but Hashtable is a legacy class from earlier versions of .NET, while Dictionary is a generic class introduced in .NET 2.0. Dictionary provides type safety and better performance compared to Hashtable.

  7. Q: Explain the Queue collection in C#. A: Queue is a collection that follows the FIFO (First-In, First-Out) principle. It allows adding elements at the end and removing elements from the front. Here's an example:

    csharp
    Queue<string> names = new Queue<string>(); names.Enqueue("John"); names.Enqueue("Alice"); Console.WriteLine(names.Dequeue()); // Output: John
  8. Q: Explain the Stack collection in C#. A: Stack is a collection that follows the LIFO (Last-In, First-Out) principle. It allows adding elements at the top and removing elements from the top. Here's an example:

    csharp
    Stack<string> names = new Stack<string>(); names.Push("John"); names.Push("Alice"); Console.WriteLine(names.Pop()); // Output: Alice
  9. Q: Explain the HashSet collection in C#. A: HashSet is a collection that stores unique elements. It provides fast lookup and ensures that no duplicate elements are present. Here's an example:

    csharp
    HashSet<int> numbers = new HashSet<int>(); numbers.Add(1); numbers.Add(2); numbers.Add(2); // Ignored (already exists) Console.WriteLine(numbers.Count); // Output: 2
  10. Q: What is the difference between a List and a HashSet? A: Lists allow duplicate elements, while HashSets store unique elements only. Lists maintain the insertion order, while HashSets do not guarantee any specific order.

  11. Q: Explain the SortedList collection in C#. A: SortedList is a collection that maintains the elements in sorted order based on the keys. It provides efficient searching, insertion, and removal of elements. Here's an example:

    csharp
    SortedList<int, string> items = new SortedList<int, string>(); items.Add(3, "Item 3"); items.Add(1, "Item 1"); items.Add(2, "Item 2"); Console.WriteLine(items.Values[1]); // Output: Item 2
  12. Q: Explain the LinkedList collection in C#. A: LinkedList is a collection that represents a doubly-linked list. It allows efficient insertion, removal, and traversal of elements. Here's an example:

    csharp
    LinkedList<string> names = new LinkedList<string>(); names.AddLast("John"); names.AddLast("Alice"); Console.WriteLine(names.First.Value); // Output: John
  13. Q: Explain the ObservableCollection collection in C#. A: ObservableCollection is a collection that provides notifications when the collection is modified. It is useful for data binding scenarios where UI elements need to be updated automatically. Here's an example:

    csharp
    ObservableCollection<string> names = new ObservableCollection<string>(); names.CollectionChanged += (sender, e) => { Console.WriteLine("Collection modified!"); }; names.Add("John");
  14. Q: Explain the BitArray collection in C#. A: BitArray is a collection that stores a compact array of Boolean values. It provides space-efficient storage and bitwise operations on the elements. Here's an example:
  15. csharp
    BitArray bits = new BitArray(4); bits.Set(1, true); bits.Set(3, true); Console.WriteLine(bits.Get(3)); // Output: True
  16. Q: How do you iterate over elements in a collection? A: You can use loops such as foreach or for to iterate over elements in a collection. Here's an example with a List:

    csharp
    List<int> numbers = new List<int> { 1, 2, 3 }; foreach (int number in numbers) { Console.WriteLine(number); }
  17. Q: How do you check if a collection contains a specific element? A: You can use the Contains method to check if a collection contains a specific element. Here's an example:

    csharp
    List<string> names = new List<string> { "John", "Alice" }; bool containsJohn = names.Contains("John"); Console.WriteLine(containsJohn); // Output: True
  18. Q: How do you sort elements in a collection? A: You can use the Sort method or the OrderBy LINQ extension method to sort elements in a collection. Here's an example with a List:

    csharp
    List<int> numbers = new List<int> { 3, 1, 2 }; numbers.Sort(); Console.WriteLine(string.Join(", ", numbers)); // Output: 1, 2, 3
  19. Q: How do you find the maximum or minimum element in a collection? A: You can use the Max or Min methods to find the maximum or minimum element in a collection. Here's an example:

    csharp
    List<int> numbers = new List<int> { 3, 1, 2 }; int maxNumber = numbers.Max(); int minNumber = numbers.Min(); Console.WriteLine(maxNumber); // Output: 3 Console.WriteLine(minNumber); // Output: 1
  20. Q: How do you remove elements from a collection? A: You can use methods like Remove, RemoveAt, or RemoveRange to remove elements from a collection. Here's an example with a List:

    csharp
    List<string> names = new List<string> { "John", "Alice", "Bob" }; names.Remove("Alice"); Console.WriteLine(string.Join(", ", names)); // Output: John, Bob
  21. Q: How do you clear all elements from a collection? A: You can use the Clear method to remove all elements from a collection. Here's an example:

    csharp
    List<int> numbers = new List<int> { 1, 2, 3 }; numbers.Clear(); Console.WriteLine(numbers.Count); // Output: 0
  22. Q: How do you convert a collection to an array? A: You can use the ToArray method to convert a collection to an array. Here's an example:

    csharp
    List<int> numbers = new List<int> { 1, 2, 3 }; int[] array = numbers.ToArray(); Console.WriteLine(string.Join(", ", array)); // Output: 1, 2, 3
  23. Q: How do you convert a collection to a string? A: You can use the string.Join method to concatenate the elements of a collection into a single string. Here's an example:

    csharp
    List<string> names = new List<string> { "John", "Alice" }; string namesString = string.Join(", ", names); Console.WriteLine(namesString); // Output: John, Alice
  24. Q: How do you check if a collection is empty? A: You can use the Count property or the Any method to check if a collection is empty. Here's an example:

    csharp
    List<int> numbers = new List<int>(); bool isEmpty = numbers.Count == 0; Console.WriteLine(isEmpty); // Output: True
  25. Q: How do you copy elements from one collection to another? A: You can use methods like AddRange or Concat to copy elements from one collection to another. Here's an example:

    csharp
    List<int> numbers1 = new List<int> { 1, 2, 3 }; List<int> numbers2 = new List<int>(); numbers2.AddRange(numbers1); Console.WriteLine(string.Join(", ", numbers2)); // Output: 1, 2, 3
  26. Q: How do you check if two collections are equal? A: You can use the SequenceEqual method to check if two collections have the same elements in the same order. Here's an example:

    csharp
    List<int> numbers1 = new List<int> { 1, 2, 3 }; List<int> numbers2 = new List<int> { 1, 2, 3 }; bool areEqual = numbers1.SequenceEqual(numbers2); Console.WriteLine(areEqual); // Output: True

These are some of the top interview questions and answers related to the C# Collection framework. Remember to understand the concepts and practice implementing them to gain confidence in using collections effectively in your code.

Post a Comment

0 Comments