361 Lesson 2 Introduction to Object-Oriented Programming
Abstract Classes
1. The _________ ________ provide a common definition of a base class that can be shared by multiple derived classes. 2. The ___________ _______ often provides incomplete implementation. 3. To instantiate an _________ ______ you must inherit from it and complete its implementation. abstract class Polygon { public double Length { get; protected set; } public double Width { get; protected set; } abstract public double GetArea(); } class Rectangle: Polygon { public Rectangle(double length, double width) { Length = length; Width = width; } public override double GetArea() { return Width * Length; } }
sealed classes
1. The _________ classes provide complete functionality but cannot be used as base classes. 2. Use the ____________ keyword when your implementation is complete and you do not want a class or its member to be inherited. sealed class Rectangle: Polygon { // a sealed class implementation goes here } public clas Sample : Polygon { // example of a sealed method sealed public override string GetName() { return "MyPolygon"; } }
objects
1. ______-oriented programming is a programming technique that makes use of objects. 2. _________ are self-contained data structures that consist of properties, methods, and events. a.Properties specify the data represented by an object b. Methods specify an object's behavior c. Events provide communication between objects
delegates
1. __________ are special objects that can hold a reference to a method with a specific signature. 2. ________ are special types that are used to encapsulate a method with a specific signature. public delegate void RectangleHandler(Rectangle rect); 3. Here, you define a RectangleHandler delegate that can hold references to a method that returns void and accepts a single parameter of the Rectangle type. public void DisplayArea(Rectangle rect) { Console.WriteLine(rect.GetArea()); } 4. The signature of DisplayArea method matches the RectangleHandler delegate and therefore can be assigned to one of its instance.
events
1. ____________ are a way for a class to notify other classes or objects when something of interest happens. 2. The class that sends the notification is called a publisher of the event. 3. The class that receives the notification is called the subscriber of the event class Rectangle { public event EventHandler Changed; private double length; public double Length { get { return length ; } set { length = value; Changed((this, EventArgs.Empty); } } }
polymorphism
1. ______________ is the ability of derived classes to share common functionality with base classes but still define their own unique behavior. 2. ________________ allows the objects of a derived class to be treated at runtime as objects of the base class. When a method is invoked at runtime, its exact type is identified, and the appropriate method is invoked from the derived class.
interfaces
1. ________________ are used to establish contracts through which objects can interact with each other without knowing the implementation details. 2.An _____________ definition cannot consist of any data fields or any implementation details such as method bodies. 3. A common ______________ defined in the System namespace is the IComparable namespace. interface IComparable { int CompareTo(object obj); } 4. Each class that implements IComparable is free to provide its own custom comparison logic inside the CompareTo method.
constructors
1. ________________ are used to initialize the data members of the object. 2. ____________ are special class methods that are executed when a new instance of a class is created. 3. _______________ must have exactly the same name as the class and they do not have a return type. 4. Multiple _______________, each with a unique signature, can be defined for a class. class Rectangle { private double length; private double width; public Rectangle(double l, double w) { length = l; width = w; }
inheritance
1. __________________ is an OOP feature that allows you to develop a class once, and then reuse that code over and over as the basis of new classes. 2. The class whose functionality is inherited is called a base class. 3. The class that inherits the functionality is called a derived class 4. A derived class can also define additional features that make it different from the base class. 5. Unlike classes, the structs do not support inheritance.
encapsulation; access modifiers
1. is a mechanism to restrict access to a class or class members in order to hide design decisions that are likely to change. 2. is an information-hiding mechanism that makes code easy to maintain and understand. 3. _______ ________ control where a type or type member can be used.
b. virutal
In a class, you defined a method called Render. This method provides functionality to render bitmap files on the screen. You would like the derived classes to supersede this functionality to support the rendering of additional image formats. You also want the Render method of the derived classes to be executed even if a derived class is cast as the base class. Which keyword should you use with the definition of the Render method in the base class? a. abstract b. virtual c. new d. overrides
Creating Objects
1. Objects need a template that defines how they should be built. 2. All objects created from the same template look and behave in a similar way. class Program { static void Main(string[] args) { Rectangle rect = new Rectangle(10.0, 20.0); double area = rect.GetArea(); Console.WriteLine("Area of Rectangle: {0}",area); } }
The override and new Keywords
1. The___________ keyword replaces a base class member in a derived class. 2. The ___ keyword creates a new member of the same name in the derived class and hides the base class implementation. class Triangle: Polygon { public new void Draw() { Console.WriteLine("Drawing: Triangle"); } } class Program { static void Main(string[] args) { Triangle t = new Triangle(); t.Draw(); Polygon p = t; p.Draw(); }
The is Operator
1. To avoid runtime errors such as InvalidCastException, the __ operator can be used to check whether the cast is allowed before actually performing the cast. if (o is Rectangle) { Rectangle r = (Rectangle) o; } 2. Here, the runtime checks the value of the object o. The cast statement is only executed if o contains a Rectangle object.
is
You can use the __ operator to check whether it is legal to cast one type to another type
static
You can use the ________ keyword to declare a member that belongs to the class itself rather than to a specific object.
namespace
You can use_______________to group related classes in order to reduce name collisions.
Answer: b Section Reference: Understanding Polymorphism Use the virtual keyword to define the Area method. When a virtual method is invoked, the runtime type of the object is checked for an overriding member. The overriding member in the most derived class is called, which might be the original member, if no derived class has overridden the member.
You created a class named GeoShape. You defined a method called Area in the GeoShape class. This method calculates the area of a geometric shape. You want the derived classes of GeoShape to supersede this functionality to support the area calculation of additional geometric shapes. When the method Area is invoked on a GeoShape object, the area should be calculated based on the runtime type of the GeoShape object. Which keyword should you use with the definition of the Area method in the GeoShape class? a) abstract b) virtual c) new d) overrides
Inheritance - Example
class Polygon { public double Length { get; protected set; } public double Width { get; protected set; } } class Rectangle: Polygon { public Rectangle(double length, double width) { Length = length; Width = width; } public double GetArea() { return Width * Length; }
Polymorphism - Example
static void Main(string[] args) { List<Polygon> polygons = new List<Polygon>(); polygons.Add(new Polygon()); polygons.Add(new Rectangle()); polygons.Add(new Triangle()); foreach (Polygon p in polygons) { p.Draw(); } } Drawing: Polygon Drawing: Rectangle Drawing: Triangle
class
1. A ______ defines a blueprint for an object. 2. A _______ defines how the objects should be built and how they should behave. 3. An object is also known as an instance of a ________. class Rectangle { private double length; private double width; public Rectangle(double l, double w) { length = l; width = w; } public double GetArea() { return length * width; }
Answer: a Section Reference: Understanding Static Members A class is not a concrete instance of an object. Instead, an object is a concrete instance of a class. The facts in the other answer choices are all correct.
Suppose that you are a new C# developer and are reviewing object-oriented programming fundamentals. Which of the following statements is not true? a) A class is a concrete instance of an object. b) A class defines the template for an object. c) A class is a definition of a new data type. d) A constructor is used to initialize the data members of the object.
Answer: a Section Reference: Understanding Inheritance Use the sealed keyword to define the Scenario class. When applied to a class, the sealed modifier prevents other classes from inheriting from it.
Suppose that you defined a class Scenario that defines functionality for running customized pivot transform on large data sets. You do not want the functionality of this class to be inherited into derived classes. What keyword should you use to define the Scenario class? a) sealed b) abstract c) private d) internal
Answer: a Section Reference: Understanding Methods A method defines the behavior of a class. You can write a method that returns a list of all books sorted by the author's last name.
You are writing code for a class named Book. You should be able to get a list of all books sorted by the author's last name. You need to write code to define this behavior of a class. Which of the following class elements should you use? a) method b) property c) event d) delegate
b. Create a constructor in the Product class to initialize data members
You are writing code for a class named Product. You need to make sure that the data members of the class are initialized to their correct values as soon as you create an object of the Product class. The initialization code should be always executed. What should you do? a. Create a static method in the Product class to initialize data members. b. Create a constructor in the Product class to initialize data members. c. Create a static property in the Product class to initialize data members. d. Create an event
Polymorphism - Example
class Polygon { public virtual void Draw() { Console.WriteLine("Drawing: Polygon"); } class Rectangle: Polygon { public override void Draw() { Console.WriteLine("Drawing: Rectangle"); } } class Triangle: Polygon { public override void Draw() { Console.WriteLine("Drawing: Triangle"); } }
Values and References
1. A ______ type directly stores data within its memory. Most built-in elementary data types (such as bool, int, char, double, etc.) are value types. User-defined data types created by using the keyword struct are value types as well. 2. ___________ types store only a reference to a memory location. The actual data is stored at the memory location being referred to. Reference types include the types created by using the keywords object, string, interface, delegate, and class. 3. When you copy a reference type variable to another variable of the same type, only the references are copied. As a result, after the copy, both variables will point to the same object. class Program { public class Rectangle { public double Length { get; set; } public double Width { get; set; } } class Program { public struct Rectangle { public double Length { get; set; } public double Width { get; set; } }
methods
1. A _________ is a block of code containing a series of statements. 2. A _________ defines the actions or operations supported by a class. 3. A ________ is defined by specifying the access level, the return type, the name of the method, and an optional list of parameters in parentheses followed by a block of code enclosed in braces.
namespace
1. A ____________ is a language element that allows you to organize code and create globally unique class names. 2. The .NET Framework uses namespaces to organize all its classes. a. The System namespace groups all the fundamental classes. b. The System.Data namespace organizes classes for data access. c. The System.Web namespace is used for Web-related classes. namespace CompanyA { public class Widget { } } namespace CompnayB { public class Widget { } }
Cast
1. In C#, you can ____ an object to any of its base types. 2. All classes in the .NET Framework inherit either directly or indirectly from the Object class. 3. Assigning a derived class object to a base class object doesn't require any special syntax: Polygon o = new Rectangle(10, 20); 4. Assigning a base class object to a derived class object must be explicitly cast: Rectangle r = (Rectangle) o; 5. At execution time, if the value of o is not compatible with the Rectangle class, the runtime throws a System.InvalidCastException.
Method Example
1. The InitFields method takes two parameters and uses the parameter values to respectively assign the data field length and width. 2. When a method's return type is void, a return statement with no value can be used. 3. If a return statement is not used, as in the InitFields method, the method will stop executing when it reaches the end of the code block. public void InitFields(double l, double w) { length = l; width = w; }
Inheriting from Object
1. The Object class is the ultimate base class of all the classes in the .NET Framework. 2. All classes in the .NET Framework inherit either directly or indirectly from the Object class. class Polygon { public double Length { get; protected set; } public double Width { get; protected set; } } // the above class is equivalent to the following class Polygon: Object { public double Length { get; protected set; } public double Width { get; protected set; } }
The as Operator
1. The __ operator is similar to the cast operation but, in the case of __, if the type conversion is not possible, null is returned instead of raising an exception. Rectangle r = o as Rectangle; if (r != null) { // do something } 2. At runtime, if it is not possible to cast the value of variable o to a rectangle, a value of null is assigned to the variable r. No exceptions will be raised.
The this Keyword
1. The _____ keyword is a reference to the current instance of the class. 2. You can use the _____ keyword to refer to any member of the current object. class Rectangle { private double length; private double width; public Rectangle(double l, double w) { this.length = l; this.width = w; } 3. The this keyword can be used to access members from within constructors, instance methods, and accessors of instance properties.
static members
1. The ______ keyword is used to declare members that do not belong to individual objects but to a class itself. 2. When an instance of a class is created, a separate copy is created for each instance field, but only one copy of a static field is shared by all instances. 3. A _____ member cannot be referenced through an instance object. Instead, a static member is referenced through the class name. class Program { static void Main(string[] args) { Rectangle rect = new Rectangle { Length = 10.0, Width = 20.0 }; Console.WriteLine("Shape Name: {0}, Area: {1}", Rectangle.ShapeName, rect.GetArea()); }
Answer: b Section Reference: Understanding Classes Constructors are special class methods that are executed when a new instance of a class is created. Constructors are used to initialize the object's data members.
7. Suppose that you are writing code for a class named Product. You need to make sure that the data members of the class are initialized to their correct values as soon as you create an object of the Product class. The initialization code should always be executed. What should you do? a) Create a static method in the Product class to initialize data members. b) Create a constructor in the Product class to initialize data members. c) Create a static property in the Product class to initialize data members. d) Create an event in the Product class to initialize data members.
Answer: b Section Reference: Understanding Polymorphism The new keyword creates a new member of the same name in the derived class and hides the base class implementation. The override keyword is not the correct answer because it replaces a base class member in a derived class.
8. You are creating a new class named Sphere derived from the Shape class. The Shape class has the following code: class Shape { public virtual void Area() { // additional code... } } The Area method in the Shape class should provide new functionality but also hide the Shape class implementation of the Area method. Which code segment should you use to accomplish this? a) class Sphere : Shape { public override void Area() { // additional code ... } } b) class Sphere : Shape { public new void Area() { // additional code ... } } c) class Sphere : Shape { public virtual void Area() { // additional code ... } } d) class Sphere : Shape { public static void Area() { // additional code } }
abstract
A class that does not provide a complete implementation must be declared with the keyword _________.
Access modifier: protected internal
A combination of protected and internal—that is, access is restricted to any code in the same assembly and only to derived classes in another assembly.
accessors
A property has two ______________, get and set. The get _______ is used to return the property value, and the set ____________ is used to assign a new value to the property.
delegate
A(n)___________________is a type that references a method.
class
A(n)__________________is a blueprint of an object.
struct, class
A(n)___________is a value type, whereas a(n)_____________is a reference type.
Access modifier: public
Access is not restricted.
Access modifier: internal
Access is restricted to the code in the same assembly.
Access modifier: protected
Access is restricted to the containing class and to any class that is derived directly or indirectly from the containing class.
Access modifier: private
Access is restricted to the containing class.
CompareTo
Classes that want to support comparison must implement the IComparable interface and then provide a body for the__________________method.
this
The ____ keyword refers to the current instance of a class.
Subscribing to Events
The signature of the event handler method matches the requirements of the event's delegate. class Program { static void Main(string[] args) { Rectangle r = new Rectangle(); r.Changed += new EventHandler(r_Changed); r.Length = 10; } static void r_Changed(object sender, EventArgs e) { Rectangle r = (Rectangle)sender; Console.WriteLine("Value Changed: Length = {0}", r.Length); } }
encapsulation, inheritance, and polymorphism
Three main features of an object-oriented programming language are____________, ____________, and _______________________.
Answer: d Section Reference: Understanding Access Modifiers For the private access modifier, access is restricted only to the containing class. For the public access modifier, access is not restricted. For the protected access modifier, access is restricted only to the derived classes. For the internal access modifier, access is restricted only to the code in the same assembly.
You are C# developer who is developing a Windows application. You develop a new class that must be accessible to all the code packaged in the same assembly. Even the classes that are in the same assembly but do not directly or indirectly inherit from this class must be able to access the code. Any code outside the assembly should not be able to access the new class. Which access modifier should you use to declare the new class? a) public b) protected c) private d) internal
Answer: d Section Reference: Understanding Inheritance The abstract classes provide a common definition of a base class that can be shared by multiple derived classes. The sealed classes, on the other hand, provide complete functionality but cannot be used as base classes. The virtual and interface keywords cannot be applied to a class.
You are C# developer who is developing a Windows application. You need to provide a common definition of a base class that can be shared by multiple derived classes. Which keyword should you use to declare the new class? a) virtual b) sealed c) interface d) abstract
Answer: c Section Reference: Understanding Polymorphism Polymorphism is the ability of derived classes to share common functionality with base classes but still define their own unique behavior. Inheritance is a feature of object-oriented programming that allows you to develop a class once, and then reuse that code over and over as the basis of new classes.
You are C# developer who is developing a Windows application. You need to provide derived classes the ability to share common functionality with base classes but still define their own unique behavior. Which object-oriented programming concept should you use to accomplish this functionality? a) encapsulation b) abstraction c) polymorphism d) inheritance
Answer: c Section Reference: Understanding Inheritance In case of simple cast operation, the runtime checks whether the value of the variable o is compatible with the Rectangle class. If, at execution time, the value of o is not compatible with the Rectangle class, the runtime throws a System.InvalidCastException. The as operator is similar to the cast operation but, in the case of as, if the type conversion is not possible, null is returned rather than an exception raised.
You are C# developer who is developing a Windows application. You write the following code: Object o; Later in the code, you need to assign the value in the variable o to an object of Rectangle type. You expect that at runtime the value in the variable o is compatible with the Rectangle class. However, you need to make sure that no exceptions are raised when the value is assigned. Which of the following code should you use? a) Rectangle r = (Rectangle) o; b) Rectangle r = o; c) Rectangle r = o as Rectangle; d) Rectangle r = o is Rectangle;
Answer: a Section Reference: Understanding Interfaces The return value of the CompareTo method indicates the result of comparing the given parameter with the current object. According to the documentation of the CompareTo method, • If the instance is equal to the parameter, CompareTo returns 0. • If the parameter value is less than the instance or if the parameter is null, a positive value is returned. • If the parameter value is greater than the instance, a negative value is returned. • If the parameter is not of the compatible type, an ArgumentException is thrown.
You are creating a new class named Polygon. You write the following code: class Polygon : IComparable { public double Length { get; set; } public double Width { get; set; } public double GetArea() { return Length * Width; } public int CompareTo(object obj) { // to be completed } } You need to complete the definition of the CompareTo method to enable comparison of the Polygon objects. Which of the following code segments should you use? a) public int CompareTo(object obj) { Polygon target = (Polygon)obj; double diff = this.GetArea() - target.GetArea(); if (diff == 0) return 0; else if (diff > 0) return 1; else return -1; } b) public int CompareTo(object obj) { Polygon target = (Polygon)obj; double diff = this.GetArea() - target.GetArea(); if (diff == 0) return 1; else if (diff > 0) return -1; else return 0; } c) public int CompareTo(object obj) { Polygon target = (Polygon)obj; if (this == target) return 0; else if (this > target) return 1; else return -1; } d) public int CompareTo(object obj) { Polygon target = (Polygon)obj; if (this == target) return 1; else if (this > target) return -1; else return 0; }
a. public int CompareTo(object obj) { Rectangle target = (Rectangle)obj; double diff = this.GetArea() - target.GetArea(); if (diff == 0) return 0; else if (diff > 0) return 1; else return -1; }
You are creating a new class named Rectangle. You write the following code: class Rectangle : IComparable { public double Length { get; set; } public double Width { get; set; } public double GetArea() { return Length * Width; } public int CompareTo(object obj) { // to be completed } } You need to complete the definition of the CompareTo method to enable comparison of the Rectangle objects. Which of the following code should you write? a. public int CompareTo(object obj) { Rectangle target = (Rectangle)obj; double diff = this.GetArea() - target.GetArea(); if (diff == 0) return 0; else if (diff > 0) return 1; else return -1; } b. public int CompareTo(object obj) { Rectangle target = (Rectangle)obj; double diff = this.GetArea() - target.GetArea(); if (diff == 0) return 1; else if (diff > 0) return -1; else return 0; } c. public int CompareTo(object obj) { Rectangle target = (Rectangle)obj; if (this == target) return 0; else if (this > target) return 1; else return -1; } d. public int CompareTo(object obj) { Rectangle target = (Rectangle)obj; if (this == target) return 1; else if (this > target) return -1; else return 0; }
b. class Square : Polygon { public new void Draw() { // additional code ... } }
You are creating a new class named Square that is derived from the Polygon class. The Polygon class has the following code: class Polygon { public virtual void Draw() { // additional code... } } The Draw method in the Square class should provide new functionality but also hide the Polygon class implementation of the Draw method. Which code segment should you use to accomplish this? a. class Square : Polygon { public override void Draw() { // additional code ... } } b. class Square : Polygon { public new void Draw() { // additional code ... } } c. class Square : Polygon { public virtual void Draw() { // additional code ... } } d. class Square : Polygon { public static void Draw() { // additional code ... } }
Answer: a Section Reference: Understanding Namespaces Use the namespace keyword. A namespace is a language element that allows you to organize code and create globally unique class names.
You are developing a C# application. You create a class of the name Widget. You use some third-party libraries, one of which also contains a class of the name Widget. You need to make sure that using the Widget class in your code causes no ambiguity. Which C# keyword should you use to address this requirement? a) namespace b) override c) delegate d) class
Answer: d Section Reference: Understanding Static Members The static keyword is used to declare members that do not belong to individual objects but to a class itself. A static member cannot be referenced through an instance object. Instead, a static member is referenced through the class name. It is not possible to use the this keyword reference with a static method or property because the this keyword can be used only to access instance objects.
You are developing a C# application. You need to decide whether to declare a class member as static. Which of the following statements is true about static members of a class? a) You can use the this keyword reference with a static method or property. b) Only one copy of a static field is shared by all instances of a class. c) Static members of a class can be used only after an instance of a class is created. d) The static keyword is used to declare members that do not belong to individual objects but to a class itself.
Answer: a Section Reference: Understanding Access Modifiers The private modifier restricts the access to the class in which the member was defined. The protected modifier restricts the access to the containing class and to any class derived directly or indirectly from the containing class. The internal modifier restricts the access to the code in the same assembly. The public modifier does not restrict access.
You are developing code for a method that calculates the discount for the items sold. You name the method CalculateDiscount. The method defines a variable, percentValue of the type double. You need to make sure that percentValue is accessible only within the CalculateDiscount method. What access modifier should you use when defining the percentValue variable? a) private b) protected c) internal d) public
Answer: b Section Reference: Understanding Methods If a method does not intend to return any value, its return type is specified by the keyword void. As the method takes two parameters of data type double, the parameter list must declare two variables of type double.
You are developing code that defines an InitFields method. The method takes two parameters of data type double and does not return any value to the calling code. Which of the following code segments would you use to define the InitFields method? a) public double InitFields(double l, double w) { length = l; width = w; return length * width; } b) public void InitFields(double l, double w) { length = l; width = w; } c) public void InitFields(double l) { length = l; width = l; return; } d) public double InitFields(double l, double w) { length = l; width = w; }
Answer: c Section Reference: Understanding Values and References The class Rectangle is a reference type, and the content of variable r1 is actually a reference to a memory location that holds a Rectangle object. So, after the r2 = r1; statement, both r1 and r2 point to the same memory location and in turn the same Rectangle object. In other words, there is only one rectangle object in memory, and both r1 and r2 are referring to it. When the Length property is modified, the change applies to both objects r1 and r2.
You are reviewing a C# program that contains the following class: public class Rectangle { public double Length {get; set;} public double Width { get; set; } } The program executes the following code as part of the Main method: Rectangle r1, r2; r1 = new Rectangle { Length = 10.0, Width = 20.0 }; r2 = r1; r2.Length = 30; Console.WriteLine(r1.Length); What will be the output when this code is executed? a) 10 b) 20 c) 30 d) 40
Answer: a Section Reference: Understanding Values and References The struct is a value rather than a reference type, so both r1 and r2 maintain their own copies of data. So, after the r2 = r1; statement, both r1 and r2 point to different memory locations. When the Length property for r2 object is modified, the change doesn't affect the object r1.
You are reviewing a C# program. The program contains the following class: public struct Rectangle { public double Length {get; set;} public double Width { get; set; } } The program executes the following code as part of the Main method: Rectangle r1, r2; r1 = new Rectangle { Length = 10.0, Width = 20.0 }; r2 = r1; r2.Length = 30; Console.WriteLine(r1.Length); What will be the output when this code is executed? a) 10 b) 20 c) 30 d) 40
Answer: c Section Reference: Understanding Inheritance The as operator is similar to the cast operation but, in the case of as, if the type conversion is not possible, null is returned rather than an exception raised. An exception may be generated with the code in the other answer choices.
You are writing code for a new method named Process: void Draw(object o) { } The code receives a parameter of type object. You need to cast this object into the type Polygon. At times, the value of o that is passed to the method might not be a valid Polygon value. You need to make sure that the code does not generate any System.InvalidCastException errors while doing the conversions. Which of the following lines of code should you use inside the Draw method to accomplish this goal? a) Polygon p = (Polygon) o; b) Polygon p = o is Polygon; c) Polygon p = o as Polygon; d) Polygon p = (o != null) ? o as Polygon : (Polygon) o;
b. Rectangle r = o as Rectangle;
You are writing code for a new method named Process: void Process(object o) { } The code receives a parameter of type object. You need to cast this object into the type Rectangle. At times, the value of o that is passed to the method might not be a valid Rectangle value. You need to make sure that the code does not generate any System.InvalidCastException errors while doing the conversions. Which of the following lines of code should you use inside the Process method to accomplish this goal? a. Rectangle r = (Rectangle) o; b. Rectangle r = o as Rectangle; c. Rectangle r = o is Rectangle; d. Rectangle r = (o != null) ? o as rectangle : (Rectangle) o;
Answer: c Section Reference: Understanding Events You need to use the += operator rather than the simple assignment operator (=) to attach the event handler. By using the += operator, you ensure that this event handler will be added to a list of event handlers already attached with the event.
You are writing code to handle events in your program. You define a delegate named PolygonHandler like this: public delegate void PolygonHandler(Polygon p); You also create a variable of the PolygonHandler type as follows: PolygonHandler handler; Later in the program, you need to add a method named CalculateArea to the method invocation list of the handler variable. The signature of the CalculateArea method matches the signature of the PolygonHandler method. Any code that you write should not affect any existing event-handling code. Given this restriction, which of the following code lines should you write? a) handler = new PolygonHandler(CalculateArea); b) handler = CalculateArea; c) handler += CalculateArea; d) handler -= CalculateArea;
c. handler += DisplayArea;
You are writing code to handle events in your program. You define a delegate named RectangleHandler like this: public delegate void RectangleHandler(Rectangle rect); You also create a variable of the RectangleHandler type as follows: RectangleHandler handler; Later in the program, you need to add a method named DisplayArea to the method invocation list of the handler variable. The signature of the DisplayArea method matches the signature of the RectangleHandler method. Any code that you write should not affect any existing event-handling code. Given this restriction, which of the following code should you write? a. handler = new RectangleHandler(DisplayArea); b. handler = DisplayArea; c. handler += DisplayArea; d. handler -= DisplayArea;
a. sealed
You defined a class AdvMath that defines advanced mathematical functionality. You do not want the functionality of this class to be inherited into derived classes. What keyword should you use to define the AdvMath class? a. sealed b. abstract c. private d. internal
Answer: d Section Reference: Understanding Interfaces You should create a common interface that is implemented by all classes. Interfaces are used to establish contracts through which objects can interact with each other without knowing the implementation details.
You need to provide printing functionality to several of your classes. Each class's algorithm for printing will likely be different. Also, not all the classes have an "is-a" relationship with each other. How should you support this functionality? a) Add the print functionality to a base class with the public access modifier. b) Have all classes inherit from an abstract base class and override the base-class method to provide their own print functionality. c) Have all the classes inherit from a base class that provides the print functionality. d) Create a common interface that all classes implement.
d. Create a common interface that is implemented by all the classes.
You need to provide query functionality to several of your classes. Each class's algorithm for the query will likely be different. Also, not all the classes have an "is-a" relationship with each other. How should you support this functionality? a. Add the query functionality to a base class with public access modifier. b. Have all the classes inherit from an abstract base class and override the base-class method to provide their own query functionality. c. Have all the classes inherit from a base class that provides the query functionality. d. Create a common interface that is implemented by all the classes.
c. protected
You want to restrict the access for a method to the containing class or to a class that is derived from the containing class. Which access modifier should you use for this method? a. public b. private c. protected d. internal
as
___ operator is similar to the cast operation but, in the case of __, if the type conversion is not possible, null is returned instead of raising an exception.
Properties
________ allow you to access class data in a safe and flexible way. 1. ___________ are class members that can be accessed like data fields but contain code like a method. 2. A ____________ has two accessors, get and set. The get accessor is used to return the property value, and the set accessor is used to assign a new value to the property. class Rectangle { private double length; public double Length { get { return length; } Set { If (value>0.0) length = value } }
keyword struct
_________ is used to create user-defined types that consist of small groups of related fields. _________ are value types—as opposed to classes, which are reference types.
Auto-implemented properties
simplify property declarations.
