Professional Documents
Culture Documents
2. You need to expose some data from a class. The data can be
read by other types but can be changed only by derived
types. What should you use?
A. A protected field
B. A public property with a protected set modifier
C. A protected property
D. A protected property with a public get modifier
Answer:
1. Correct answer: D
A. Incorrect: A public class with public members can be accessed
from other assemblies without any restrictions.
B. Incorrect: Types in other assemblies can derive from the
class and access the protected methods.
C. Incorrect: Types in other assemblies cannot derive from the
class, but other types in the same assembly can access the
method.
D. Correct: An internal class cannot be accessed outside of its
assembly. The protected methods can be accessed only by
derived types.
2. Correct answer: B
A. Incorrect: A protected field cannot be read by other types
outside of the class.
B. Correct: A public property can be read by all other types.
The protected set modifier restricts changes to derived
types.
C. Incorrect: A protected property cannot be read by other
types outside of the class.
D. Incorrect: This will generate a compile error because the
accessibility modifier of the get accessor must be more
restrictive than the property. Public is less restrictive
than protected.
3. Correct answer: A
A. Correct: Implementing IA implicitly will make this the
default implementation. When dealing with a reference to the
class, this method will be invoked. Implementing IB
explicitly will invoke the implementation for IB when
dealing with a reference to the IB interface.
B. Incorrect: When both IA and IB are implemented explicitly,
you need to cast a reference to the class to one or both
interface types to invoke the method.
C. Incorrect: Implementing both IA and IB implicitly wonft
allow for a different implementation for IB.
D. Incorrect: Implementing IB implicitly makes IB the default
implementation instead of IA.