C# Generics and Collections Fundamentals Quiz Quiz

Explore the essentials of Generics and Collections in C#, covering their usage, advantages, and common types. This quiz is designed to reinforce your understanding of generic lists, dictionaries, collections methods, and type safety in C# programming.

  1. Understanding Generic Lists

    Which C# collection allows you to store elements of a specific type and provides dynamic resizing as shown in: Listu003Cintu003E numbers = new Listu003Cintu003E();?

    1. Hashtable
    2. Listu003CTu003E
    3. ArrayList
    4. Queue

    Explanation: Listu003CTu003E is a generic collection that can hold elements of a specified type and automatically resizes as elements are added or removed. ArrayList is a non-generic collection that stores objects, not specific types, leading to possible type safety issues. Queue is used for first-in, first-out operations and does not focus on dynamic resizing in the same context. Hashtable is a non-generic collection used for key-value storage without type enforcement.

  2. Type Safety in Generics

    Why are generics important for collections like Listu003CTu003E with respect to type safety in C#?

    1. They prevent adding objects of the wrong type to the collection
    2. They make all collection items immutable
    3. They reduce the memory usage of the collection
    4. They automatically sort the collection items

    Explanation: Generics enforce type safety by restricting the collection to one type, preventing runtime errors due to invalid types. While generics do not automatically sort items, they allow you to specify sorting methods if needed. Generics do not inherently reduce the overall memory usage or make the items immutable; those are separate concerns.

  3. Dictionary Collection Usage

    Which generic collection would you use to store pairs of keys and values, such as assigning names to ages, in C#?

    1. Stacku003CTu003E
    2. Listu003CTu003E
    3. Dictionaryu003CTKey, TValueu003E
    4. Queueu003CTu003E

    Explanation: Dictionaryu003CTKey, TValueu003E is designed for key-value pairs, making it suitable for scenarios like mapping names to ages. Stacku003CTu003E is a last-in, first-out collection and does not store pairs. Listu003CTu003E is for single-value collections. Queueu003CTu003E operates on a first-in, first-out basis, but does not pair keys with values.

  4. Syntax for Declaring Generics

    What is the correct way to declare a generic method in C# that accepts a parameter of any type?

    1. public T ShowValueu003CTu003E(value)
    2. public void ShowValue(T value)
    3. public void ShowValueu003CTu003E(T value)
    4. public void ShowValueu003CTu003E()

    Explanation: The correct syntax is public void ShowValueu003CTu003E(T value), specifying the type parameter u003CTu003E and using it as the method parameter. The option public void ShowValue(T value) is incorrect because T would not be recognized without being declared as a generic type. public void ShowValueu003CTu003E() accepts no arguments, and public T ShowValueu003CTu003E(value) has a syntax error as the parameter lacks a type.

  5. Removing Elements from Generic Lists

    Which method would you use to remove an element by value from a Listu003CTu003E collection in C#?

    1. Discard
    2. Remove
    3. Delete
    4. Pop

    Explanation: The Remove method is specifically designed to remove an element by its value from a Listu003CTu003E in C#. Delete and Discard are not valid methods in this context. Pop is used with stack collections, not lists. Hence, Remove is the correct and only valid option here.

  6. Specifying Multiple Type Parameters

    When declaring a generic class with two type parameters in C#, which syntax is appropriate?

    1. class Pairu003CT, Uu003E {}
    2. class Pairu003CT-Uu003E {}
    3. class Pair[] {}
    4. class Pairu003Cu003E {}

    Explanation: The correct way to declare a generic class with two type parameters is class Pairu003CT, Uu003E {}. The syntax Pairu003Cu003E lacks type parameters and is invalid. Pairu003CT-Uu003E uses a dash instead of a comma, which is incorrect. Pair[] does not declare generic type parameters at all.

  7. Sorting with Generic Lists

    Which built-in method would you use to sort a Listu003Cstringu003E in C# alphabetically?

    1. Shuffle
    2. Order
    3. Sort
    4. Arrange

    Explanation: Sort is the built-in method that orders elements in a generic list, such as Listu003Cstringu003E, in ascending order. Arrange and Order are not methods available to Listu003CTu003E. Shuffle is a concept for randomizing order, not sorting alphabetically, and is not a default List method.

  8. Stacku003CTu003E Characteristics

    Which operation retrieves and removes the top element from a Stacku003CTu003E collection in C#?

    1. Pop
    2. Enqueue
    3. Peek
    4. RemoveAt

    Explanation: Pop is the method used for retrieving and removing the top element of a Stacku003CTu003E, following the last-in, first-out principle. Peek returns the top element without removing it. Enqueue is for adding elements to a queue, and RemoveAt is a method for lists, not stacks.

  9. Checking for Key Existence in Dictionary

    How do you safely determine if a Dictionaryu003Cint, stringu003E contains a specific key before accessing its value?

    1. Use the HasValue method
    2. Use the ContainsKey method
    3. Access the value directly and handle an exception
    4. Use the Exists method

    Explanation: The ContainsKey method allows you to check if a key exists before attempting to access its value in a dictionary. Exists and HasValue are not valid methods for dictionaries in this context. Accessing the value directly risks an exception if the key is missing, so it is less safe compared to using ContainsKey.

  10. Default Value in Generics

    In a generic method, what does the default keyword return for a value type parameter T in C#?

    1. It returns an uninitialized random value
    2. It throws an exception
    3. It returns zero for numeric types and false for bool
    4. It returns null for all types

    Explanation: For value types, default returns the zero-equivalent, such as 0 for integers and false for bool. For reference types, default returns null, but this is not the case for all types as suggested in the second option. It does not produce random values or throw exceptions.