8.5 Object-oriented design

8.5 Object-oriented design: 

 

Analysis to Design

What is the primary goal of analysis in object-oriented design?

A) To identify and understand the requirements of the system

B) To create a detailed design blueprint

C) To implement the system

D) To test the system

 

Answer: A) To identify and understand the requirements of the system

 

Explanation: Analysis phase focuses on gathering requirements, understanding user needs, and defining the problem domain before moving on to design.

 

Which of the following is NOT a key activity in the design phase of object-oriented design?

A) Class and object identification

B) Implementation of algorithms

C) Designing relationships between classes

D) Refinement of user requirements

 

Answer: D) Refinement of user requirements

 

Explanation: Design phase involves tasks like identifying classes, defining relationships, designing algorithms, and creating a detailed system structure based on the analysis.

In object-oriented design, what does the term "encapsulation" refer to?

A) Hiding the implementation details of a class

B) Establishing relationships between classes

C) Grouping related data and behaviors into a single unit

D) Inheritance of attributes and methods

 

Answer: C) Grouping related data and behaviors into a single unit

 

Explanation: Encapsulation is the bundling of data and methods that operate on the data into a single unit, often referred to as a class.

 

Which design principle promotes the reusability of code by allowing new classes to inherit properties and behaviors from existing classes?

A) Encapsulation

B) Polymorphism

C) Inheritance

D) Abstraction

 

Answer: C) Inheritance

 

Explanation: Inheritance allows new classes to be based on existing classes, inheriting their attributes and behaviors, thereby promoting code reuse.

What is the purpose of a class diagram in object-oriented design?

A) To represent the interaction between objects at runtime

B) To depict the structure of the system in terms of classes and their relationships

C) To show the sequence of operations in a system

D) To model the flow of control within the system

 

Answer: B) To depict the structure of the system in terms of classes and their relationships

 

Explanation: Class diagrams provide a visual representation of the system's structure, including classes, attributes, methods, and relationships between classes.

Which design principle suggests that objects should be open for extension but closed for modification?

A) Liskov Substitution Principle (LSP)

B) Single Responsibility Principle (SRP)

C) Open/Closed Principle (OCP)

D) Dependency Inversion Principle (DIP)

 

Answer: C) Open/Closed Principle (OCP)

 

Explanation: The Open/Closed Principle states that software entities (classes, modules, functions, etc.) should be open for extension but closed for modification, meaning that their behavior can be extended without modifying their source code.

What does the term "polymorphism" mean in object-oriented design?

A) The ability to hide the implementation details of a class

B) The ability of a single interface to be used to reference objects of different classes

C) The process of bundling data and behaviors into a single unit

D) The reuse of existing code through inheritance

 

Answer: B) The ability of a single interface to be used to reference objects of different classes

 

Explanation: Polymorphism allows objects of different classes to be treated as objects of a common superclass, enabling flexibility and extensibility in code.

Which diagram is used to illustrate the dynamic behavior of a system in object-oriented design?

A) Class diagram

B) Sequence diagram

C) State diagram

D) Component diagram

 

Answer: B) Sequence diagram

 

Explanation: Sequence diagrams depict the interactions between objects in a particular scenario or use case, showing the sequence of messages exchanged between objects.

What is the purpose of the "constructor" method in object-oriented design?

A) To create a new instance of a class

B) To destroy an instance of a class

C) To update the attributes of a class

D) To define a subclass

 

Answer: A) To create a new instance of a class

Explanation: Constructors are special methods used for initializing newly created objects, setting initial values for their attributes, and preparing them for use.

Which design principle suggests that a class should have only one reason to change?

A) Open/Closed Principle (OCP)

B) Single Responsibility Principle (SRP)

C) Liskov Substitution Principle (LSP)

D) Interface Segregation Principle (ISP)

 

Answer: B) Single Responsibility Principle (SRP)

 

Explanation: The Single Responsibility Principle states that a class should have only one reason to change, meaning that it should have only one responsibility or job within the system. This promotes better maintainability and flexibility in the codebase.


 

Describing and Elaborating Use Cases

What is the primary purpose of describing use cases in object-oriented design?

A) To outline the implementation details of the system

B) To provide a high-level view of system functionality from an end-user perspective

C) To define the internal structure of classes and objects

D) To specify the interactions between objects during runtime

 

Answer: B) To provide a high-level view of system functionality from an end-user perspective

 

Explanation: Use cases describe the interactions between actors (users) and the system to achieve specific goals or tasks, focusing on the system's behavior from an external viewpoint.

Which of the following is NOT typically included in a use case description?

A) Preconditions

B) Postconditions

C) Class hierarchy

D) Flow of events

 

Answer: C) Class hierarchy

 

Explanation: Use case descriptions typically include preconditions, postconditions, and the flow of events but do not typically involve class hierarchy details, which are more relevant to design rather than use case descriptions.

What does a postcondition specify in the context of a use case?

A) The actions performed by the system

B) The conditions that must be true after the successful completion of the use case

C) The conditions that must be true before the use case can be executed

D) The interactions between different actors

 

Answer: B) The conditions that must be true after the successful completion of the use case

 

Explanation: Postconditions define the state of the system or the environment after the successful execution of the use case.

Which of the following is a key benefit of elaborating use cases in object-oriented design?

A) Providing detailed class diagrams

B) Facilitating communication between stakeholders

C) Optimizing code implementation

D) Specifying low-level system architecture

 

Answer: B) Facilitating communication between stakeholders

 

Explanation: Elaborating use cases helps in clarifying requirements and expectations among stakeholders, ensuring a shared understanding of system functionality and behavior.

What is an "actor" in the context of a use case?

A) An object within the system

B) A class representing a specific entity

C) A user or system component interacting with the system

D) A method that performs an action

 

Answer: C) A user or system component interacting with the system

 

Explanation: Actors are external entities, such as users or other systems, that interact with the system being designed to accomplish a goal or task.

Which section of a use case description typically describes the normal flow of events?

A) Preconditions

B) Postconditions

C) Alternative flows

D) Basic flow

 

Answer: D) Basic flow

 

Explanation: The basic flow section of a use case description outlines the primary sequence of steps or interactions between the actor(s) and the system to achieve the main goal of the use case.

What purpose does an alternative flow serve in a use case description?

A) Describing exceptional or alternative scenarios

B) Specifying the starting state of the system

C) Identifying the primary actor

D) Defining the final outcome of the use case

 

Answer: A) Describing exceptional or alternative scenarios

 

Explanation: Alternative flows describe deviations from the main or basic flow of events, typically representing exceptional or alternative scenarios that may occur during the execution of the use case.

What is the role of preconditions in a use case description?

A) To define the actions performed by the system

B) To specify the conditions that must be true before the use case can be executed

C) To outline the interactions between different actors

D) To describe the final state of the system

Answer: B) To specify the conditions that must be true before the use case can be executed

 

Explanation: Preconditions specify the conditions or requirements that must be met before the use case can be initiated or executed.

How do use cases contribute to the overall object-oriented design process?

A) By providing detailed implementation guidelines

B) By identifying and describing system requirements from a user's perspective

C) By defining the internal structure of classes and objects

D) By specifying low-level system architecture

 

Answer: B) By identifying and describing system requirements from a user's perspective

 

Explanation: Use cases play a crucial role in identifying and describing system requirements and functionality from an end-user perspective, serving as a basis for designing the system's structure and behavior.

What technique is commonly used to document use case descriptions in object-oriented design?

A) Unified Modeling Language (UML)

B) Structured Query Language (SQL)

C) Hypertext Markup Language (HTML)

D) JavaScript Object Notation (JSON)

 

Answer: A) Unified Modeling Language (UML)

 

Explanation: UML is a standard notation for modeling object-oriented systems, including use cases, classes, relationships, and other aspects of software design. Use case diagrams and descriptions are commonly used in UML to document system requirements and behavior.


 

