Chapter 8 Flashcards
- You are given an assignment to create a code generator to automate the task of creating repetitive code. Which namespace contains the types needed to generate code?
a. System.Reflection
b. CodeDom
c. Reflection
d. System.CodeDom
d. System.CodeDom
- Which code can create a lambda expression?
a. delegate x = x => 5 + 5;
b. delegate MySub(double x);
MySub ms = delegate(double y) {
y * y;
}
c. x => x * x;
d. delegate MySub();
MySub ms = x * x;
c. x => x * x;
- You are consulting for a company called Contoso and are taking over an application that was built by a third-party software company. There is an executable that is currently not working because it is missing a DLL that is referenced. How can you figure out which DLLs the application references?
a. Create an instance of the Assembly class, load the assembly, and iterate through the ReferencedAssemblies property.
b. Create an instance of the Assembly class, load the assembly, and call the GetReferencedAssemblies method.
c. Create an instance of the Assembly class, load the assembly, and iterate through the Modules property.
d. Create an instance of the Assembly class, load the assembly, and call the GetModulesReferenced method.
b. Create an instance of the Assembly class, load the assembly, and call the GetReferencedAssemblies method.
- You are a developer for a finance department and are building a method that uses reflection to get a reference to the type of object that was passed as a parameter. Which syntax can be used to determine an object’s type?
a. Type myType = typeof(myParameter);
b. Object myObject =
myParameter.GetType(object);
c. Object myObject = typeof(myParameter);
d. Type myType = myParameter.GetType();
d. Type myType = myParameter.GetType();
- You are asked to create a custom attribute that has a single property, called Version, that allows the caller to determine the version of a method. Which code can create the attribute?
a. class MyCustomAttribute :
System.Reflection.Attribute
{
public string Version { get; set; }
}
b. class MyCustomAttribute : System.Attribute
{
public string Version { get; set; }
}
c. class MyCustomAttribute :
System.AttributeUsage
{
public string Version { get; set; }
}
d. class MyCustomAttribute :
System.ClassAttribute
{
public string Version { get; set; }
}
b. class MyCustomAttribute : System.Attribute { public string Version { get; set; } }
- Which class in the System.Reflection namespace would you use if you want to determine all the classes contained in a DLL file?
a. FileInfo
b. Assembly
c. Type
d. Module
b. Assembly
- Which method of the Assembly class allows you to get all the public types defined in the Assembly?
a. DefinedTypes
b. Types
c. GetExportedTypes
d. GetModules
c. GetExportedTypes
- Which property of the Assembly class returns the name of the assembly?
a. Name
b. FullyQualifiedName
c. Location
d. FullName
d. FullName
- Which method of the Assembly class returns an instance of the current assembly?
a. GetExecutingAssembly
b. GetAssembly
c. GetCurrentAssembly
d. Assembly
a. GetExecutingAssembly
- Which syntax will Load an Assembly? (Choose all that apply)
a. Assembly.Load(“System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”);
b. Assembly.LoadFrom(@”c:\MyProject\
Project1.dll”);
c. Assembly.LoadFile(@”c:\MyProject\
Project1.dll”);
d. Assembly.ReflectionOnlyLoad((“System.Data,
Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089”);
a. Assembly.Load(“System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”);
b. Assembly.LoadFrom(@”c:\MyProject\
Project1.dll”);
c. Assembly.LoadFile(@”c:\MyProject\
Project1.dll”);
d. Assembly.ReflectionOnlyLoad((“System.Data,
Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089”);
- Which method should you call if you want the .NET Framework to look in the load-context to load an Assembly?
a. ReflectionOnlyLoad
b. LoadFrom
c. Load
d. LoadFromContext
c. Load
- Which method should you call if you want the .NET Framework to look in the load-from context?
a. ReflectionOnlyLoad
b. LoadFrom
c. Load
d. LoadFromContext
b. LoadFrom
- Which line creates an instance of a DataTable using reflection?
a. myAssembly.CreateInstance
(“System.Data.DataTable”);
b. DataTable.GetType();
c. typeof(“System.Data.DataTable”);
d. myType.CreateInstance
(“System.Data.DataTable”);
a. myAssembly.CreateInstance
(“System.Data.DataTable”);
- Which class would you create if you wanted to determine all the properties contained in a class using reflection?
a. Assembly
b. Class
c. Property
d. Type
d. Type
- How can you determine if a class is public or private?
a. Create an instance of the Type class using the typeof keyword and then examine the IsPublic property of the Type variable.
b. Create an instance of the Type class using the typeof keyword and then examine the IsPrivate property of the Type variable.
c. Create an instance of the class within a try catch block and catch the exception if you cannot create an instance of the class.
d. Create an instance of the class and check the IsPublic property.
a. Create an instance of the Type class using the typeof keyword and then examine the IsPublic property of the Type variable.
- Which class in the System.Reflection namespace is used to represent a field defined in a class?
a. PropertyInfo
b. FieldInfo
c. Type
d. EventInfo
b. FieldInfo
- Which property of the Type class can you use to determine the number of dimension for an array?
a. GetDimension
b. GetRank
c. GetDimensions
d. GetArrayRank
d. GetArrayRank
- Which statement will returns a private, instance field called “myPrivateField” using reflection? Assume the myClass variable is an instance of a class.
a. myClass.GetType().GetField
(“myPrivateField”, BindingFlags.NonPublic |
BindingFlags.Instance)
b. myClass.myPrivateField
c. myClass.GetType().GetField
(“myPrivateField”)
d. myClass. GetType().GetField(“myPrivateField”,
BindingFlags.NonPublic &
BindingFlags.Instance)
a. myClass.GetType().GetField
(“myPrivateField”, BindingFlags.NonPublic |
BindingFlags.Instance)
- Which method of the MethodInfo class can be used to execute the method?
a. Execute
b. Invoke
c. Call
d. Load
b. Invoke
20. Which statement uses reflection to execute the method and passes in two parameters given the following code block? MyClass myClass = new MyClass(); MethodInfo myMethod = typeof(MyClass).GetMethod(“Multiply”); a. myMethod.Execute(myClass, new object[] { 4, 5 }); b. myMethod.Execute(MyClass, new object[] { 4, 5 }); c. myMethod.Invoke(myClass, new object[] { 4, 5 }); d. myMethod.Invoke(MyClass, new object[] { 4, 5 });
c. myMethod.Invoke(myClass, new object[] { 4,
5 });
E1. Which class name follows the standard naming convention for a custom attribute class?
a. AttributeMyCustom
b. MyCustomAttribute
c. MyCustom
d. MyCustom_Attribute
b. MyCustomAttribute
E2. How can you limit the scope of a custom attribute that you are creating so that it can be used only by a class?
a. Set the custom attribute’s TargetLevel property to Class.
b. Add the following attribute above the custom attribute’s class declaration:
[System.AttributeUsage(AttributeTargets.Class)]
c. Set the custom attribute’s TargetLevel attribute to Class.
d. Set the custom attribute’s Scope property to Class.
b. Add the following attribute above the custom attribute’s class declaration:
E3. If you have created a custom attribute class called DataMappingAttribute and have used it in a class called Person, which statement will return all the DataMappingAttributes that have been applied to the Person class? a. typeof(DataMappingAttribute). GetCusomAttributes(typeof(Person), false); b. typeof(Person).GetAttributes(typeof (DataMappingAttribute), false); c. typeof(DataMappingAttribute). GetAttributes(typeof(Person), false); d. typeof(Person).GetCusomAttributes (typeof(DataMappingAttribute), false);
d. typeof(Person).GetCusomAttributes
(typeof(DataMappingAttribute), false);
E4. Which class in the CodeDOM is the top-level class that is the container for all other objects within the class?
a. CodeNamespace
b. CodeCompileUnit
c. CodeConstructor
d. CodeDOMProvider
b. CodeCompileUnit
E5. Which statement generates a namespace statement using the CodeDOM? a. Namespace namespace = new Namespace("MyNamespace"); b. string namespace = Namespace.Create("MyNamespace"); c. CodeNamespace codeNamespace = new CodeNamespace("MyNamespace"); d. string namespace = CodeNamespace.Create("MyNamespace");
c. CodeNamespace codeNamespace = new CodeNamespace("MyNamespace");
E6. Which class in the CodeDOM is used to create a statement that declares a class?
a. CodeClassDeclaration
b. CodeCompileUnit
c. CodeTypeDeclaration
d. CodeClass
c. CodeTypeDeclaration
E7. Which statement should be added to the following block of code to set a field’s type to a double using the CodeDOM? CodeMemberField xField = new CodeMemberField(); xField.Name = "x"; a. xField.Type = new CodeTypeReference(typeof(double)); b. xField.Type = double; c. xField.Type = typeof(double)); d. xField.Type = new double();
a. xField.Type = new CodeTypeReference(typeof(double));
E8. Which property of the CodeMemberProperty class should be set to true to tell the compiler to generate a set accessor using the CodeDOM?
a. Set
b. HasSet
c. CreateSet
d. GenerateSet
b. HasSet
E9. Which class in the CodeDOM is used to represent either the this keyword in C# or the Me keyword in VB.NET?
a. CodeMeReference
b. CodeThisReference
c. CodeMeReferenceExpression
d. CodeThisReferenceExpression
d. CodeThisReferenceExpression
E10. Which property of the CodeConditionStatement is used to add statements to the true path of logic using the CodeDOM?
a. TrueConditions
b. TrueStatements
c. TrueCodeStatements
d. TrueExpressions
b. TrueStatements
E11. Which class in the CodeDOM is used to call a method?
a. CodeMethodInvoke
b. CodeMethodInvokeExpression
c. CodeMethodCall
d. CodeMethodCallExpression
b. CodeMethodInvokeExpression
E12. Which class in the CodeDOM is used to generate a class file?
a. CodeDOMProvider
b. CodeDOMTextWriter
c. CodeDOMClassWriter
d. CodeDOMGenerator
a. CodeDOMProvider
E13. You are looking at the output of a file generated using the CodeDOM. The file is in C# and you notice that the brackets for all your methods are not on separate lines. Which property of the CodeGeneratorOptions class can you use to force the brackets to be on separate lines, and what should the property be set to?
a. BracketStyle = BracketStyle.Separate
b. BracingStyle = BracingStyle.Separate
c. BracingStyle = “C”
d. BracketStyle = “C”
c. BracingStyle = “C”
E14. Which statement is a valid expression lambda?
a. x => x * y
b. x => x * x
c. x => { int y; y = x * x; }
d. x => y
b. x => x * x
(Answer B is an expression lamba; answer C is a statement lambda)
E15. Which statement should be on the left side of a goes to operator if you want to pass two parameters to a lambda expression?
a. { x, y }
b. ( x, y )
c. { x => y }
d. ( x => y )
b. ( x, y )
E16. What is the correct delegate declaration for the following method?
void int Multiply(int x, int y)
a. int MyDelegate(int a, int b) as delegate;
b. delegate int MyDelegate(int a, int b);
c. int Multiply(int x, int y);
d. delegate void MyDelegate(void a, void b);
b. delegate int MyDelegate(int a, int b);
E17. True or false? You can reference local variables that are not passed as parameters in an anonymous method.
a. True
b. False
a. True
E18. Which method of the CodeDOMProvider generates a code file?
a. GenerateCode
b. GenerateCodeFileFromCompileUnit
c. GenerateFileFromCompileUnit
d. GenerateCodeFromCompileUnit
c. GenerateFileFromCompileUnit
E19. Which class in the CodeDOM is used to create a method declaration statement?
a. MethodDeclaration
b. CodeMethodDeclaration
c. CodeMemberMethod
d. CodeMemberDeclaration
c. CodeMemberMethod
E20. Which is the => syntax referred to as in a lambda expression?
a. The parameter operator
b. The goes to operator
c. The lambda operator
d. The expression operator
b. The goes to operator