C# - Attributes Questions and Answers
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
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") >
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? Custom attribute cannot be applied to an assembly. [assembly: Tested("Sachin", testgrade.Good)] [Tested("Virat", testgrade.Excellent)] class customer { /* .... */ } Custom attribute cannot be applied to a method. Custom attribute cannot be applied to a class
- 1 only
- 1, 5
- 2, 3
- 4, 5
13. Attributes can be applied to Method Class Assembly Namespace Enum
- 1 and 2 only
- 1, 2 and 3
- 4 and 5 only
- All of the above
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
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.
16. Which symbol is used to apply an attribute in C#?
- #
- @
- Explanation: Attributes are applied using square brackets [ ].
- <>
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
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
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
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