C# - Attributes Questions and Answers

Practice Mode
Showing 10 of 38 questions
Q11
Which of the following is the correct way to apply an attribute to an Assembly?
  • A [ AssemblyDescription("DCube Component Library") ]
  • B [ assembly : AssemblyDescription("DCube Component Library") ]
  • C [ Assemblylnfo : AssemblyDescription("DCube Component Library") ]
  • D < Assembly: AssemblyDescription("DCube Component Library") >
Answer: Option B
Q12
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
  • A 1 only
  • B 1, 5
  • C 2, 3
  • D 4, 5
Answer: Option C
Q13
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
  • A 1 and 2 only
  • B 1, 2 and 3
  • C 4 and 5 only
  • D All of the above
Answer: Option B
Q14
What is an attribute in C#?
  • A A variable that stores metadata
  • B A method modifier
  • C Explanation: Attributes are used to add metadata or declarative information to program elements.
  • D A runtime exception handler
Answer: Option C
Explanation: Attributes are used to add metadata or declarative information to program elements.
Q15
Which namespace contains most of the predefined attributes in C#?
  • A System.IO
  • B System.Reflection
  • C System.ComponentModel
  • D Explanation: Most built-in attributes are defined in the System namespace.
Answer: Option D
Explanation: Most built-in attributes are defined in the System namespace.
Q16
Which symbol is used to apply an attribute in C#?
  • A #
  • B @
  • C Explanation: Attributes are applied using square brackets [ ].
  • D <>
Answer: Option C
Explanation: Attributes are applied using square brackets [ ].
Q17
Which attribute specifies how a custom attribute can be used?
  • A UsageAttribute
  • B Explanation: AttributeUsage defines valid targets, inheritance, and multiplicity of an attribute.
  • C AttributeTarget
  • D AttributeType
Answer: Option B
Explanation: AttributeUsage defines valid targets, inheritance, and multiplicity of an attribute.
Q18
Which base class must be inherited to create a custom attribute?
  • A Object
  • B System.Custom
  • C Explanation: All custom attributes must inherit from System.Attribute.
  • D System.Metadata
Answer: Option C
Explanation: All custom attributes must inherit from System.Attribute.
Q19
Which target allows an attribute to be applied to a method?
  • A AttributeTargets.Class
  • B AttributeTargets.Property
  • C Explanation: AttributeTargets.Method specifies that the attribute can be applied to methods.
  • D AttributeTargets.Interface
Answer: Option C
Explanation: AttributeTargets.Method specifies that the attribute can be applied to methods.
Q20
Which attribute controls whether multiple instances of the same attribute are allowed?
  • A Explanation: AllowMultiple determines whether an attribute can be applied more than once.
  • B MultiUse
  • C Inherited
  • D AttributeUsage
Answer: Option A
Explanation: AllowMultiple determines whether an attribute can be applied more than once.
Questions and Answers for Competitive Exams Various Entrance Test