Saturday, May 22, 2010

Which of the following statements about classes is true?

a. Every class belongs to an object.


b. The new operator is used to construct a new class.


c. Classes are factories or blueprints for objects.


d. The public interface of a class is hidden from the user.


e. Classes are examples of objects.

Which of the following statements about classes is true?
Well, that would be C.





Objects belong to classes. new is used to create a new object. The user must be able to see the public interface. Objects are examples of classes.
Reply:A class is a definition (or blueprint) for objects. So every object is created from a class via the "new" operator.
Reply:Which of the following statements about classes is true?





a. Every class belongs to an object.





-- No, every object belongs to a class.





b. The new operator is used to construct a new class.





-- It *can* be, in some languages (eg Smalltalk), where a class *is* an object. Not so in C++, so the answer is "No".





c. Classes are factories or blueprints for objects.





-- Yes, a class is a "blueprint" for an object. "new" is the factory (strictly speaking), but it could be viewed that way. So the answer is "Yes".





d. The public interface of a class is hidden from the user.





-- No, the public interface is the ONLY thing that is NOT hidden.





e. Classes are examples of objects.





-- See the answer for "b". In Smalltalk, classes ARE examples of objects. In most other OOP systems they are not.
Reply:WRONG!





Classes are examples of Objects!





(a) is not true in some cases, for example:





public class myClass{





}





(b) The new operator is used to declare a new instance of a class.





(c) This statement is equivalent to (e), thus true.





(d) The public interface must be visible from outside the class, if not how can you use it?





(e) Classes are examples of objects. An object can also be a button, scroll bar, window, text box, event handler, error handler, files...








Answers are (e) and (c)


No comments:

Post a Comment