Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 90 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
90
Dung lượng
485,07 KB
Nội dung
706 Answers 2. Correct Answer: C A. Incorrect: You should order catch clauses from most specific to most gen- eral. B. Incorrect: The first type that matches is caught and subsequent catch clauses are skipped. C. Correct: The first type that matches is caught, and subsequent catch clauses are skipped. Therefore, you should order catch clauses from most specific to most general to enable you to catch errors that you have specific error-handling for, while still catching other exceptions with the more gen- eral catch clauses. D. Incorrect: The first type that matches is caught and subsequent catch clauses are skipped. 3. Correct Answer: A A. Correct: Using the String type to construct a dynamic string can result in a lot of temporary strings in memory because the String type is immutable. Therefore, using the StringBuilder class is preferable. B. Incorrect: Strings are limited to 32,767 bytes, not 256 bytes. C. Incorrect: You can search and replace with a standard String class. D. Incorrect: Strings are never value types; they are reference types. 4. Correct Answer: B A. Incorrect: Although this statement is true, the real advantage of using a finally block is that code is executed even if the runtime does not throw an exception. Code in a catch block is executed only if an exception occurs. B. Correct: Use finally blocks for code that should run whether or not an exception occurs. C. Incorrect: The compiler will not throw an error if you do not include a finally block. finally blocks are optional. D. Incorrect: You can dispose of resources in a catch block. However, the code will run only if an exception occurs. Typically, you need to dispose of resources whether or not an exception occurs. 5. Correct Answer: B A. Incorrect: The Exception.Message property provides a description of the exception, but it does not specify the line of code that caused the exception. Answers 707 B. Correct: The Exception.StackTrace property provides a full dump of the call stack at the time the exception was thrown, including the file and line of code that caused the exception. C. Incorrect: The Exception.Source property lists the library that the exception occurred in. However, this is rarely useful for troubleshooting. D. Incorrect: The Exception.Data property is a collection of key/value pairs with user-defined information. It does not include the line of code that threw the exception. 6. Correct Answer: B A. Incorrect: First, value types must be initialized before being passed to a procedure unless they are specifically declared as nullable. Second, passing a null value would not affect the behavior of a value type. B. Correct: Procedures work with a copy of variables when you pass a value type by default. Therefore, any modifications that were made to the copy would not affect the original value. C. Incorrect: The variable might have been redeclared, but it would not affect the value of the variable. D. Incorrect: If the variable had been passed by reference, the original value would have been modified. Lesson 3 1. Correct Answers: B and C A. Incorrect: Interfaces define a contract between types; inheritance derives a type from a base type. B. Correct: Interfaces define a contract between types, ensuring that a class implements specific members. C. Correct: Inheritance derives a type from a base type, automatically imple- menting all members of the base class, while allowing the derived class to extend or override the existing functionality. D. Incorrect: Interfaces define a contract between types; inheritance derives a type from a base type. 2. Correct Answers: A and C A. Correct: Nullable is a generic type. B. Incorrect: Boolean is a nongeneric value type. 708 Answers C. Correct: EventHandler is a generic type. D. Incorrect: System.Drawing.Point is a structure and is not generic. 3. Correct Answer: D A. Incorrect: The object class does not have a Dispose member method. Addi- tionally, you would need to use a constraint to mandate types implement- ing the IDisposable interface to call the Dispose method. B. Incorrect: Implementing an interface does not enable generic types to use interface methods. C. Incorrect: Deriving the generic class from an interface does not enable generic types to use interface methods. D. Correct: If you use constraints to require types to implement a specific interface, you can call any methods used in the interface. 4. Correct Answer: A A. Correct: Delegates define the signature (arguments and return type) for the entry point. B. Incorrect: Event procedures can be Shared/static or instance members. C. Incorrect: If you mistyped the event procedure name, you would receive a different compiler error. D. Incorrect: Events work equally well, regardless of the language used. 5. Correct Answer: D A. Incorrect: IEquatable allows two instances of a class to be compared for equality but not sorted. B. Incorrect: IFormattable enables you to convert the value of an object into a specially formatted string. C. Incorrect: IDisposable defines a method to release allocated resources and is not related to sorting objects. D. Correct: IComparable requires the CompareTo method, which is necessary for instances of a class to be sorted. Lesson 4 1. Correct Answer: A A. Correct: The primary reason to avoid boxing is because it adds overhead. Answers 709 B. Incorrect: Boxing requires no special privileges. C. Incorrect: Boxing does not make code less readable. 2. Correct Answers: A and B A. Correct: Value types are boxed when an abstract method inherited from System.Object is called. Overriding the method avoids boxing. B. Correct: By default, the ToString method simply returns the type name, which is typically not useful for a consuming application. C. Incorrect: The compiler does not require structures to override the ToString method. D. Incorrect: ToString never causes a run-time error; it simply returns the type name unless overridden. 3. Correct Answer: B A. Incorrect: You can’t omit a member of an interface and still conform to that interface. B. Correct: InvalidCastException is the recommended exception to throw. C. Incorrect: While you could throw a custom exception, using standard exception types makes it easier for developers writing code to consume your type to catch specific exceptions. D. Incorrect: You must return a value for each conversion member. 4. Correct Answers: A and C A. Correct: You can convert from Int16 to Int32 because that is considered a widening conversion. Because Int32 can store any value of Int16, implicit conversion is allowed. B. Incorrect: You cannot convert from Int32 to Int16 because that is consid- ered a narrowing conversion. Because Int16 cannot store any value of Int32, implicit conversion is not allowed. C. Correct: You can convert from Int16 to double because that is considered a widening conversion. Because double can store any value of Int16, implicit conversion is allowed. D. Incorrect: You cannot convert from Double to Int16 because that is consid- ered a narrowing conversion. Because Int16 cannot store any value of Double, implicit conversion is not allowed. 710 Answers Chapter 1: Case Scenario Answers Case Scenario: Designing an Application 1. Both subscribers and doctors have a lot of information in common, including names, phone numbers, and addresses. However, subscribers and doctors have unique categories of information as well. For example, you need to track the sub- scription plan and payment information for subscribers. For doctors, you need to track contract details, medical certifications, and specialties. Therefore, you should create separate classes for subscribers and doctors but derive them from a single class that contains all members shared by both classes. To do this, you could create a base class named Person and derive both the Subscriber and Doctor classes from Person. 2. You can use an array to store a group of subscribers. 3. Yes, you can use generics to create a method that can accept both subscribers and doctors. To access the information in the base class that both classes share, you need to make the base class a constraint for the generic method. 4. The security error would generate an exception. Therefore, to respond to the exception, you should wrap the code in a try/catch block. Within the catch block, you should inform users that they should contact their manager. Chapter 2: Lesson Review Answers Lesson 1 1. Correct Answer: D A. Incorrect: The FileInfo class provides information about individual files and cannot retrieve a directory listing. B. Incorrect: Use the DriveInfo class to retrieve a list of disks connected to the computer. It cannot be used to retrieve a directory listing. C. Incorrect: You can use the FileSystemWatcher class to trigger events when files are added or updated. You cannot use it to retrieve a directory listing, however. D. Correct: You can create a DirectoryInfo instance by providing the path of the parent directory and then call DirectoryInfo.GetDirectories to retrieve a list of sub-directories. Answers 711 2. Correct Answer: B A. Incorrect: The FileSystemWatcher class can detect changes to files. B. Correct: You cannot use FileSystemWatcher to detect new drives that are connected to the computer. You would need to query manually by calling the DriveInfo.GetDrives method. C. Incorrect: The FileSystemWatcher class can detect new directories. D. Incorrect: The FileSystemWatcher class can detect renamed files. 3. Correct Answers: B and D A. Incorrect: The File type is static; you cannot create an instance of it. B. Correct: The easiest way to copy a file is to call the static File.Copy method. C. Incorrect: This code sample calls the FileInfo.CreateText method, which creates a new file that overwrites the existing File1.txt file. D. Correct: This code sample creates a FileInfo object that represents the exist- ing File1.txt file and then copies it to File2.txt. Lesson 2 1. Correct Answer: A A. Correct: When you write to a MemoryStream, the data is stored in memory instead of being stored to the file system. You can then call Memory- Stream.WriteTo to store the data on the file system permanently. B. Incorrect: The BufferedStream class is useful for configuring how custom stream classes buffer data. However, you typically do not need to use it when working with standard stream classes. C. Incorrect: The GZipStream class compresses and decompresses data writ- ten to streams. You cannot use it alone to store streamed data temporarily in memory. D. Incorrect: While you would need to use the FileStream class to store data permanently on the file system, you should use MemoryStream to store the data temporarily. 2. Correct Answers: B and C A. Incorrect: You should use GZipStream only if you need to read or write a compressed file. While you can compress text, it would cease to be a stan- dard text file if the data were compressed. 712 Answers B. Correct: The TextReader class is ideal for processing text files. C. Correct: StreamReader derives from TextReader and can also be used to read text files. D. Incorrect: Use BinaryReader for processing binary data on a byte-by-byte basis. You cannot use BinaryReader to process data as strings without per- forming additional conversion. 3. Correct Answer: A A. Correct: The GetUserStoreForAssembly method returns an instance of Isolat- edStorageFile that is private to the user and assembly. B. Incorrect: The GetMachineStoreForAssembly method returns an instance of IsolatedStorageFile that is private to the assembly but could be accessed by other users running the same assembly. C. Incorrect: The GetUserStoreForDomain method returns an instance of Iso- latedStorageFile that is private to the user but could be accessed by other assemblies running in the same application domain. D. Incorrect: The GetMachineStoreForDomain method returns an instance of IsolatedStorageFile that could be accessed by other users or other assem- blies running in the same application domain. Chapter 2: Case Scenario Answers Case Scenario 1: Creating a Log File 1. The TextWriter class is ideal for generating text files, including log files. 2. You could use the static File.Copy method. Alternatively, you could create an instance of the FileIO class and call the CopyTo method. 3. No. In this scenario, members of the accounting department need to have direct access to the files. You cannot access files directly in isolated storage from other processes. Case Scenario 2: Compressing Files 1. Instead of writing directly to the BinaryWriter class, you could use the GZip- Stream class. GZipStream can compress or decompress data automatically, reduc- ing the storage requirements. Answers 713 2. Yes, if the application uses the same compression algorithm to access the files. 3. Yes, there’s nothing to prevent you from using GZipStream with isolated storage. Chapter 3: Lesson Review Answers Lesson 1 1. Correct Answer: C A. Incorrect: This code sample would work correctly; however, it performs a case-sensitive replacement. Therefore, it would not replace “HTTP://” correctly. B. Incorrect: This code sample has the parameters reversed and would replace “https://” with “http://”. C. Correct: This code sample correctly replaces “http://” with “https://” regardless of case. D. Incorrect: This code sample has the parameters reversed and would replace “https://” with “http://”. 2. Correct Answer: A A. Correct: This code sample correctly specifies the RegexOptions.Multiline option and does not use angle brackets to name regular expression groups. B. Incorrect: You must specify the RegexOptions.Multiline option to process multiline input. C. Incorrect: When naming a group, you should not include the angle brackets. D. Incorrect: When naming a group, you should not include the angle brack- ets. In addition, you must specify the RegexOptions.Multiline option to pro- cess multiline input. 3. Correct Answer: B A. Incorrect: This regular expression would match “zoot”, but it does not match “zot”. B. Correct: This regular expression matches both strings. C. Incorrect: This regular expression does not match either string because it begins with the “$” symbol, which matches the end of the string. D. Incorrect: This regular expression does match “zot”, but it does not match “zoot”. 714 Answers 4. Correct Answers: A, C, and E A. Correct: This string matches the regular expression. B. Incorrect: This string does not match the regular expression because the fourth character does not match. C. Correct: This string matches the regular expression. D. Incorrect: This string does not match the regular expression because the second and third characters must be “mo”. E. Correct: This string matches the regular expression. Lesson 2 1. Correct Answer: A A. Correct: UTF-32 has the largest byte size and yields the largest file size. B. Incorrect: UTF-16 has a smaller byte size than UTF-32. C. Incorrect: UTF-8 has a smaller byte size than UTF-32. D. Incorrect: ASCII has a smaller byte size than UTF-32. 2. Correct Answers: A, B, and C A. Correct: UTF-32 provides large enough bytes to support Chinese Unicode characters. B. Correct: UTF-16 provides large enough bytes to support Chinese Unicode characters. C. Correct: UTF-8 provides large enough bytes to support Chinese Unicode characters. D. Incorrect: ASCII supports only English-language characters. 3. Correct Answers: C and D A. Incorrect: ASCII uses 8-bit bytes, whereas UTF-32 uses larger bytes. B. Incorrect: ASCII uses 8-bit bytes, whereas UTF-16 uses larger bytes. C. Correct: UTF-8 uses 8-bit bytes for characters in the ASCII range and is backward-compatible with ASCII. D. Correct: UTF-7 correctly decodes ASCII files. Answers 715 4. Correct Answer: D A. Incorrect: iso-2022-kr provides support for Korean characters. However, Unicode also provides support for the Korean language, as well as other languages in the same file, and it provides the widest-ranging compatibility. B. Incorrect: x-EBCDIC-KoreanExtended provides support for Korean char- acters. However, Unicode also provides support for the Korean language, as well as other languages in the same file, and it provides the widest-ranging compatibility. C. Incorrect: x-mac-korean provides support for Korean characters. However, Unicode also provides support for the Korean language, as well as other languages in the same file, and it provides the widest-ranging compatibility. D. Correct: Though you could use one of several different Korean code pages, Unicode provides support for the Korean language and is the best choice for creating new documents. Chapter 3: Case Scenario Answers Case Scenario 1: Validating Input 1. You can use separate ASP.NET RegularExpressionValidator controls to restrict the input for each of the three boxes. For the company name validator, set the Vali- dationExpression property to [a-zA-Z'`-Ã,´\s]{1,40}. For the contact name valida- tor, you can use the regular expression, [a-zA-Z'`-Ã,´\s]{1,30}. Finally, for the phone number validator, you can use the built-in regular expression built into ASP.NET, “((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}”. 2. You can write code to constrain, reject, and sanitize the input further. In partic- ular, if the database developer provides further restrictions such as not allowing apostrophes or percent symbols, you can remove those symbols from the input by using the String.Replace method. Case Scenario 2: Processing Data from a Legacy Computer 1. Yes, you can extract data from text reports using regular expressions. You could use the Regex.Match method and the Match.Groups collection to extract the important data. 2. Yes, you can read ASCII files. You do not need to specify a certain type of encod- ing to read ASCII files because the standard settings for file streams will process ASCII correctly. [...]... ConfigurationManager.ConnectionStrings D Incorrect: Application settings (accessible through ConfigurationManager.AppSettings) are separate from connection strings Instead, you should access ConfigurationManager.ConnectionStrings 2 Correct Answers: A, C, and D A Correct: Because you are creating a WPF application, you are using a recent version of NET Framework Starting with NET Framework version 2.0, you should derive... serialization In this case, you will be communicating only with other NET Framework based applications In addition, the network manager asked you to conserve bandwidth 2 In all likelihood, you will need to add only the Serializable attribute to enable serialization 3 It depends on how you establish the network connection, but the serialization itself should require only two or three lines of code 722 Answers... Configuring an Application 1 You can configure a section in either the Machine.config file or the application s config file 2 You can configure an section in either the Machine.config file or the application s config file 3 The setting should be stored in the Machine.config file because it will be shared by multiple applications The application- specific... hostEvidence As Object() = {New Zone (SecurityZone.Internet)} Dim internetEvidence As Evidence = New Evidence (hostEvidence, Nothing) Dim myDomain As AppDomain = AppDomain.CreateDomain("QADomain") myDomain.ExecuteAssembly("C:\path\CASDemands.exe", internetEvidence) // C# object [] hostEvidence = {new Zone(SecurityZone.Internet)}; Evidence internetEvidence = new Evidence(hostEvidence, null); AppDomain... other ways to start separate processes B Correct: You can call AppDomain.Unload to close the application domain and free up resources C Incorrect: Creating a separate application domain does not improve performance D Correct: Application domains provide a layer of separation In addition, you can limit the application domain’s privileges, reducing the risk of a security vulnerability being exploited... specify the base directory for an application B Incorrect: BaseDirectory is read-only C Incorrect: The DynamicBase property specifies the location in which dynamically generated files are located It does not specify the base directory for an application D Correct: Use an instance of the AppDomainSetup class to configure an application domain and set the AppDomainSetup.ApplicationBase property to set... Management contains the Services snap-in, which you can use to configure user accounts for services C Incorrect: While you can use the Net command to start, stop, pause, and continue a service, you cannot use Net to configure user accounts for services D Incorrect: The .NET Framework Configuration tool does not contain a tool to configure user accounts for services 5 Correct Answer: A A Correct: You must... ManagementEventWatcher.EventArrived handler Chapter 10: Case Scenario Answers Case Scenario 1: Improving the Manageability of an Application 1 To provide real-time performance data, create a custom performance counter category and publish performance information from your application Systems administrators can then monitor the performance data either locally or across the network To provide logging information... Testing Tool 1 You should create an application that prompts the user to select a zone and an assembly Based on their selections, you should start the assembly in an application domain with evidence that would cause it to be assigned to the code group corresponding to the selected zone 2 Although several techniques would work, the simplest way to do this is to assign Internet zone evidence to the assembly,... Correct: The primary purpose of providing evidence for an application domain is to modify the privileges that the runtime assigns to the application domain D Incorrect: Evidence is not related to auditing 2 Correct Answers: A and D A Correct: You can pass evidence to the AppDomain.CreateDomain method to apply the evidence to any assemblies run within that application domain B Incorrect: You can read AppDomain.Evidence, . expression, [a-zA-Z'`-Ã,´s]{1 ,30 }. Finally, for the phone number validator, you can use the built-in regular expression built into ASP .NET, “(((d {3} ) ?)|(d {3} -))?d {3} -d{4}”. 2. You can write. requirements. Answers 7 13 2. Yes, if the application uses the same compression algorithm to access the files. 3. Yes, there’s nothing to prevent you from using GZipStream with isolated storage. Chapter 3: Lesson. Correct: UTF -32 has the largest byte size and yields the largest file size. B. Incorrect: UTF-16 has a smaller byte size than UTF -32 . C. Incorrect: UTF-8 has a smaller byte size than UTF -32 . D. Incorrect: