Previous Section  < Day Day Up >  Next Section

3.14. Test Your Understanding

1:

What type of class cannot be inherited?

2:

How many classes can a class directly inherit? How many interfaces?

3:

Referring to the following class, answer the questions below:


public class ShowName {

   public static void ShowMe(string MyName)

   { Console.WriteLine(MyName); }

}


  1. Can the method ShowName be referenced from s?

    
    ShowName s = new ShowName();
    
    s.ShowMe("Giacomo");
    
    

  2. Write a code sample that will print "My Name is Ishmael".

4:

Can an abstract class have non-abstract methods?

5:

What keyword must a derived class use to replace a non-virtual inherited method?

6:

What are the results from running the following code?


public class ParmTest

{

   public static void GetCoordinates(ref int x, int y)

   {

      x= x+y;

      y= y+x;

   }

}

// calling method

int x=20;

int y=40;

ParmTest.GetCoordinates(ref x, y);

Console.WriteLine("x="+x+" y="+y);


  1. 
    x=60 y=40
    
    

  2. 
    x=20 y=60
    
    

  3. 
    x=20 y=40
    
    

  4. 
    x=60 y=60
    
    

7:

What is the best way to ensure that languages that do not recognize operator overloading can access C# code containing this feature?

8:

Name two ways that you can prevent a class from being instantiated.

9:

Your application contains a class StoreSales that fires an event when an item is sold. The event provides the saleprice (decimal), date (DateTime), and the itemnum (int) to its subscribers. Create an event handler that processes the ItemSold event by extracting and printing the sales data.


public delegate void SaleDelegate(object sender,

                                  SaleEvArgs e);

public event SaleDelegate ItemSold;

StoreSales mysale= new StoreSale();

mySale.ItemSold += new SaleDelegate(PrintSale);


10:

What happens if you attempt to compile and run the following code?


using System;

public class Base

{

   public void aMethod(int i, String s)

   {

      Console.WriteLine("Base Method");

   }

   public Base()

   {

      Console.WriteLine("Base Constructor");

   }

}



public class Child: Base

{

   string parm="Hello";

   public static void Main(String[] argv)

   {

      Child c = new Child();

      c.aMethod();

   }

   void aMethod(int i, string Parm)

  {

      Console.WriteLine(Parm);

   }

   public void aMethod()

   { }

}


  1. Error during compilation.

  2. "Base Constructor" is printed.

  3. "Base Constructor" and "Base Method" are printed.

  4. "Base Constructor" and "Hello" are printed.

    Previous Section  < Day Day Up >  Next Section