70-483 Create and use Types

¡Supera tus tareas y exámenes ahora con Quizwiz!

You are creating a generic class that should work only with reference types. Which type constraint should you add? 1.where T : class 2.where T : struct 3.where T : new() 4.where T : IDisposable

Correct answer: A 1.Correct: Constraining your generic type parameter to class allows the class to be used only with a reference type. 2.Incorrect: This will constrain the class to be used with a value type, not a reference type. 3.Incorrect: This will constrain the class to be used with a type that has an empty default constructor. It can be both a value and a reference type. 4.Incorrect: This constrains the class to be used with a type that implements the IDisposable interface.

You want your type to be able to be converted from string. Which interface should you implement? 1.IFormattable 2.IFormatProvider 3.IComparable 4.IConvertible

Correct answer: A 1.Correct: IFormattable provides the functionality to format the value of an object into a string representation. It is also used by the Convert class to do the opposite. 2.Incorrect: IFormatProvider is used to retrieve an object that controls formatting, not the actual formatting. 3.Incorrect: IComparable is used to sort items. 4.Incorrect: IConvertible defines methods to convert a type to an equivalent CLR type.

You have a class that implements two interfaces that both have a method with the same name. Interface IA should be the default implementation. Interface IB should be used only in special situations. How do you implement those interfaces? 1.Implement IA implicitly and IB explicitly. 2.Implement both IA and IB explicitly. 3.Implement both IA and IB implicitly. 4.Implement IA explicitly and IB implicitly

Correct answer: A 1.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. 2.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. 3.Incorrect: Implementing both IA and IB implicitly won't allow for a different implementation for IB. 4.Incorrect: Implementing IB implicitly makes IB the default implementation instead of IA

You are using an ArrayList as a collection for a list of Points, which are a custom struct. You are experiencing performance problems when working with a large amount of Points. What do you have to do? 1.Use a generic collection instead of ArrayList. 2.Change Point to be a reference type. 3.Add an implicit conversion from Point to object. 4.Make the collection of type dynamic.

Correct answer: A 1.Correct: Using a generic collection will eliminate the need to box and unbox values. This will improve performance, especially when working with a large number of items. 2.Incorrect: Changing Point to be a reference type could increase memory usage. You will still have to convert from object to Point when using the nongeneric ArrayList. 3.Incorrect: Point is a struct that inherits from System.ValueType, which in turn inherits from System.Object. The implicit conversion is already present; adding it won't improve performance. 4.Incorrect: Making the collection dynamic will loosen compile-time checking. It won't improve performance because the runtime has to do extra work.

You are creating a new collection type and you want to make sure the elements in it can be easily accessed. What should you add to the type? 1.Constructor 2.Indexer property 3.Generic type parameter 4.Static property

Correct answer: B 1.Incorrect: A constructor is used to create an instance of a new type. 2.Correct: An indexer property enables the user of the type to easily access a type that represents an array-like collection. 3.Incorrect: Making the type generic enables you to store multiple different types inside your collection. 4.Incorrect: A static property cannot access the instance data of the collection.

You are creating a custom Distance class. You want to ease the conversion from your Distance class to a double. What should you add? 1.Nothing; this is already possible. 2.An implicit cast operator. 3.An explicit cast operator. 4.A static Parse method

Correct answer: B 1.Incorrect: A conversion between a custom class and a value type does not exist by default. 2.Correct: Adding an implicit operator will enable users of your class to convert between Distance and double without any extra work. 3.Incorrect: Although adding an explicit cast operator will enable users of the class to convert from Distance to double, they will still need to explicitly cast it. 4.Incorrect: A Parse method is used when converting a string to a type. It doesn't add conversions from your type to another type

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? 1.A protected field 2.A public property with a protected set modifier 3.A protected property 4.A protected property with a public get modifier

Correct answer: B 1.Incorrect: A protected field cannot be read by other types outside of the class. 2.Correct: A public property can be read by all other types. The protected set modifier restricts changes to derived types. 3.Incorrect: A protected property cannot be read by other types outside of the class. 4.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.

You want to create a type that can be easily sorted. Which interface should you implement? 1.IEnumerable 2.IComparable 3.IDisposable 4.IUnknown

Correct answer: B 1.Incorrect: IEnumerable should be implemented on collection-like types so they can be easily iterated over. 2.Correct: IComparable enables objects to be compared to each other. It returns an integer value that represents the relative position as smaller than 0, 0 or larger than 0. 3.Incorrect: IDisposable should be implemented on types that access unmanaged resources and that need to release them. 4.Incorrect: IUnknown is used only when working with the COM

You are parsing a large piece of text to replace values based on some complex algorithm. Which class should you use? 1.StringReader 2.StringBuilder 3.StringWriter 4.String

Correct answer: B 1.Incorrect: StringReader is an adapter of the StringBuilder class so that it can be used in places where a TextReader is required. 2.Correct: The StringBuilder class is most efficient when changing large amounts of strings. 3.Incorrect: The StringWriter is used in places where a TextWriter is required. It's an adapter of the StringBuilder. 4.Incorrect: The regular String class is immutable, so it's not efficient to use when changing large amounts of strings.

Your application is using a lot of memory. Which solution should you use? 1.Turn all references into WeakReferences. 2.Set all references to null when you are done with them. 3.Use a caching algorithm to decide which objects can be freed. 4.Use a background thread to call GC.Collect() on a scheduled interval.

Correct answer: C 1.Incorrect: A WeakReference is not an equivalent to an efficient caching strategy. Turning all items into WeakReferences will complicate your code (you have to see whether the memory is cleared). It could potentially increase your memory usage because the WeakReference itself also takes up memory. 2.Incorrect: Setting references to null is optimized away by the compiler. Unlike some other languages, it doesn't explicitly free any memory. 3.Correct: A caching strategy is the best solution. You can decide whether you want to free memory based on usage, a timestamp, or some other criteria. 4.Incorrect: Calling GC.Collect on a scheduled interval won't improve your memory usage. Memory is freed only when there are no root references to an object. GC.Collect will stall your execution thread, making things slower, while not freeing any more memory than waiting for a regular Collect to take place.

You want to create a hierarchy of types because you have some implementation code you want to share between all types. You also have some method signatures you want to share. What should you use? 1.An interface 2.A class with virtual methods 3.An abstract class 4.A sealed class

Correct answer: C 1.Incorrect: An interface won't let you share any implementation code, only the public member signatures. 2.Incorrect: A class requires you to have an implementation for every member. It doesn't give you the option to only declare a member signature. 3.Correct: An abstract class enables you to share both implemented methods and method signatures that a derived class needs to implement. 4.Incorrect: A sealed class can't be inherited

You pass a struct variable into a method as an argument. The method changes the variable; however, when the method returns, the variable has not changed. What happened? 1.The variable was not initialized before it was passed in. 2.A value type cannot be changed inside a method. 3.Passing a value type makes a copy of the data. The original wasn't changed. 4.The method didn't return the changes

Correct answer: C 1.Incorrect: Passing a noninitialized struct will result in a compile error of using an unassigned local variable. 2.Incorrect: A struct can be changed inside a method. It won't change the original struct that was passed in, however. 3.Correct: Passing a struct will make a copy of the data. The copy can be changed; the original won't change with it. 4.Incorrect: With a reference type, the method can make changes that will reflect on the original. Because a value type is copied, it won't change the original. Returning the changes from the method will again create a new instance that will overwrite the original

You are about to execute a piece of code that is performance-sensitive. You are afraid that a garbage collection will occur during the execution of this code. Which method should you call before executing your code? 1.GC.RemoveMemoryPressure() 2.GC. SuppressFinalize() 3.GC.Collect() 4.GC.WaitForPendingFinalizers()

Correct answer: C 1.Incorrect: RemoveMemoryPressure should be called only after calling AddMemoryPressure to inform the runtime that a managed object uses a large amount of unmanaged memory. 2.Incorrect: SuppressFinalize should be called in the Dispose method of an object to inform the runtime that the object doesn't need to be finalized any more. 3.Correct: Collect will execute a garbage collection, freeing as much memory as possible at that time, which can be a very expensive process. This won't prevent the garbage collector from executing during your time-sensitive code, but it will make it less likely to happen. 4.Incorrect: WaitForPendingFinalizers suspends the current thread so all finalizers that are on the finalization queue can run. This will free some memory (for all objects that are waiting for finalization). Normally, however, you call this code after calling Collect if you want to make sure that all finalizers have run.

