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

21. Which attribute indicates that a class should not be inherited?

  • NonInheritable
  • Final
  • Explanation: sealed prevents a class from being inherited, though it is not an attribute.
  • Obsolete
Show Answer Report

22. Which attribute marks a program element as outdated?

  • Deprecated
  • Old
  • Explanation: Obsolete warns that a member should no longer be used.
  • Legacy
Show Answer Report

23. What happens when an Obsolete attribute is marked as error = true?

  • Warning is ignored
  • Program fails at runtime
  • Explanation: Setting error = true causes a compilation error when used.
  • Attribute is skipped
Show Answer Report

24. Which attribute hides a public member from IntelliSense?

  • Hidden
  • Explanation: EditorBrowsable controls visibility in IntelliSense.
  • NonBrowsable
  • Ignore
Show Answer Report

25. Which attribute specifies a method that should run before a test?

  • PreTest
  • TestInit
  • Explanation: TestInitialize is used in unit testing frameworks.
  • Setup
Show Answer Report

26. Which reflection class is commonly used to read attributes at runtime?

  • AttributeReader
  • Explanation: The Type class is used with reflection to retrieve attributes.
  • AssemblyInfo
  • MetaData
Show Answer Report

27. Which method retrieves attributes applied to a program element?

  • GetAttribute()
  • ReadAttributes()
  • Explanation: GetCustomAttributes() is used to obtain attribute instances.
  • FetchAttributes()
Show Answer Report

28. Which attribute indicates a method does not return a value?

  • Void
  • NoReturn
  • Explanation: DoesNotReturn informs the compiler and analyzers.
  • Returnless
Show Answer Report

29. Which attribute is applied at the assembly level?

  • Explanation: AssemblyTitle provides metadata about the assembly.
  • AssemblyUsage
  • AssemblyInfo
  • AssemblyScope
Show Answer Report

30. Which attribute specifies the version of an assembly?

  • AssemblyBuild
  • AssemblyInfo
  • Explanation: AssemblyVersion defines the assembly version.
  • AssemblyLevel
Show Answer Report
Questions and Answers for Competitive Exams Various Entrance Test