Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Download: http://solutionzip.com/downloads/week-2-learning-team_answers/
- 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
- 2. 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;
- 3. 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 file that is referenced. How can you figure out which
- DLL files 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.
- 354 | CHAPTER 8 REFLECTION, CUSTOM ATTRIBUTES, THE CODEDOM, AND LAMBDA EXPRESSIONS
- 4. 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();
- 5. 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; } }
- 6. 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
- 7. 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
- 8. Which property of the Assembly class returns the name of the assembly?
- a. Name
- b. FullyQualifiedName
- c. Location
- d. FullName
- Chapter Test Questions | 355
- 9. Which method of the Assembly class returns an instance of the current assembly?
- a. GetExecutingAssembly
- b. GetAssembly
- c. GetCurrentAssembly
- d. Assembly
- 10. 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″);
- 11. 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
- 12. 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
- 13. 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”);
- 356 | CHAPTER 8 REFLECTION, CUSTOM ATTRIBUTES, THE CODEDOM, AND LAMBDA EXPRESSIONS
- 14. 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
- 15. 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.
- 16. 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
- 17. 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
- 18. 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)
- Additional Reading and Resources | 357
- 19. Which method of the MethodInfo class can be used to execute the method?
- a. Execute
- b. Invoke
- c. Call
- d. Load
- 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 });
- Download: http://solutionzip.com/downloads/week-2-learning-team_answers/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement