Unit 3 Flashcards
-
What is an association in object-oriented programming?
- a) A collection of classes needed for a database
- b) A generalization relationship between two objects
- c) A link between two classes that allows them to communicate through their instances
- d) A dependency relationship required for program compilation
Correct Answer: c) A link between two classes that allows them to communicate through their instances
-
In the context of Java, what does aggregation represent?
- a) A dynamic method dispatch mechanism
- b) A whole-part relationship between objects
- c) An array of objects stored within another object
- d) A static class method that operates on global data
Correct Answer: b) A whole-part relationship between objects
-
How is a composition relationship different from aggregation?
- a) Composition implies a weaker relationship where the lifecycle of the parts is not managed by the whole.
- b) In composition, objects can exist independently of the whole, whereas in aggregation they cannot.
- c) Composition implies a stronger relationship where the parts do not exist independently of the whole.
- d) There is no practical difference in code, only theoretical.
Correct Answer: c) Composition implies a stronger relationship where the parts do not exist independently of the whole.
-
What does it mean if a class A has a unidirectional association with class B in Java?
- a) Class A can contain and control instances of Class B, but not vice versa.
- b) Class B can contain and control instances of Class A, but not vice versa.
- c) Instances of Class A can call methods on instances of Class B only.
- d) Both classes can contain each other.
Correct Answer: c) Instances of Class A can call methods on instances of Class B only.
-
In a UML diagram, how is a bidirectional association represented compared to a unidirectional association?
- a) Bidirectional is shown with a line having arrowheads on both ends, whereas unidirectional has only one.
- b) Unidirectional is shown with a dotted line, while bidirectional uses a solid line.
- c) Unidirectional associations are not represented in UML.
- d) Bidirectional is shown with a single arrow; unidirectional uses a line without arrows.
Correct Answer: a) Bidirectional is shown with a line having arrowheads on both ends, whereas unidirectional has only one.
-
What is multiplicity in the context of associations in UML diagrams?
- a) It specifies the abstract classes involved in the association.
- b) It specifies the number of instances of one class that can be associated with one instance of another class.
- c) It determines the methods that can be called on the associated objects.
- d) It indicates the maximum number of methods that can be invoked.
Correct Answer: b) It specifies the number of instances of one class that can be associated with one instance of another class.
-
How would you implement a “1 to many” relationship in Java code?
- a) By using a single instance variable of type List in the class on the “1” side.
- b) By creating an array of objects on the “many” side.
- c) By declaring static variables in both classes.
- d) By using a HashMap to manage multiple instances with unique keys.
Correct Answer: a) By using a single instance variable of type List in the class on the “1” side.
-
What is a wrapper class in Java?
- a) A class that contains static methods to operate on primitives.
- b) A class that ‘wraps’ primitive types in an object so they can be included in activities reserved for objects.
- c) Any class that implements the Serializable interface.
- d) A class that serves to combine several classes into one compound class.
Correct Answer: b) A class that ‘wraps’ primitive types in an object so they can be included in activities reserved for objects.
-
Which of the following best describes the role of the
Integer
class in Java?- a) To provide a way to use int values as method parameters where Objects are expected.
- b) To extend the functionality of the int data type.
- c) To encapsulate the complexity of number management in Java.
- d) To serve as the default type for integer values in Java.
Correct Answer: a) To provide a way to use int values as method parameters where Objects are expected.
-
When are objects of wrapper classes, like
Integer
andDouble
, necessary?- a) When there’s a need to store primitive data types in parameterized classes, such as
ArrayList
. - b) When the program requires explicit casting between different object types.
- c) When large numbers of primitive types would cause memory overflow without wrappers.
- d) When the primitives need to be converted to objects for serialization.
- a) When there’s a need to store primitive data types in parameterized classes, such as
Correct Answer: a) When there’s a need to store primitive data types in parameterized classes, such as ArrayList.