Collaboration Diagram

What is the primary purpose of a collaboration diagram in object-oriented design?

A) To depict the interactions between objects within a single class

B) To illustrate the interactions between objects to accomplish a specific task or scenario

C) To represent the inheritance hierarchy of classes

D) To demonstrate the internal structure of a class

 

Answer: B) To illustrate the interactions between objects to accomplish a specific task or scenario

 

Explanation: Collaboration diagrams, also known as communication diagrams, depict the interactions between objects to achieve a particular goal or scenario, showing the flow of messages between objects.

In a collaboration diagram, what do the lines with arrows represent?

A) Class inheritance relationships

B) Data flow between objects

C) Method calls or messages sent between objects

D) Object creation instances

 

Answer: C) Method calls or messages sent between objects

 

Explanation: Lines with arrows in collaboration diagrams represent method calls or messages exchanged between objects, indicating the flow of communication during the execution of a scenario.

Which notation is commonly used to represent objects in a collaboration diagram?

A) Rectangles

B) Circles

C) Diamonds

D) Boxes

 

Answer: B) Circles

 

Explanation: Objects are typically represented by circles in collaboration diagrams, with the object name written inside the circle.

In a collaboration diagram, what does a lifeline represent?

A) The lifespan of an object during the execution of a scenario

B) The relationships between classes

C) The attributes of an object

D) The method calls made by an object

 

Answer: A) The lifespan of an object during the execution of a scenario

 

Explanation: A lifeline in a collaboration diagram represents the lifespan of an object, indicating the time period during which the object exists and participates in interactions within the scenario.

What does a number beside a message arrow in a collaboration diagram indicate?

A) The priority of the message

B) The order of message execution

C) The number of objects involved in the interaction

D) The size of the message

 

Answer: B) The order of message execution

 

Explanation: Numbers beside message arrows in collaboration diagrams indicate the order of message execution, specifying the sequence in which messages are sent between objects.

Which of the following is NOT typically included in a collaboration diagram?

A) Object names

B) Method signatures

C) Class attributes

D) Message arrows

 

Answer: C) Class attributes

 

Explanation: Collaboration diagrams focus on illustrating interactions between objects rather than detailing class attributes, which are more commonly represented in class diagrams.

What is the significance of the object creation message in a collaboration diagram?

A) It indicates the deletion of an object

B) It specifies the starting point of the scenario

C) It represents the creation of a new object instance

D) It marks the end of the scenario

 

Answer: C) It represents the creation of a new object instance

 

Explanation: An object creation message in a collaboration diagram indicates the creation of a new object instance, often denoted by a message arrow with a "create" or "new" label.

In a collaboration diagram, what does a return message arrow indicate?

A) The termination of an object

B) The completion of a method call and the return of a value to the calling object

C) The deletion of a message

D) The inheritance relationship between objects

 

Answer: B) The completion of a method call and the return of a value to the calling object

 

Explanation: A return message arrow in a collaboration diagram indicates the completion of a method call and the return of a value from the called object to the calling object.

What role does a self-message play in a collaboration diagram?

A) It indicates an object sending a message to itself

B) It represents a message exchanged between two different objects

C) It signifies the termination of an object

D) It denotes the creation of a new object

 

Answer: A) It indicates an object sending a message to itself

 

Explanation: A self-message in a collaboration diagram represents an object sending a message to itself, often used to illustrate internal processing or method invocation within an object.

How does a collaboration diagram differ from a sequence diagram?

A) Collaboration diagrams focus on static relationships, while sequence diagrams focus on dynamic interactions.

B) Collaboration diagrams depict interactions between classes, while sequence diagrams depict interactions between objects.

C) Collaboration diagrams use rectangles to represent objects, while sequence diagrams use circles.

D) Collaboration diagrams show the inheritance hierarchy, while sequence diagrams show the flow of control.

 

Answer: A) Collaboration diagrams focus on static relationships, while sequence diagrams focus on dynamic interactions.

 

Explanation: Collaboration diagrams emphasize the static relationships between objects and their interactions to achieve a particular scenario, while sequence diagrams focus on the dynamic sequence of messages exchanged between objects during runtime.

 

Objects and Patterns

What is the primary purpose of objects in object-oriented design?

A) To represent real-world entities and their interactions

B) To define the structure and behavior of the system

C) To optimize code execution

D) To abstract away implementation details

 

Answer: A) To represent real-world entities and their interactions

 

Explanation: Objects in object-oriented design model real-world entities or concepts and their interactions within the system, providing a natural and intuitive way to represent and manipulate data and behavior.

In object-oriented design, what is a design pattern?

A) A predefined set of classes and objects

B) A template for generating code

C) A reusable solution to a common problem in software design

D) A diagram depicting the interactions between objects

 

Answer: C) A reusable solution to a common problem in software design

 

Explanation: Design patterns are reusable solutions to common problems encountered during software design, providing proven approaches to solving recurring design challenges.

Which design pattern is used to ensure that only one instance of a class is created and provides a global point of access to that instance?

A) Singleton pattern

B) Factory pattern

C) Observer pattern

D) Prototype pattern

 

Answer: A) Singleton pattern

 

Explanation: The Singleton pattern ensures that a class has only one instance and provides a global access point to that instance, commonly used for resource management or configuration settings.

What is the purpose of the Factory Method pattern in object-oriented design?

A) To provide a centralized registry for creating objects

B) To define an interface for creating an object, but allow subclasses to alter the type of objects that will be created

C) To represent a family of algorithms and make them interchangeable

D) To establish one-to-many relationships between objectsAnswer: B) To define an interface for creating an object, but allow subclasses to alter the type of objects that will be created

 

Explanation: The Factory Method pattern defines an interface for creating objects but allows subclasses to override the method to change the type of objects created, promoting flexibility and extensibility.

Which design pattern is used to represent a family of algorithms and make them interchangeable without client code modification?

A) Adapter pattern

B) Strategy pattern

C) Observer pattern

D) Decorator pattern

Answer: B) Strategy pattern

 

Explanation: The Strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable, allowing clients to vary algorithms independently from the context using them.

What does the Observer pattern facilitate in object-oriented design?

A) Dynamic instantiation of objects

B) One-to-many dependency relationships between objects

C) Lazy loading of objects

D) Inheritance of attributes and methods

 

Answer: B) One-to-many dependency relationships between objects

 

Explanation: The Observer pattern establishes one-to-many dependency relationships between objects, such that when one object changes state, all its dependents are notified and updated automatically.

Which design pattern is used to add new functionality to an existing object dynamically without altering its structure?

A) Adapter pattern

B) Decorator pattern

C) Singleton pattern

D) Composite pattern

 

Answer: B) Decorator pattern

 

Explanation: The Decorator pattern allows behavior to be added to individual objects dynamically without affecting the behavior of other objects of the same class, providing a flexible alternative to subclassing for extending functionality.

What is the primary purpose of the Adapter pattern in object-oriented design?

A) To convert the interface of a class into another interface that clients expect

B) To represent a family of algorithms and make them interchangeable

C) To ensure that only one instance of a class is created

D) To provide a global point of access to an instance

Answer: A) To convert the interface of a class into another interface that clients expect

 

Explanation: The Adapter pattern allows incompatible interfaces to work together by converting the interface of a class into another interface that clients expect, enabling objects with different interfaces to collaborate seamlessly.

Which design pattern is used to compose objects into tree structures to represent part-whole hierarchies?

A) Composite pattern

B) Iterator pattern

C) Proxy pattern

D) Builder pattern

 

Answer: A) Composite pattern

 

Explanation: The Composite pattern allows objects to be composed into tree structures to represent part-whole hierarchies, treating individual objects and compositions of objects uniformly.

