MCQs on Reflection and Attributes | C#

Chapter 17 of C# covers Reflection and Attributes, enabling developers to inspect types and assemblies at runtime. It also explores the creation of custom attributes and their usage for code documentation, enhancing the flexibility and maintainability of your code. This chapter is essential for mastering dynamic programming in C# and utilizing metadata for various tasks like debugging and testing.


MCQs on Reflection and Attributes in C#

Section 1: Understanding Reflection (10 Questions)

  1. What is Reflection in C#?
    • a) A process of compiling code
    • b) A way to inspect and manipulate metadata at runtime
    • c) A debugging tool
    • d) A way to create custom methods
  2. Which namespace in C# contains classes for Reflection?
    • a) System.IO
    • b) System.Reflection
    • c) System.Text
    • d) System.Runtime
  3. What does the GetType() method return in C#?
    • a) The name of the type
    • b) A Type object representing the type of the current instance
    • c) The assembly where the type is declared
    • d) The constructor of the type
  4. What type of object does Type.GetProperties() return?
    • a) List of property names
    • b) Array of PropertyInfo objects
    • c) A Property object
    • d) A collection of methods
  5. What method is used to obtain metadata about a class’s methods in Reflection?
    • a) GetMethods()
    • b) GetFields()
    • c) GetConstructors()
    • d) GetProperties()
  6. Which of the following is true about Reflection?
    • a) It can only be used for classes in the same assembly
    • b) It allows access to private members of types
    • c) It is limited to static types
    • d) It does not work with interfaces
  7. How can you invoke a method using Reflection in C#?
    • a) Using Invoke() method on MethodInfo
    • b) Using MethodInfo.Invoke() on Type
    • c) Using Method class directly
    • d) Reflection does not allow invoking methods
  8. Which type of Reflection method allows you to examine the fields of a type?
    • a) GetFields()
    • b) GetMethods()
    • c) GetProperties()
    • d) GetEvents()
  9. Which of the following does not return a Type object?
    • a) typeof()
    • b) GetType()
    • c) GetAssembly()
    • d) Assembly.GetType()
  10. Which of the following is a reason for using Reflection in C#?
    • a) To manipulate data at runtime
    • b) To inspect types and their members dynamically
    • c) To optimize performance
    • d) To enforce security restrictions

Section 2: Using Reflection to Inspect Types and Assemblies (10 Questions)

  1. What class in C# is used to load assemblies at runtime?
    • a) Assembly
    • b) Type
    • c) TypeInfo
    • d) Reflection
  2. How can you get all the types in an assembly using Reflection?
    • a) Using Assembly.GetTypes()
    • b) Using Type.GetTypes()
    • c) Using Type.GetMethods()
    • d) Using Assembly.Load()
  3. Which method in Assembly allows loading an assembly from a specific file path?
    • a) Assembly.LoadFrom()
    • b) Assembly.Load()
    • c) Assembly.GetExecutingAssembly()
    • d) Assembly.LoadFile()
  4. What does Assembly.GetExportedTypes() return?
    • a) All internal types of the assembly
    • b) All types defined in the assembly
    • c) Only publicly accessible types
    • d) All methods in the assembly
  5. How do you obtain an Assembly object from a type in C#?
    • a) type.GetAssembly()
    • b) Assembly.Load(type)
    • c) Type.Assembly()
    • d) type.Assembly
  6. Which of the following is used to inspect method parameters in Reflection?
    • a) MethodInfo.GetParameters()
    • b) PropertyInfo.GetParameters()
    • c) Type.GetMethods()
    • d) FieldInfo.GetParameters()
  7. What method do you use to load an assembly by its name using Reflection?
    • a) Assembly.Load()
    • b) Assembly.GetType()
    • c) Type.GetAssembly()
    • d) Assembly.LoadFrom()
  8. Which method would you use to examine the attributes of a type in Reflection?
    • a) GetCustomAttributes()
    • b) GetMethods()
    • c) GetProperties()
    • d) GetEvents()
  9. How do you get a list of all methods from a specific class in C# using Reflection?
    • a) Type.GetMethods()
    • b) Assembly.GetMethods()
    • c) MethodInfo.GetMethods()
    • d) TypeInfo.GetMethods()
  10. How can you retrieve a specific method of a class using Reflection?
    • a) Using GetMethod(name)
    • b) Using GetMethodInfo()
    • c) Using GetSpecificMethod()
    • d) Reflection does not allow specific method retrieval

Section 3: Custom Attributes (5 Questions)

  1. How do you define a custom attribute in C#?
    • a) By inheriting from Attribute
    • b) By implementing IAttribute
    • c) By creating a new class with public modifier
    • d) By using the custom keyword
  2. Which of the following is required when defining a custom attribute?
    • a) A constructor
    • b) The Attribute suffix in the class name
    • c) The public access modifier
    • d) The class must inherit from object
  3. Where can custom attributes be applied in C#?
    • a) Only on classes
    • b) Only on methods
    • c) Only on fields
    • d) Classes, methods, properties, and fields
  4. What is the primary purpose of using custom attributes in C#?
    • a) To manage permissions
    • b) To define metadata for classes and methods
    • c) To speed up execution
    • d) To define inheritance rules
  5. How can you access the values of custom attributes at runtime in C#?
    • a) Using Reflection
    • b) Using GetValue()
    • c) By directly accessing the field
    • d) Attributes cannot be accessed at runtime

Section 4: Using Attributes for Code Documentation (5 Questions)

  1. What type of attribute is commonly used for documenting code in C#?
    • a) DocumentationAttribute
    • b) XmlDocumentationAttribute
    • c) ObsoleteAttribute
    • d) SummaryAttribute
  2. Which attribute marks a method as deprecated or obsolete in C#?
    • a) DeprecatedAttribute
    • b) ObsoleteAttribute
    • c) DeprecatedMethodAttribute
    • d) OutdatedAttribute
  3. How do you provide a description for an obsolete method in C#?
    • a) Obsolete("This method is outdated.")
    • b) Obsolete("Use new version")
    • c) Obsolete("Deprecated")
    • d) All of the above
  4. Which attribute is used to suppress warnings generated by the compiler in C#?
    • a) SuppressWarningAttribute
    • b) CompilerWarningAttribute
    • c) SuppressMessageAttribute
    • d) NoWarningAttribute
  5. How do you apply an attribute to a class in C#?
    • a) public class MyClass : Attribute
    • b) [MyCustomAttribute]
    • c) public MyCustomAttribute() {}
    • d) MyCustomAttribute.MyClass()

Answer Key

QnoAnswer
1b) A way to inspect and manipulate metadata at runtime
2b) System.Reflection
3b) A Type object representing the type of the current instance
4b) Array of PropertyInfo objects
5a) GetMethods()
6b) It allows access to private members of types
7a) Using Invoke() method on MethodInfo
8a) GetFields()
9c) GetAssembly()
10b) To inspect types and their members dynamically
11a) Assembly
12a) Using Assembly.GetTypes()
13a) Assembly.LoadFrom()
14c) Only publicly accessible types
15d) type.Assembly
16a) MethodInfo.GetParameters()
17a) Assembly.Load()
18a) GetCustomAttributes()
19a) Type.GetMethods()
20a) Using GetMethod(name)
21a) By inheriting from Attribute
22a) A constructor
23d) Classes, methods, properties, and fields
24b) To define metadata for classes and methods
25a) Using Reflection
26b) XmlDocumentationAttribute
27b) ObsoleteAttribute
28d) All of the above
29c) SuppressMessageAttribute
30b) [MyCustomAttribute]

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