Object Oriented Programming
How many parameters can a default constructor have?
0 *Explanation* If you don't provide a constructor for your class, C# creates one by default that instantiates the object and sets member variables to default values. The default constructor does not have any parameters.
Encapsulation
A class or struct can specify how accessible each of its members is to code outside of the class or struct. Encapsulation gives class designers the flexibility to change a section of code when needed without changing all the other code that makes use of that code. Also, when you hide information, you hide the complexity associated with it. As a result, with the help of encapsulation, you can write code that is easier to understand and maintain. Encapsulation can be implemented by using access modifiers, using constructors to enforce data-field initialization, using properties and methods, as well as using interfaces to access classes. *Benefits are: Restricted Access, flexibility, and maintainability*
A protected virtual member function
Can be overridden by a derived class *Which of the following types of functions can be overridden by a derived class?* A member must be either virtual or abstract to be overridden. The private access modifier restricts access to its containing class, so it is not accessible by a derived class. A protected member is accessible within its class and by all derived classes, so the correct answer is a protected virtual member function.
When a base class declares a method as virtual, the method....
Can be overriden with its own implementation by a derived class. *Explanation* A virtual method may be overridden by a derived class but does not have to be. It can still be implemented as a member of the base class.
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?
Create a constructor in the Product class to initialize data members.
Which term is used to describe a class that inherits functionality from an existing class? Base class Inherited class Derived class Super class
Derived class A derived class inherits all the functionality of the base class and can also define additional features that make it different from the base class.
Inheritance
Enables you to create new classes that reuse, extend, and modify the functionality defined in existing classes. The class that inherits the functionality is called a derived class, and the class whose functionality is inherited is called a base class. A derived class inherits all the functionality of the base class and can also define additional features that make it different from the base class.
A class named Manager is derived from a parent class named Employee. The Manager class includes characteristics that are unique to managers. Which term is used to describe this object-oriented concept? Encapsulation Data Modeling Inheritance Data hiding
Inheritance
Inheritance
Inheritance enables you to create new classes that *reuse, extend and modify the functionality* defined in existing classes.
Upcasting
Is an operation that creates a base class reference from a derived class reference.
3 items that are the benefits of encapsulation
Maintainability Flexibility Restricted access
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? *Add the query functionality to a base class with the public access modifier Have all the classes inherit from an abstract base class and override the base-class method to provide their own query functionality Have all the classes inherit from a base class the provides the query functionality. Create a common interface that is implemented by all the classes.*
Option 1, 2 and 3 requires for the classes to inherit from the base class, which requires an is-a relationship. The question states that not all classes have an is-a relationship with each other. *Creating a common interface that is implemented by all classes* will make provision for each class's algorithm for the query to be different.
You have a base class named Shape in your application. The Rectangle and Triangle classes both inherit Circumference from the Shape class but each compute it differently. Which term is used to describe this object-oriented concept?
Polymorhpism
Polymorhpism
Polymorphism is the ability of derived classes to share common functionality with base classes but still define their own unique behaviour. You can allow derived classes to override a member in a base class by declaring the base member as virtual (it may be overridden in the derived class member) or abstract (it has to be overridden by the derived class member) and using the override keyword in the derived class member declaration.
You are designing a base class for an application. You need to restrict access to a variable named count to the base class and any classes that are derived from the base class. Private Protected Internal Public
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? Public Private Procted Internet
Protected Explanation *Public* access is not restricted. *Private* access is restricted to the containing class. *Protected* access is restricted to the containing class and to any class that is derived directly or indirectly from the containing class. *Internal* access is restricted to the code in the same assembly. *Protected internal* is 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.
You need to allow a consumer of a class to modify a private data member. What should you do?
Prove a public function that assigns a value to the data member.
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?
Sealed When applied to a class, the sealed modifier prevents other classes from inheriting from it.
You have a class with a property. You need to ensure that consumers of the class can write to the value of the property. Which keyword should you use?
Set *Explanation* The set keyword defines an accessor method in a property or indexer that assigns a value to the property or the indexer element.
Static members
Static members belong to a class itself rather than individual objects. A static method is callable on a class even when no instance of the class has been created.
What is the purpose of a class constructor?
To initialize an object of the class. Constructors are special class methods that are executed when a new instance of a class is created. Constructors are used to initialize the data members of the object. Constructors must have exactly the same name as the class, and they do not have a return type.
Abstract modifier
Use the abstract modifier in a *class* declaration to indicate that a *class* is intended only to be a *base class* of other classes. Abstract modifier indicates that the object being modified has a missing or incomplete implementation. Members marked as abstract, or included in an abstract class, *must* be implemented by classes that derive from the abstract class.
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?
Virtual
Downcasting
an operation that creates a derived class reference from a base class reference.
You have a class named Glass that inherits from a class named Windows. The Windows class includes a protected method named Break(). How should you call the Glass class implementation of the Break() method?
base.Break(); *Explanation* The correct way would be to use base.Break(); When you create the break method in the Glass class in Visual Studio, it automatically adds the base.break(); implementation to the overridden break method in the Glass class.
You have a class named Truck that inherits from a base class named Vehicle. The Vehicle class includes a protected method named brake (). How should you call the Truck class implementation of the brake () method?
base.break(); *Explanation* The base keyword is used to access members of the base class from within a derived class: Call a method on the base class that has been overridden by another method.
You need to create a property in a class. Consumers of the class must be able to read the values of the property. Consumers of the class must be prevented from writing values to the property. Which property procedure should you include?
get
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?
public *new* void The override keyword replaces a base-class member in a derived class. The new keyword creates a new member with the same name in the derived class and hides the base-class implementation.
has-a
refers to composition of classes - one class can make use of the properties of another, e.g. Car has an engine, so there would be a Car class in which you can instantiate an Engine object, but an Engine is not a Car. Both inheritance and composition allows you to create sub-objects.
Is-a relationship
refers to inheritance. Properties can also be protected or internal to be inherited. Both inheritance and composition allows you to create sub-objects.
Polymorphism
the ability of derived classes to share common functionality with base classes but still define their own *unique behavior*.
Converting an object to a more general type is called
upcasting Upcasting converts an object of a specialized type to a more general type. Downcasting converts an object from a general type to a more specialized type.
Virtual
used to modify a method, property, indexer, or event declaration and *allow* for it to be overridden in a derived class. The virtual member can also be executed from the base class.