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: |
|
A: |
|
7: |
|
A: |
|
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?
|
A: |
|
12: | Could you design a program that allows the end user to assign a value to a:
Hint: What is the lifetime of a constant member? What is the lifetime of a readonly instance member? |
A: |
|
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. |
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; } } |