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 ...

Get C# Primer Plus now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.