C# - Attributes Questions and Answers

Take Exam

C# Attributes programming questions and answers help developers understand metadata and reflection in .NET applications. Attributes provide declarative information about program elements, influencing how the runtime behaves. These concepts are often asked in interviews by companies like HCL, Infosys, and TCS to assess deep understanding of advanced C# features. Practicing programming interview questions with answers on attributes helps candidates learn how to create custom attributes, retrieve metadata using reflection, and apply predefined ones like [Obsolete] or [Serializable]. Strengthening this topic ensures you can confidently tackle conceptual and coding questions in software development interviews.

Focus on attributes and modifiers in C programming language. Also try C functions and subroutines and C datatypes
 

C# - Attributes

Showing 10 of 38 questions

11. Which of the following is the correct way to apply an attribute to an Assembly?

  • [ AssemblyDescription("DCube Component Library") ]
  • [ assembly : AssemblyDescription("DCube Component Library") ]
  • [ Assemblylnfo : AssemblyDescription("DCube Component Library") ]
  • < Assembly: AssemblyDescription("DCube Component Library") >
Show Answer Report

12. Which of the following is the correct way of applying the custom attribute called Tested which receives two-arguments - name of the tester and the testgrade? &nbsp;&nbsp;&nbsp; Custom attribute cannot be applied to an assembly. &nbsp;&nbsp;&nbsp; [assembly: Tested(&quot;Sachin&quot;, testgrade.Good)] &nbsp;&nbsp;&nbsp; [Tested(&quot;Virat&quot;, testgrade.Excellent)] &nbsp;&nbsp;&nbsp; class customer { /* .... */ } &nbsp;&nbsp;&nbsp; Custom attribute cannot be applied to a method. &nbsp;&nbsp;&nbsp; Custom attribute cannot be applied to a class

  • 1 only
  • 1, 5
  • 2, 3
  • 4, 5
Show Answer Report

13. Attributes can be applied to &nbsp;&nbsp;&nbsp; Method &nbsp;&nbsp;&nbsp; Class &nbsp;&nbsp;&nbsp; Assembly &nbsp;&nbsp;&nbsp; Namespace &nbsp;&nbsp;&nbsp; Enum

  • 1 and 2 only
  • 1, 2 and 3
  • 4 and 5 only
  • All of the above
Show Answer Report

14. What is an attribute in C#?

  • A variable that stores metadata
  • A method modifier
  • Explanation: Attributes are used to add metadata or declarative information to program elements.
  • A runtime exception handler
Show Answer Report

15. Which namespace contains most of the predefined attributes in C#?

  • System.IO
  • System.Reflection
  • System.ComponentModel
  • Explanation: Most built-in attributes are defined in the System namespace.
Show Answer Report

16. Which symbol is used to apply an attribute in C#?

  • #
  • @
  • Explanation: Attributes are applied using square brackets [ ].
  • <>
Show Answer Report

17. Which attribute specifies how a custom attribute can be used?

  • UsageAttribute
  • Explanation: AttributeUsage defines valid targets, inheritance, and multiplicity of an attribute.
  • AttributeTarget
  • AttributeType
Show Answer Report

18. Which base class must be inherited to create a custom attribute?

  • Object
  • System.Custom
  • Explanation: All custom attributes must inherit from System.Attribute.
  • System.Metadata
Show Answer Report

19. Which target allows an attribute to be applied to a method?

  • AttributeTargets.Class
  • AttributeTargets.Property
  • Explanation: AttributeTargets.Method specifies that the attribute can be applied to methods.
  • AttributeTargets.Interface
Show Answer Report

20. Which attribute controls whether multiple instances of the same attribute are allowed?

  • Explanation: AllowMultiple determines whether an attribute can be applied more than once.
  • MultiUse
  • Inherited
  • AttributeUsage
Show Answer Report
Questions and Answers for Competitive Exams Various Entrance Test