Classes are user-defined data types - Quiz Explanation

The answers you selected are indicated below, along with text that explains the correct answers.

 
1. Consider a Teacher class. Which of the following statements about this class is most true?
Please select the best answer.
  A. John Smith is an attribute of the Teacher class.
  B. The Teacher class is an attribute of John Smith.
  C. High School Teacher is an instance of the Teacher class.
  D. John Smith is an instance of the Teacher class.
  The correct answer is D. Each object (teacher John Smith) is an instance of its class. A is incorrect because it states that all teachers will have a John Smith attribute, and no meaningful value is possible for such an attribute. B is incorrect because an attribute may be an object, but it may not be a class. C is incorrect because High School Teacher is another class, not an instance of the Teacher class.

2. What is the largest group of data types that you can choose from to specify the attributes of a class?
Please select the best answer.
  A. Numbers or strings only
  B. Numbers, strings, and built-in data types of a language only
  C. Positive numbers, strings, and built-in data types of a language only
  D. Numbers, strings, built-in data types of a language, and user-defined objects in other classes
  The correct answer is D. There is no restriction on the types that can be used for attributes: user defined types (in other words, objects in other classes), built-in types such as date, numbers, strings, and objects reused from some other project are all fine as types for your attributes. Answers A and B are valid for attributes, but do not represent all valid attribute data types. Answer C is incorrect because the value of an object's attribute is not part of the selection of data type.

3. Consider a ReportCard class. Which one of these design requirements would be best handled through class composition?
Please select the best answer.
  A. The report card will be two pages long.
  B. The report card will show the student's name.
  C. The report card will show the teacher's name.
  D. The report card will show contact information for the school.
  The correct answer is D. The exact details of what is considered "contact information" do not matter to someone writing code to look up and report marks for each student. If in a later version of the program, the school's email address or Web site is added to the contact information, it should be added everywhere that uses contact information. B and C are incorrect because names do not have a lot of business information or rules: In fact, attempts by computers to enforce rules about names (no spaces in a last name, everyone must have a middle initial, and so on) are responsible for a lot of complaints about computer systems. A is incorrect because "first page of a report card" is not a well-designed class. There's no reason why some information would be in that class and some in the "second page of a report card" class.

4. Which of the statements below illustrates a proper use of an object diagram?
Please select the best answer.
  A. An object diagram helps everyone understands the types of the attributes of a class.
  B. An object diagram provides an example of values for all the attributes of a class.
  C. An object diagram demonstrates the business rules of each class.
  D. An object diagram lists the names of all the objects in the system.
  The correct answer is B. Not all projects include object diagrams. Where they do, it's as an example of values the attributes can hold, so that the whole team understands the class more fully. A is incorrect because a class diagram lists the types of attributes; object diagrams list the values. C is incorrect because business rules (such as "month cannot be greater than 12") are not shown on object diagrams. D is incorrect because an object diagram typically shows representative examples of the objects, not every one in the system.