Question 1Medium
Choose the method defined by MemberInfo:
GetCustomAttributes()IsDefined()GetCustomeAttributesData()- All of the mentioned
Answer: D. All of the mentioned
Explanation
MemberInfo includes two abstract methods: Get CustomAttributes( ) and Is Defined( ). These both relate to attributes. The first obtains a list of the custom attributes associated with the invoking object.
The second determines if an attribute is defined for the invoking object. The .NET Framework Version 4.0 adds a method called Get CustomAttributesData(), which returns information about custom attributes
Step-by-step solution
- MemberInfo declares GetCustomAttributes, which lists the custom attributes applied to a member.
- MemberInfo declares IsDefined, which checks whether a given attribute is applied.
- From .NET 4.0 it also declares GetCustomAttributesData; the option spelled GetCustomeAttributesData is the same method.
- So all three, the correct answer 3, is correct.
Why the other options are wrong
- A. GetCustomAttributes is on MemberInfo, but it is incomplete alone.
- B. IsDefined is on MemberInfo, but it is incomplete alone.
- C. GetCustomAttributesData is on MemberInfo, but it is incomplete alone.
Tip: An All of the mentioned answer is right only when every listed item is true.
Question 2Medium
What does the following property signifies? MemberTypes MemberType
- Helps in distinguishing kind of members
- Property helps in determining if member is a field, method, property or event
- Both a & b
- None of the mentioned
Answer: C. Both a & b
Explanation
This property obtains the kind of the member. This value indicates if the member is a field, method, property, event, or constructor among others.
Step-by-step solution
- MemberType (of type MemberTypes) tells you which kind of member an item is: field, method, property or event.
- Both statements in the options are true: it helps distinguish the kind of member, and it tells whether the member is a field, method, property or event.
- So 'Both a & b' is correct.
Why the other options are wrong
- A. Option a is true, but it leaves out option b.
- B. Option b is true, but it leaves out option a.
- D. Both statements are true, so 'None of the mentioned' is wrong.
Tip: MemberType is a property of the MemberInfo class in .NET reflection.
Question 3Medium
Choose the keyword which declares the indexer?
- base
- this
- super
- extract
Answer: B. this
Explanation
The indexer is declared using the keyword 'this'.
Step-by-step solution
- An indexer is declared with the keyword this.
- Base refers to the base class, super is not a C# keyword, and extract is not a keyword.
- So the correct answer, this, is correct.
Why the other options are wrong
- A. Base refers to the base class, not to an indexer declaration.
- C. C# does not use super for indexers.
- D. Extract is not a C# keyword for indexers.
Tip: Indexer syntax is a type followed by this[index] with get and set.
Question 4Medium
Choose the correct statement among the following?
- A property can be a static member whereas an indexer can is always an instance member
- A get accessor of a property corresponds to a method with no parameters whereas get accessor of an indexer corresponds to a method with same formal parameters lists as the indexer
- It is an error for an indexer to declare a local variable with same name as indexer parameters
- All of the mentioned
Answer: D. All of the mentioned
Explanation
An indexer allows an object to be indexed like an array. When you define an indexer for a class, this class behaves like a virtual array. You can then access the instance of this class using the array accessoperator ([ ]). Declaration of behavior of an indexer is to some extent similar to a property. Like properties, you use get and set accessors fordefining an indexer. However, properties return or set a specificdata member, whereas indexers returns or sets a particular value from theobject instance. In other words, it breaks the instance data into smallerparts and indexes each part, gets or sets each part. Indexers can beoverloaded. Indexers can also be declared with multiple parameters and each parameter may be a different type. It is not necessary that theindexes have to be integers. C# allows indexes to be of other types, for example, a string.
Step-by-step solution
- Option A is true: a property can be static, but an indexer is always an instance member.
- Option B is true: a property's get has no parameters, while an indexer's get has the indexer's parameter list.
- Option C is true: a local variable with the same name as an indexer parameter is an error.
- All three statements are correct, so the answer is option D (All of the mentioned).
Why the other options are wrong
- A. Options B and C are also correct, so A alone is incomplete.
- B. Options A and C are also correct, so B alone is incomplete.
- C. Options A and B are also correct, so C alone is incomplete.
Tip: Indexers use get and set like properties, but take parameters and cannot be static.
Question 5Medium
Which feature enables to obtain information about use and capabilities of types at runtime?
- Runtime type ID
- Reflection
- Attributes
- None of the mentioned
Answer: B. Reflection
Explanation
Reflection is the feature that enables you to obtain information about a type. The term reflection comes from the way the process works: A Type object mirrors the underlying type that it represents.
Reflection is a powerful mechanism because it allows us to learn and use the capabilities of types that are known only at runtime.
Step-by-step solution
- Reflection provides information about types during runtime.
- It allows inspecting and using type capabilities at runtime.
- This is the core feature for runtime type inspection.
- The correct answer is Reflection.
- Final Answer: B (Reflection).
Why the other options are wrong
- A. Runtime type ID identifies types but does not provide detailed use or capabilities.
- C. Attributes describe metadata but do not enable runtime inspection of type capabilities.
- D. Reflection is a valid feature, so 'None of the mentioned' is incorrect.
Tip: Use reflection for dynamic type analysis in code.