Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Output of the following code is: delegate int NumberChanger(int n); namespace

ID: 3854971 • Letter: 1

Question

1. Output of the following code is:

delegate int NumberChanger(int n);

namespace DelegateApp

{

   class TestDelegate

   {

      static int num = 10;

      public static int AddNum(int p)

      {

         num += p;

         return num;

      }

      public static int MultNum(int q)

      {

         num *= q;

         return num;

      }

      static void Main(string[] args)

      {

         //create delegate instances

         NumberChanger nc;

         NumberChanger nc1 = new NumberChanger(AddNum);

         NumberChanger nc2 = new NumberChanger(MultNum);

         nc = nc1;

         nc = nc2;

        

         //Invoke

num = nc(5);

         Console.WriteLine("Value of Num: {0}", num);

         Console.ReadKey();

      }

   }

}

2. What modifier should be used with a method if you have a class which can be used directly, but for which you want inheritors to be able to change certain behavior.

3. When you create an abstract method, you provide __________.

4. Abstract classes and interfaces are similar in that __________.

5. When you create any derived class object, __________.

6. In a program that declares a derived class object, you __________ assign it to a variable of its base class type.

Value of Num: 55

Explanation / Answer

1)Value of Num: 15

2)Protected: It makes class member inaccessible outside the class. But they can be accessed by any subclass of that class(Inheritance)

3)When you create an abstract method, we provide nothing(no curly braces, no method statements)

but we create abstract method with abstract keyward

Ex:

abstract methodName();

4)Abstract classes and interfaces are similar in that ,we cannot instantiate concrete objects from either one.

We can not instanitate objects for abstract classes and interfaces. But we can use them for reference other classes

5)When you create any derived class object,the base class constructor must execute first, then the derived class constructor executes.

Base class constructor must exectutes first than the derived class constructor beacuse of inheritance of propeties(from base class) in the deried class. So Base class constructor has to extecte first inorder to initialize base class objects(variables and all)

6)In a program that declares a derived class object, you __________ assign it to a variable of its base class type.

we can assign it to a variable of its base class type(Run time polymophism)