What is the purpose of the Prototype pattern in object-oriented design?

A) To provide a global point of access to an instance

B) To define an interface for creating an object, but allow subclasses to alter the type of objects that will be created

C) To ensure that only one instance of a class is created

D) To specify the kinds of objects to create using a prototypical instance

 

Answer: D) To specify the kinds of objects to create using a prototypical instance

 

Explanation: The Prototype pattern specifies the kinds of objects to create using a prototypical instance and creates new objects by copying this prototype, avoiding the need for explicit instantiation and enabling cloning of objects.

 

Determining Visibility

In object-oriented design, what does visibility refer to?

A) The physical appearance of objects on the screen

B) The accessibility of class members (attributes and methods) from outside the class

C) The arrangement of objects within a class diagram

D) The classification of objects into different categories

 

Answer: B) The accessibility of class members (attributes and methods) from outside the class

 

Explanation: Visibility in object-oriented design determines whether class members can be accessed from outside the class, controlling the level of encapsulation and information hiding.

Which visibility modifier in Java allows class members to be accessed by any other class in the same package?

A) private

B) protected

C) public

D) package-private (default)

 

Answer: D) package-private (default)

 

Explanation: In Java, package-private (default) visibility allows class members to be accessed by any other class in the same package but not from classes in different packages.

What does the "private" visibility modifier signify in object-oriented design?

A) The class member is accessible only within the same class

B) The class member is accessible by any other class in the same package

C) The class member is accessible by subclasses

D) The class member is accessible by any class

Answer: A) The class member is accessible only within the same class

 

Explanation: Private visibility restricts access to class members to within the same class, ensuring that they cannot be accessed or modified directly by other classes.

In object-oriented design, what does the "protected" visibility modifier indicate?

A) The class member is accessible only within the same class

B) The class member is accessible by any other class in the same package

C) The class member is accessible by subclasses and classes in the same package

D) The class member is accessible by any class

 

Answer: C) The class member is accessible by subclasses and classes in the same package

 

Explanation: Protected visibility allows class members to be accessed by subclasses and classes in the same package, but not by classes in different packages.

Which visibility modifier in Java allows class members to be accessed from any other class?

A) private

B) protected

C) public

D) package-private (default)

 

Answer: C) public

 

Explanation: Public visibility allows class members to be accessed from any other class, regardless of the package in which they reside.

What is the default visibility modifier for class members in Java if no visibility modifier is specified?

A) private

B) protected

C) public

D) package-private

 

Answer: D) package-private

 

Explanation: If no visibility modifier is specified in Java, the default visibility is package-private, meaning the class member is accessible within the same package but not from outside the package.

Which of the following statements about visibility in object-oriented design is true?

A) Private members can be accessed by any class in the same package.

B) Public members can only be accessed by subclasses.

C) Protected members are accessible from any class.

D) Package-private members are accessible from any package.

 

Answer: C) Protected members are accessible from any class.

 

Explanation: Protected members are accessible by subclasses and classes in the same package, regardless of the package in which they reside.

What is the main advantage of using encapsulation in object-oriented design?

A) Encapsulation allows for easy inheritance of class members.

B) Encapsulation simplifies class diagrams.

C) Encapsulation improves code readability and maintainability.

D) Encapsulation increases the visibility of class members.

 

Answer: C) Encapsulation improves code readability and maintainability.

 

Explanation: Encapsulation hides the internal state of objects and restricts access to class members, promoting better code organization, readability, and maintainability.

Which visibility modifier is commonly used for class constructors in Java to allow instantiation from any other class?

A) private

B) protected

C) public

D) package-private

 

Answer: C) public

 

Explanation: Public constructors in Java allow classes to be instantiated from any other class, enabling objects of that class to be created from anywhere in the program.

In object-oriented design, why is it important to carefully determine the visibility of class members?

A) It simplifies the design process.

B) It ensures efficient memory management.

C) It promotes encapsulation and information hiding.

D) It allows for easy inheritance of class members.

 

