Review Questions

   


1:

You are writing a Planet class for a solar system simulation program. You need to keep track of the number of instantiated Planet objects. If you were to store this data as a variable inside Planet, should it be an instance variable or a static variable? Why?

2:

Can you assign a value to a static variable even if no objects have been created?

3:

Can a static variable be accessed from within an object of the class in which it is declared?

4:

The following declaration was found inside a class:

 public static const double MassOfElectron = 9.0e-28; 

Is it correct? Why or why not?

5:

Does it go against encapsulation principles to declare a const to be public?

6:

A class called Planet contains the following declarations:

 class Planet {     private static uint numberOfPlanetsCreated;     private double mass;     ...     public static double CalculateGravity(...)     {         ...     }     public double Density(...)     {         ...     } } 

Is it possible to

  1. Access numberOfPlanetsCreated from within the CalculateGravity method

  2. Access mass from within the CalculateGravity method

  3. Access numberOfPlanetsCreated from within the Density method

  4. Access mass from within the Density method

  5. Access numberOfPlanetsCreated from outside the Planet class by writing Planet.numberOfPlanetsCreated

  6. Access mass from outside the Planet class by writing Planet.mass

7:

Among other methods, your class Planet contains two methods of approximately the same length. One is a static method related to the Planet class, the other is an instance method related to a particular Planet object. Suppose 100 objects of type Planet exist inside your running solar system simulation. Is the following statement true or false? "All the instance methods of the Planet class take up approximately 100 times more memory than the static method." Why?

8:

Consider the following method definition:

 public void MyMethod(ref int myValue, int yourValue) {     myValue = 100;     yourValue = 200; } 

Which of the following calls to MyMethod are correct and, if correct, what is the value of myArgument and yourArgument after MyMethod returns (myArgument and yourArgument are both of type int and initially both contain the value 0)?

  1. MyMethod(myArgument, yourArgument)

  2. MyMethod(ref myArgument, ref yourArgument)

  3. MyMethod(ref 10, ref yourArgument)

9:

Is the following method correct? Why or why not?

 public int Sum(out int x, int y) {     return x + y; } 
10:

Consider the following method header:

 public double Average(params int[] numbers) 

Which of the following calls to this method are valid? (myArray is an array of base type int with 100 elements.)

  1. Average()

  2. Average(10)

  3. Average(10,20)

  4. Average(10,20,30.0)

  5. Average(myArray)

11:

The following two method headers were found inside the same class. What can you call the methods?

 public int Sum(int number1, int number2) public double Sum(double number1, double number2) 
12:
  1. Are the methods with the following method headers correctly overloaded? Why or why not?

     public double Average(int x, int y) public int Average(int x, int y) 
  2. Are the methods with the following method headers correctly overloaded? Why or why not?

     public double Average(int myX, int myY) public int Average(int yourX, int yourY) 
13:

A class contains the following method header:

 public uint Sum(uint x, uint y) 

Is the following method call valid? Why or why not? (myByte and yourByte are both of type byte, which is different from Sum's parameter types uint.)

 Sum(myByte, yourByte) 
14:

Consider the following class called Planet:

 class Planet {     private double myMass = 2000000;     public double DoSomething(double myMass)     {         return (myMass * this.myMass) + myMass;     } } 

What value will the following method call have when DoSomething returns (if the value of the instance variable myMass is still 2000000)?

 DoSomething(20.0) 


   


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net