Chapter 8 Flashcards

1
Q
  1. 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
A

d. System.CodeDom

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. 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;
A

c. x => x * x;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. 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.
A

b. Create an instance of the Assembly class, load the assembly, and call the GetReferencedAssemblies method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. 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();
A

d. Type myType = myParameter.GetType();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. 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; }
    }
A
b.	class MyCustomAttribute : System.Attribute
    {
        public string Version { get; set; }
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. 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
A

b. Assembly

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. 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
A

c. GetExportedTypes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. Which property of the Assembly class returns the name of the assembly?
    a. Name
    b. FullyQualifiedName
    c. Location
    d. FullName
A

d. FullName

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. Which method of the Assembly class returns an instance of the current assembly?
    a. GetExecutingAssembly
    b. GetAssembly
    c. GetCurrentAssembly
    d. Assembly
A

a. GetExecutingAssembly

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. 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

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”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. 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
A

c. Load

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. 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
A

b. LoadFrom

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. 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

a. myAssembly.CreateInstance

(“System.Data.DataTable”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
  1. 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
A

d. Type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  1. 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

a. Create an instance of the Type class using the typeof keyword and then examine the IsPublic property of the Type variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
  1. 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
A

b. FieldInfo

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
  1. 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
A

d. GetArrayRank

18
Q
  1. 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

a. myClass.GetType().GetField
(“myPrivateField”, BindingFlags.NonPublic |
BindingFlags.Instance)

19
Q
  1. Which method of the MethodInfo class can be used to execute the method?
    a. Execute
    b. Invoke
    c. Call
    d. Load
A

b. Invoke

20
Q
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 });
A

c. myMethod.Invoke(myClass, new object[] { 4,

5 });

21
Q

E1. Which class name follows the standard naming convention for a custom attribute class?

a. AttributeMyCustom
b. MyCustomAttribute
c. MyCustom
d. MyCustom_Attribute

A

b. MyCustomAttribute

22
Q

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.

A

b. Add the following attribute above the custom attribute’s class declaration:

23
Q
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);
A

d. typeof(Person).GetCusomAttributes

(typeof(DataMappingAttribute), false);

24
Q

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

A

b. CodeCompileUnit

25
Q
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");
A
c.	CodeNamespace codeNamespace = 
new CodeNamespace("MyNamespace");
26
Q

E6. Which class in the CodeDOM is used to create a statement that declares a class?

a. CodeClassDeclaration
b. CodeCompileUnit
c. CodeTypeDeclaration
d. CodeClass

A

c. CodeTypeDeclaration

27
Q
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

a. xField.Type = new CodeTypeReference(typeof(double));

28
Q

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

A

b. HasSet

29
Q

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

A

d. CodeThisReferenceExpression

30
Q

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

A

b. TrueStatements

31
Q

E11. Which class in the CodeDOM is used to call a method?

a. CodeMethodInvoke
b. CodeMethodInvokeExpression
c. CodeMethodCall
d. CodeMethodCallExpression

A

b. CodeMethodInvokeExpression

32
Q

E12. Which class in the CodeDOM is used to generate a class file?

a. CodeDOMProvider
b. CodeDOMTextWriter
c. CodeDOMClassWriter
d. CodeDOMGenerator

A

a. CodeDOMProvider

33
Q

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”

A

c. BracingStyle = “C”

34
Q

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

A

b. x => x * x

(Answer B is an expression lamba; answer C is a statement lambda)

35
Q

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 )

A

b. ( x, y )

36
Q

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);

A

b. delegate int MyDelegate(int a, int b);

37
Q

E17. True or false? You can reference local variables that are not passed as parameters in an anonymous method.

a. True
b. False

A

a. True

38
Q

E18. Which method of the CodeDOMProvider generates a code file?

a. GenerateCode
b. GenerateCodeFileFromCompileUnit
c. GenerateFileFromCompileUnit
d. GenerateCodeFromCompileUnit

A

c. GenerateFileFromCompileUnit

39
Q

E19. Which class in the CodeDOM is used to create a method declaration statement?

a. MethodDeclaration
b. CodeMethodDeclaration
c. CodeMemberMethod
d. CodeMemberDeclaration

A

c. CodeMemberMethod

40
Q

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

A

b. The goes to operator