Review Questions


  1. If Speed is a property defined inside an object called myRocket, what is the general name for the block of statements defined for the Speed property that are executed when the following line is executed?

     myRocket.Speed = 40; 
  2. When the following line is executed?

     travelTime = distance / myRocket.Speed; 

Which capitalization style is recommended for

  1. Instance variables?

  2. Methods?

  3. Properties?


The following lines specify an instance variable declaration and what is meant to be a property with the task of allowing access to the instance variable speed from outside the object. However, the property definition has four problems that need to be fixed before it will work correctly. Find and fix the problems.

 private double speed; private int Speed() {     get     {         return Speed;     } } 

What is delayed initialization? When would you use it? Could delayed initialization also be implemented with accessor and mutator methods?


The header of the following indexer declaration has three problems. What are they?

 static int myIndexer this [ ] {     ... } 

The Rainfall class contains an array with twelve elements. It also contains an indexer to access the array elements, which, like its array, is zero-based (first element is zero). A rainfallParis object is instantiated from the Rainfall class.

  1. Write a statement that assigns the value of the third element of rainfallParis to the variable rainfallMarch.

  2. Write a statement that assigns the value rainfallJuly to the seventh element of rainfallParis.

  3. You are extending the Rainfall class with another method. Among other things in this method, you need to assign the third element of the indexer to your myMarchRainfall variable. Write the statement.


You have just finished implementing your Car class and are satisfied with the end result. During a quiet moment of contentment, your colleague suddenly suggests that to make the Car class perfect, you should include operator overloading for the class so it can be used with the + and - operators and support code like car3 = car1 + car2. Is this a good idea? Why or why not?


Why is operator overloading sometimes referred to as syntactic sugar?


Recall the TimeSpan class from Listing 14.7. It already includes an operator + method to add two TimeSpan objects together and assign the result to a TimeSpan reference variable. Your colleague decides to include an operator - method so that TimeSpan objects can be subtracted. The following is the code he initially inserted into the TimeSpan class. It contains several flaws. Find them.

 private TimeSpan   (TimeSpan timeSpan1, TimeSpan timeSpan2) {     return (timeSpan1.Seconds   timeSpan2.Seconds) } 

Among other program elements, your program contains the two classes EarthTimeSpan and BliposTimeSpan. Each class still contains an instance variable called totalSeconds of type uint. In this case, EarthTimeSpan contains a user-defined explicit conversion to the type short, and BliposTimeSpan contains a user-defined implicit conversion from the type decimal to BliposTimeSpan. myShort is a variable of type short and myDecimal is a variable of type decimal, earthTimeSpan1 is of type EarthTimeSpan and bliposTimeSpan1 is of type BliposTimeSpan. Determine whether each of the following statements is valid:

  1. myDecimal = bliposTimeSpan1;

  2. myDecimal = (decimal) bliposTimeSpan1;

  3. myShort = (short) bliposTimeSpan1;

  4. myShort = earthTimeSpan1;

  5. myShort = (short)earthTimeSpan;

  6. myDecimal = earthTimeSpan;

  7. myDecimal = (decimal)earthTimeSpan;

  8. bliposTimeSpan1 = earthTimeSpan1;

  9. bliposTimeSpan1 = (short)earthTimeSpan1;

  10. bliposTimeSpan1 = (decimal)earthTimeSpan1;

  11. earthTimeSpan1 = bliposTimeSpan1;

  12. earthTimeSpan1 = (short)bliposTimeSpan1;

  13. earthTimeSpan1 = (decimal)bliposTimeSpan1;


You are writing a program that will assist architects in drawing architectural plans. The program contains many classes that rely on manipulating two-dimensional drawings. One of these classes is called Bathroom, and it represents and manipulates drawings of bathrooms. It needs a class called Point to represent a point consisting of two coordinates. Would it be a good idea to nest this class in the Bathroom class? Why or why not?


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

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: