Review Questions

   


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.

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?

3:

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

4:

Are constructors only used to initialize instance variables?

5:

What is the return type of a constructor?

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?

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?

8:

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

 Cat myCat = new Cat(); 
9:

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

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?

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) 
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?

13:

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

14:

What are the two main tasks of the garbage collector?

15:

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

16:

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

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?

18:

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

19:

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

Similar book on Amazon

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