Answer: C) It promotes encapsulation and information hiding.

 

Explanation: Determining the visibility of class members is crucial for promoting encapsulation and information hiding, ensuring that the internal state of objects is protected and only accessible through well-defined interfaces, improving code quality and maintainability.

 

Class Diagram

What is the primary purpose of a class diagram in object-oriented design?

A) To illustrate the dynamic behavior of a system

B) To depict the interactions between objects in a specific scenario

C) To represent the static structure of a system in terms of classes and their relationships

D) To specify the sequence of operations within a method

 

Answer: C) To represent the static structure of a system in terms of classes and their relationships

 

Explanation: Class diagrams provide a visual representation of the static structure of a system, including classes, attributes, methods, and relationships between classes.

In a class diagram, what does an association line represent?

A) Inheritance relationship between classes

B) Composition relationship between classes

C) Dependency relationship between classes

D) Aggregation relationship between classes

 

Answer: C) Dependency relationship between classes

 

Explanation: An association line in a class diagram represents a dependency relationship between classes, indicating that one class depends on another in some way.

What does a diamond symbol at the end of an association line in a class diagram represent?

A) Aggregation relationship

B) Composition relationship

C) Inheritance relationship

D) Multiplicity

 

Answer: D) Multiplicity

 

Explanation: A diamond symbol at the end of an association line in a class diagram indicates multiplicity, specifying the number of instances of one class related to an instance of another class.

In a class diagram, what does an arrowhead at the end of an association line indicate?

A) Unidirectional association

B) Bidirectional association

C) Composition relationship

D) Inheritance relationship

 

Answer: A) Unidirectional association

 

Explanation: An arrowhead at the end of an association line in a class diagram indicates a unidirectional association, showing that the relationship is navigable in one direction only.

Which of the following relationships in a class diagram implies a stronger "part-of" relationship?

A) Aggregation

B) Composition

C) Inheritance

D) Dependency

 

Answer: B) Composition

Explanation: Composition in a class diagram implies a stronger "part-of" relationship than aggregation, indicating that one class (the whole) is composed of other classes (the parts), and the parts cannot exist independently of the whole.

What does a dashed line with an arrowhead represent in a class diagram?

A) Generalization relationship

B) Composition relationship

C) Association relationship

D) Dependency relationship

 

Answer: A) Generalization relationship

 

Explanation: A dashed line with an arrowhead in a class diagram represents a generalization relationship, indicating inheritance where one class is a specialization of another (subclass/superclass relationship).

In a class diagram, what does a multiplicity of "1..*" represent?

A) One-to-one relationship

B) One-to-many relationship

C) Many-to-one relationship

D) Many-to-many relationship

 

Answer: B) One-to-many relationship

 

Explanation: A multiplicity of "1..*" in a class diagram indicates a one-to-many relationship, where one instance of one class is associated with multiple instances of another class.

Which UML symbol is used to represent a class in a class diagram?

A) Rectangle

B) Circle

C) Diamond

D) Triangle

 

Answer: A) Rectangle

 

Explanation: A rectangle is used to represent a class in a class diagram, with the class name written inside the rectangle, along with its attributes and methods.

What is the purpose of a stereotype in a class diagram?

A) To specify the number of instances involved in a relationship

B) To indicate special characteristics or roles of a class

C) To define the sequence of operations within a method

D) To represent a dependency relationship between classes

 

Answer: B) To indicate special characteristics or roles of a class

 

Explanation: Stereotypes in a class diagram are used to indicate special characteristics or roles of a class, providing additional information about the class beyond its basic structure.

What does a solid line with an arrowhead between two classes in a class diagram represent?

A) Inheritance relationship

B) Composition relationship

C) Association relationship

D) Aggregation relationship

 

Answer: C) Association relationship

 

Explanation: A solid line with an arrowhead between two classes in a class diagram represents an association relationship, indicating that there is a connection or relationship between the classes, but the relationship is not as strong as composition or inheritance.