An object that is implementing IDisposable is passed to your class as an argument. Should you wrap the element in a using statement? 1.Yes, otherwise a memory leak could happen. 2.No, you should call Close on the object. 3.No, you should use a try/finally statement and call Dispose yourself. 4.No, the calling method should use a using statement.

Correct answer: D 1.Incorrect: A memory leak won't happen because the finalizer of the class will eventually execute and dispose of the element. Disposing of the object in your method will cause an ObjectDisposedException to be thrown in other code that uses the object. 2.Incorrect: The Close method is sometimes used as a secondary method that internally calls Dispose. It's implemented on types such as File. The same reasoning applies as with answer A. 3.Incorrect: A using statement is equivalent to a try/finally statement with a Dispose call. However, you don't want to dispose of the item because the calling code could depend on the object being in a usable state. 4.Correct: The calling code knows what the lifetime of the object should be and should decide when to dispose of the object.

What access modifier should you use to make sure that a method in a class can only be accessed inside the same assembly by derived types? 1.Make the class public and its members public. 2.Make the class public and its members protected. 3.Make the class internal and its members internal. 4.Make the class internal and its members protected.

Correct answer: D 1.Incorrect: A public class with public members can be accessed from other assemblies without any restrictions. 2.Incorrect: Types in other assemblies can derive from the class and access the protected methods. 3.Incorrect: Types in other assemblies cannot derive from the class, but other types in the same assembly can access the method. 4.Correct: An internal class cannot be accessed outside of its assembly. The protected methods can be accessed only by derived types.

You want to display only the date portion of a DateTime according to the French culture. What method should you use? 1.dt.ToString(new CultureInfo("fr-FR")) 2.dt.ToString("M", new CultureInfo("fr-FR")); 3.dt.ToString("d"); 4.dt.ToString("d", new CultureInfo("fr-FR"));

Correct answer: D 1.Incorrect: Only specifying the culture will give you the full date and time. 2.Incorrect: Specifying "M" as the format string results in "22 avril" without the year. 3.Incorrect: This will give the date in the correct format, but not with the French culture. 4.Correct: This will give the date in the correct French format.

You want to determine whether the value of an object reference is derived from a particular type. Which C# language feature can you use? (Choose all that apply.) 1.An as operator 2.An implicit cast 3.An is operator 4.A dynamic keyword

Correct answers: A, C 1.Correct: The as operator will return null if the conversion failed. If it succeeds, it will return the converted object. Seeing whether the result is null enables you to check for a valid conversion. 2.Incorrect: Implicitly casting something of type object to another type is not possible. It would require an explicit cast. 3.Correct: The is keyword will see whether a type is derived from another type. 4.Incorrect: The dynamic keyword can be used when you want weakly typing. It will still throw errors at runtime if an action is not possible

You want to inherit from an existing class and add some behavior to a method. Which steps do you have to take? (Choose all that apply.) 1.Use the abstract keyword on the base type. 2.Use the virtual keyword on the base method. 3.Use the new keyword on the derived method. 4.Use the override keyword on the derived method.

Correct answers: B, D 1.Incorrect: When you mark the base type as abstract, you can't create an instance of it. You can't use both the base and the derived type as concrete types in your code. 2.Correct: Marking the method in the base class as virtual enables it to be overridden by derived classes. 3.Incorrect: The new keyword hides the method in the base class. You shouldn't use it when you want to extend the behavior from the base class. 4.Correct: The override keyword enables you to override a method marked as virtual in a base class.


Conjuntos de estudio relacionados

Knowledge Assessment #2 - Fundamentals of IT (ITFUNTST2-V2)

View Set

Unit 6 review- properties of matter

View Set

History Midterm 2022 Ms. McClintock class

View Set

Self Test: Integrated Medicine Chapter 4 HCT

View Set

GEOGRAPHY 305: INTRO TO THE CITY EXAM #2

View Set

Economics- Chapter 10, Chapter 15, Chapter 16 (31-63)

View Set

Government involvement and real estate financing

View Set

Prep U questions Chapter 40: Fluid, Electrolyte, and Acid-Base Balance

View Set