Review Questions



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.


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?


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


Are constructors only used to initialize instance variables?


What is the return type of a constructor?

  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?

  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?


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

 Cat myCat = new Cat(); 

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


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?


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) 

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?


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


What are the two main tasks of the garbage collector?


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


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


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?


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


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


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata © 2008-2017.
If you may any questions please contact us: