Answers to Chapter 13 Review Questions

   


Chapter 13

1:

A class called Robot contains the following two instance variable declarations:

 private ushort age; private bool isConnected; 

What is the value of age and isConnected immediately after their object has been instantiated?

Improve the style of the declarations without changing the semantics of the code.

A:

age has the value 0 and isConnected the value false.

Improved code would be as follows:

 private ushort age = 0; private bool isConnected = false; 
2:

The users of your Robot class would like to assign a value to age at the same time as they are instantiating a Robot object. How would you accommodate for this request?

A:

By equipping the Robot class with the following constructor:

 public Robot(ushort initAge) {     age = initAge; } 
3:

What constructor names can you use for a class called Robot?

A:

All constructors of the class Robot must be called Robot.

4:

Are constructors only used to initialize instance variables?

A:

No, they can be used to perform any actions necessary when a new object is created.

5:

What is the return type of a constructor?

A:

There is no return value, so there is no return type.

6:
  1. A Dog class contains no constructor definitions in the source code. Is the following statement (found inside a method of another class) valid?

     Dog myDog = new Dog(); 
  2. You decide to include a constructor with the following header in the Dog class:

     public Dog(int initialAge) 

    Is the previous statement valid now? Why or why not?

A:
  1. When no constructors are explicitly defined for a class, the compiler automatically includes a default constructor for this class, making the call valid.

  2. The previous statement is not valid now because the compiler does not automatically include a default constructor when any explicit constructors are specified for a class.

7:
  1. You find the following two constructor headers in a class called Cat:

     public Cat(short initialAge, string initialName) : this (initialAge) public Cat(short initialAge) 

    Are these headers valid? If so, what do they mean? What is : this (initialAge) used for?

  2. A fellow programmer changes those headings to become

     public Cat() : this () 

    Is this header valid? Why or why not?

A:
  1. Yes, both headers are valid. : this (initialAge) is a constructor initializer, which in this case causes the constructor with the header public Cat(short initialAge) to be called before the statements residing in the constructor to which the constructor initializer is attached.

  2. This header is not valid because the constructor initializer is calling the constructor to which it is attached. The result is an infinite number of calls to the constructor.

8:

The following line creates a new Cat object. What do the parentheses after Cat signify?

 Cat myCat = new Cat(); 
A:

The parentheses signify that the method-like language element called a constructor is being called.

9:

How can overloaded constructors make a class more flexible to use?

A:

Overloaded constructors contain different formal parameters, so a class with several constructors accepts several different combinations of arguments when a new object is created.

10:

Why would you ever want a constructor that cannot be called from outside its class? How do you declare this kind of constructor? What is it called?

A:

A constructor that is declared private cannot be called from outside its class. If a class only contains private constructors, a class cannot be instantiated from outside the class. This technique is sometimes used for classes containing only static members.

11:

When is a static constructor called?

  1. From where is a static constructor called? When is a static constructor called?

  2. What's wrong with the following header of a static constructor?

     static Cat (int initialNumberOfCats) 
A:
  1. A static constructor is called by the runtime sometime between program startup and the first instance of the class for which it is designed is created.

  2. A static constructor cannot contain any parameters because the runtime never provides any arguments when it calls the static constructor.

12:

Could you design a program that allows the end user to assign a value to a:

  1. Constant instance member?

  2. A readonly instance member?

Hint: What is the lifetime of a constant member? What is the lifetime of a readonly instance member?

A:
  1. No. The constant instance member value is decided before the program is compiled at the time when the program is written so the programmer must know its value. A constant member cannot change value after its program has been compiled, and it is not possible to change its value until the next time the program is compiled.

  2. Yes. A readonly instance member has its value determined at the time the object is created, after which it cannot be altered. A readonly instance member has the same lifetime as the object in which it resides.

13:

Briefly explain what it means for an object to be out of reach.

A:

An object is out of reach when no reference variables are referencing the object.

14:

What are the two main tasks of the garbage collector?

A:

To identify objects that are out of reach. To reclaim the memory allocated for objects that are out of reach.

15:

Why does the garbage collector not just garbage collect any object that goes out of reach immediately?

A:

This is an inefficient approach. A better approach is to collect a group of reclaimable objects and then process them all at once.

16:

What is a destructor? Why is it not useful for freeing up scarce non-memory resources?

A:

A destructor is defined explicitly for a class, in which case it contains statements written by a programmer. The destructor can only be called by the garbage collector during its collection of an unreachable object.

The destructor is not useful to free up scarce non-memory resources because we don't know when it is called; in fact, it may never be called during the runtime of a program.

17:

A program contains a class called Book that contains 10 instance variables all of a simple type. During the execution of the program, 50 Book objects are created and most of them are destroyed. Is the garbage collector likely to run during the execution of your code?

A:

No.

18:

Why should you be careful when using C# (at this time of writing) for real time applications?

A:

When the garbage collector is activated, the rest of the program is frozen, so a real-time application is not on real time during this period. When we combine this fact with the inability to control when the garbage collector is running, it becomes difficult to write a real-time application with a GC-based programming language like C#.

19:

Briefly explain why it is a good idea to use the Dispose design pattern to free scarce non-memory resources.

A:

The dispose design pattern gives full control to the programmer of when to dispose of scarce resources held by an object.

Answers to Chapter 13 Programming Exercises

1:

Write a class called Robot with the following three instance variables: name (of type string), age (of type ushort), and isOn (of type bool). Make the program initialize these three instance variables to "unknown", 0, and false, respectively (without using constructors). Include accessor and mutato methods to assign and retrieve the values of these instance variables.

Write a Main method that tests the Robot class.

A:

Exercise 1:

 class Robot {     private string name = "unknown";     private ushort age = 0;     private bool isOn = false;     //Accessor and mutator methods } 
2:

Allow the users of the Robot class to set the initial values of the three instance variables when a Robot object is created. To this end, declare one constructor with three formal parameters and one default constructor.

Include a member variable called robotsCreated that keeps track of the number of Robot objects created. Make the constructors update robotsCreated so this variable is always up-to-date. Implement the following logic in the default constructor: If robotsCreated is less than five when this constructor is called, set isOn to true; otherwise, set it to false.

A:

Exercises 2 and 3:

 class Robot {     private string name;     private ushort age;     private bool isOn;     private static int robotsCreated;     static Robot()     {         robotsCreated = 0;     }     public Robot(string initName, ushort initAge, bool initIsOn)     {         name = initName;         age = initAge;         isOn = initIsOn;         robotsCreated++;     }     public Robot()     {         name = "unknown";         age = 0;         robotsCreated++;         if(robotsCreated < 5)             isOn = true;         else             isOn = false;     }     //accessors and mutator methods } 
3:

The Robot is able to perform a few simple calculations. For example, it can find the average of three int numbers. This ability is provided through a method with the following header:

 public int Average (int x, int y, int z) 

This method does not itself perform the average calculation but uses a static method named Average contained inside a class called RobotMath. The RobotMath class only contains static methods and static variables and should never be instantiated. Ensure that RobotMath is never instantiated.

A:

Exercise 4:

 class Robot {     ...     public int Average(int x, int y, int z)     {         return RobotMath.Average(x, y, z);     }     ... } class RobotMath {      //private constructor prevents any instances to be created from      //outside the RobotMath class.     private RobotMath()     {     }     public int Average(int x, int y, int z)     {         return (x + y + z) / 3;     } } 


   


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