Tutorial Questions
Week 1
Tutorial Sheet: Introduction to Object-Oriented Modeling
Multiple Choice Questions
What is Encapsulation in OOP? A. The process of inheriting properties from a class B. Combining data and functions that operate on them C. The process of creating multiple objects from a class D. A technique to optimize code performance
Which of the following is a principle of Object-Oriented Programming? A. Procedural Decomposition B. Encapsulation C. Linear Data Structures D. Parallel Computing
What is a Class in OOP? A. A template for creating objects B. A specific object created from a blueprint C. A function within an object D. A data type defined by the user
Choose the correct statement about an ‘Object’ in OOP. A. It is a fundamental data type B. It is a blueprint for creating classes C. It represents an entity with properties and behaviors D. It is a method within a class
What does ‘Inheritance’ allow in OOP? A. A class to pass its methods to an interface B. An object to inherit methods from another object C. A class to inherit properties and methods from another class D. A method to inherit properties from a class
Which of the following is an example of Polymorphism in OOP? A. A class with multiple methods B. A class inheriting from multiple classes C. Multiple classes having methods with the same name but different implementations D. A method changing data types during execution
What is an Abstract Class in OOP? A. A class that can be instantiated B. A class designed only to act as a parent class C. A class without any methods D. A class that contains only static methods
Choose the correct usage of an Interface in OOP. A. To create objects directly B. To define a set of methods that implementing classes must use C. To store data like a database D. To replace multiple inheritances
What is Method Overloading? A. Changing the return type of a method B. Providing multiple methods in a class with the same name but different parameters C. Deleting a method from a class D. Overriding methods of a parent class
Which OOP principle hides the internal state of an object and only exposes operations? A. Encapsulation B. Abstraction C. Inheritance D. Polymorphism
What does the term ‘Instantiation’ refer to in OOP? A. Creating a new variable B. Deleting an object C. Creating a new object from a class D. Defining a new class
In OOP, what is ‘Composition’? A. A function within an object B. A class that includes instances of other classes C. A set of instructions for a program D. The act of inheriting from multiple classes
What is the purpose of a Constructor in a class? A. To destroy an instance of a class B. To initialize a new object C. To perform computations D. To call methods of the parent class
Which concept in OOP refers to the ability of different classes to be treated as instances of the same class through inheritance? A. Encapsulation B. Polymorphism C. Abstraction D. Composition
What is ‘Method Overriding’ in OOP? A. Changing the functionality of an existing method in a subclass B. Creating new methods in a class C. Removing methods from a superclass D. Increasing the efficiency of a method
In OOP, what is a ‘Static Method’? A. A method that can change during runtime B. A method associated with an instance of a class C. A method that belongs to the class rather than any object of the class D. A method used for dynamic memory allocation
What is the main advantage of OOP? A. Faster code execution B. More efficient data processing C. Improved readability and reusability of code D. Reduced storage requirements
What is an ‘Interface’ in OOP? A. A type of class that allows multiple inheritances B. A blueprint of a class C. A collection of abstract methods D. A method used to interface with the operating system
Which of the following best describes ‘Aggregation’ in OOP? A. Objects having their lifecycle independent of the parent object B. The total memory consumed by an object C. Combining multiple objects into a single unit D. A class inheriting properties from multiple classes
What is an ‘Accessor Method’ in OOP? A. A method that modifies the value of a private member variable B. A method that initializes the properties of a class C. A method that provides read-only access to a private member variable D. A method used to access the methods of a superclass
What is the concept of ‘Coupling’ in software design? A. The degree of interdependence between software modules B. The process of linking two classes together C. The method of combining data and functions D. The act of creating new objects from a class
Choose the correct statement about ‘Loose Coupling’ in software design. A. It makes it easier to modify and maintain software B. It implies a high degree of interdependence between modules C. It is the process of combining multiple classes into one D. It refers to a class with multiple responsibilities
What is ‘Cohesion’ in software design? A. The process of inheriting properties from another class B. The degree to which elements of a module are functionally related C. The act of combining different classes into a single class D. The number of tasks a method performs
What does the ‘Single Responsibility Principle’ advocate? A. A class should have only one reason to change B. A class should be responsible for multiple functionalities C. A class should inherit from only one superclass D. A class should be designed to perform all tasks of a module
What is ‘UML’ in software engineering? A. Unified Markup Language B. Universal Methodology Language C. Unified Modeling Language D. Universal Modeling Language
In UML, what does a ‘Class Diagram’ represent? A. The sequence of operations in an application B. The relationships and dependencies among classes C. The physical deployment of a system D. The dynamic behavior of a system
What is ‘Composition’ in UML diagrams? A. A type of class relationship indicating inheritance B. A relationship where one class is part of another class C. A diagram showing the flow of control in a system D. A notation for representing multiple inheritances
Which UML diagram is used to model the dynamic aspects of a system? A. Class Diagram B. Sequence Diagram C. Component Diagram D. Deployment Diagram
In UML, what is an ‘Object Diagram’? A. A diagram that shows the objects in a system and their relationships B. A blueprint for creating classes C. A diagram showing the physical deployment of a system D. A representation of the static structure of a system
What is the purpose of a ‘Use Case Diagram’ in UML? A. To show the dynamic behavior of a system B. To illustrate the relationships between use cases and actors C. To represent the physical components of a system D. To display the inheritance among classes
Short Answer Questions
Explain the concept of ‘Inheritance’ in OOP with an example.
Describe the difference between an ‘Abstract Class’ and an ‘Interface’.
What is the significance of ‘Polymorphism’ in designing flexible software systems?
Explain how ‘Encapsulation’ enhances the security of data in an object.
Illustrate the concept of ‘Method Overloading’ with a code example.
Discuss the role of ‘Constructors’ in object initialization.
How does ‘Composition’ differ from ‘Inheritance’ in OOP?
Define ‘Method Overriding’ and provide a scenario where it is used.
Explain the importance of ‘Cohesion’ and ‘Coupling’ in software design.
Describe the role of UML in object-oriented design and modeling.
Week 2
Tutorial Sheet: Object Oriented Modelling and Design
Multiple Choice Questions
- What is the primary goal of Object-Oriented Analysis (OOA)?
- To create a conceptual solution that meets the requirements.
- To investigate the problem and identify objects and their interactions.
- To implement the design in a specific programming language.
- To evaluate the system’s performance under different conditions.
- What does Modularity refer to in Object-Oriented Analysis and Design?
- The ability to change the functionality of an application without impacting the user interface.
- The degree to which a system is composed of discrete components.
- The process of creating objects from classes.
- The ability of different classes to be treated as instances of the same class.
- In the context of software design, what does Low Coupling imply?
- High dependency between different modules of a system.
- Minimal impact on other components when a component is changed.
- The ability of a system to handle unexpected inputs gracefully.
- The degree to which a system can be used in more than one application.
- Which principle is best demonstrated by creating a class that does not represent a real-world entity but aids in design?
- Information Expert
- Polymorphism
- Pure Fabrication
- High Cohesion
- What does the term ‘Functional Requirements’ in software development refer to?
- Requirements that specify how the system should behave under certain conditions.
- Requirements that focus on the system’s operational aspects like performance and reliability.
- Requirements related to the system’s interaction with its environment.
- Requirements that define specific behaviors or functions of the system.
- In Object-Oriented Design, what is a ‘Use Case’ primarily used for?
- Defining the structure of different classes in a system.
- Describing how users interact with the system to achieve specific goals.
- Illustrating the relationships and dependencies among different classes.
- Outlining the sequence of messages between objects in a particular interaction.
- What is encapsulated within a Class Diagram in UML?
- The dynamic behavior of a system.
- The classes along with their attributes, operations, and relationships.
- The sequence of operations and their conditions.
- The physical deployment of the system components.
- Which principle advocates that a class should have only one reason to change?
- Open/Closed Principle
- Liskov Substitution Principle
- Single Responsibility Principle
- Dependency Inversion Principle
- What is an example of High Cohesion in software design?
- A class performing a wide range of unrelated tasks.
- A class with methods that are closely related in functionality.
- Multiple classes performing the same task.
- A class having minimal interaction with other classes.
- Which of the following is true about the Interface Segregation Principle?
- It suggests that interfaces should be designed for general-purpose use.
- It emphasizes the use of large, comprehensive interfaces.
- It encourages designing small, specific interfaces.
- It implies that interfaces should be implemented by as many classes as possible.
Short Answer Questions
- Explain the concept of ‘Aggregation’ in object-oriented design with an example.
- Describe the difference between ‘Functional Requirements’ and ‘Non-Functional Requirements’ in software development.
- Illustrate the concept of ‘Polymorphism’ with a code example in Java.
- Discuss the role and importance of ‘Use Cases’ in understanding user interactions with a system.
- Provide an example of a class that follows the Single Responsibility Principle.
- How does the Open/Closed Principle benefit software maintenance and extension?
- Explain the concept of ‘Low Coupling’ and why it is important in software design.
- Describe the ‘Principle of Least Astonishment’ and its significance in user interface design.
- Give an example of applying the ‘Law of Demeter’ in a Java class.
- Discuss the importance of the ‘DRY (Don’t Repeat Yourself)’ principle in software development.
Diagram Questions
- Based on the class diagram provided in the lecture, describe the relationship
between the ‘Customer’ and ‘Order’ classes.
- In the UML class diagram for the food delivery system, explain the role of the ‘DeliveryPerson’ class.
- Analyze the ‘Payment’ class in the system model and explain its associations with other classes.
- Evaluate the design of the ‘Restaurant’ class in the context of object-oriented principles.
- Interpret the package diagram provided and explain how it organizes the system into modules.
Code-Based Questions
- Refactor the given code snippet to improve its adherence to the KISS principle.
// Original Code
public class DataProcessor {
// Complex method with unnecessary steps
void processData(Data data) {
// ...
}
}- Rewrite the following Java method to demonstrate the application of the YAGNI principle.
// Original Method
public class FeatureSet {
void essentialFeature() { /* Implementation */ }
void futureFeature() { /* Might be used later */ }
}- Given a high coupling scenario in a Java application, suggest improvements to reduce coupling.
// High Coupling Example
class Order {
Payment payment;
void processOrder() {
payment.processPayment();
}
}- Demonstrate how to apply the Interface Segregation Principle to the following Java interface.
// Original Interface
interface Worker {
void work();
void eat();
void takeBreak();
}- Provide an example of using the Dependency Inversion Principle in a Java class that handles user authentication.
Advanced Conceptual Questions
- Discuss the impact of using the GRASP principles in object-oriented design.
- How do the SOLID principles contribute to the development of a robust and maintainable software system?
- Explain how ‘Composition over Inheritance’ can be beneficial in object-oriented design.
- Analyze the trade-offs between ‘Early Binding’ and ‘Late Binding’ in the context of polymorphism.
- Discuss the role of ‘Design Patterns’ in solving common software design problems.
Real-World Application Questions
- How would you apply object-oriented principles to redesign a legacy e-commerce system for better performance?
- Propose an object-oriented solution for a library management system, focusing on modularity and reusability.
- Evaluate the object-oriented design of a mobile banking application in terms of usability and security.
- Design an object model for a smart home system, considering the interactions between different smart devices.
- Critique the object-oriented approach used in a popular video game’s development, focusing on flexibility and scalability.
Week 3
Tutorial Sheet: Refactoring and Code Smells
Multiple Choice Questions
What is the primary goal of refactoring? A. To add new features B. To fix bugs C. To improve the internal structure of code without changing its external behavior D. To enhance the performance of the code
What is a ‘Code Smell’? A. A type of software bug B. A syntax error in the code C. An indicator of potential issues in your code that may require refactoring D. A tool used for code analysis
Which of the following best describes ‘Technical Debt’? A. The time taken to write technical documentation B. The financial cost of purchasing new software tools C. The implied cost of additional rework caused by choosing an easy solution now instead of using a better approach that would take longer D. The expenses related to hiring technical staff
Which of the following is a common challenge in refactoring? A. Deciding on new features to add B. Understanding and working with legacy code C. Choosing the right programming language D. Deciding on the software’s user interface design
When should refactoring be done? A. Only at the start of a new project B. Just before adding a new feature C. After the software is completed D. Whenever a bug is found
What is the ‘Extract Method’ refactoring technique? A. Removing a method from the codebase B. Combining multiple methods into one C. Creating a new method by extracting a section of code from an existing method D. Renaming a method for clarity
Which of the following is a benefit of the ‘Extract Method’ refactoring technique? A. It increases code performance B. It enhances code readability and reusability C. It combines related methods for efficiency D. It introduces necessary complexity into the code
What does the term ‘Polymorphism’ refer to in refactoring? A. Changing the structure of the code without altering its behavior B. A design pattern used to replace conditional statements C. A method of optimizing the performance of the code D. A technique for handling exceptions in code
In the context of refactoring, what is the ‘Open/Closed Principle’? A. Code should be open for extension but closed for modification B. Open source code should be used wherever possible C. Code should be closed to new features once it is tested D. Code should be open to frequent changes and updates
What does the ‘Move Method’ refactoring technique involve? A. Shifting a method from one class to another more appropriate class B. Moving a method to a different part of the same class C. Renaming a method for better clarity D. Deleting a method that is no longer needed
Which of the following is an example of a ‘Long Method’ code smell? A. A method with multiple return statements B. A method that is only called once in the codebase C. A method with too many lines of code, making it hard to understand D. A method that uses a long list of parameters
How can the ‘Long Method’ code smell be addressed? A. By adding more comments to explain the code B. By breaking it down into smaller, more focused methods C. By moving it to a different class D. By rewriting the entire method from scratch
What is a ‘Large Class’ in the context of code smells? A. A class with a large number of students B. A class with too many responsibilities or too much code C. A class that is widely used throughout the application D. A class that takes a long time to execute
What refactoring technique can be used to address a ‘Large Class’? A. Inline Method B. Extract Class C. Rename Method D. Move Field
What does the ‘Duplicated Code’ smell indicate? A. A method is called multiple times B
. The same or very similar code structure appears in more than one place C. Two or more classes have identical methods D. Variables are declared multiple times in the code
How can the ‘Duplicated Code’ smell be refactored? A. By using more comments to explain why the code is duplicated B. By extracting the duplicated code into a single method C. By renaming the methods or classes containing the duplicated code D. By splitting the duplicated code into multiple smaller pieces
What is the primary risk of the ‘Switch Statements’ smell in code? A. It makes the code easier to read B. It can lead to code that is hard to modify and extend C. It improves the performance of the code D. It helps in making the code more modular
What refactoring technique can be used for ‘Switch Statements’? A. Replace Conditional with Polymorphism B. Extract Method C. Inline Method D. Move Field
What does ‘Feature Envy’ refer to in code smells? A. A method that performs too many tasks B. A method that is more interested in a class other than the one it is in C. A class that uses too many features from external libraries D. A method that does not use any features of its own class
How can ‘Feature Envy’ be addressed in refactoring? A. By moving the method to the class it is most interested in B. By breaking the method into smaller parts C. By renaming the method for better clarity D. By combining it with other similar methods
What is ‘Primitive Obsession’ in the context of code smells? A. The use of primitive data types instead of small objects for simple tasks B. An obsession with optimizing code to its most basic form C. The preference for using primitive data types over built-in objects D. The excessive use of complex data structures
How can ‘Primitive Obsession’ be refactored? A. By replacing primitive data types with more complex structures B. By creating objects for the types of data you have C. By using more primitive data types in the code D. By simplifying the logic in methods using primitive data types
In refactoring, what is the ‘Inline Method’ technique used for? A. Combining multiple methods into one B. Moving a method to a different class C. Replacing calls to the method with the method’s content and deleting the method D. Extracting a part of a method into a new method
What is a common strategy for dealing with ‘Long Parameter Lists’? A. Introduce Parameter Object B. Extract Method C. Inline Method D. Move Method
What is the role of testing in refactoring? A. To add new functionality to the code B. To confirm unchanged behavior post-refactoring C. To speed up the performance of the code D. To increase the number of features in the software
Which of the following traits is important for a good test? A. Long execution time B. Dependency on other tests C. Easily comprehensible D. Testing multiple aspects at once
In the context of refactoring, what is ‘Continuous Testing’? A. Testing that runs for an extended period B. Implementing testing during the entire refactoring process C. Testing only at the end of the development cycle D. Testing performed by the end-users
What is the ‘Rename Method’ refactoring technique used for? A. Changing the method name to more accurately describe what it does B. Renaming variables within a method for clarity C. Combining two methods into one D. Splitting a method into two with different names
What is a ‘Data Clump’ in code smells? A. A cluster of data that always appears together but isn’t organized into a structure B. A collection of unrelated data types C. A large amount of data processed by a single method D. A group of variables that are frequently modified together
How can ‘Data Clumps’ be addressed in refactoring? A. By introducing Parameter Object or Class B. By using Inline Method C. By renaming the methods that use the data clump D. By splitting the data clump into smaller parts
Short Answer Questions
- **Explain the concept of ‘Refactoring’ with an example
.**
Describe the ‘Extract Method’ refactoring technique and its benefits.
How can the ‘Long Method’ code smell negatively impact code quality, and what is a typical way to refactor it?
Discuss the role of polymorphism in refactoring, particularly in addressing ‘Switch Statements’.
Explain how ‘Feature Envy’ can be identified and refactored in a codebase.
Describe the process and benefits of addressing ‘Primitive Obsession’ through refactoring.
What are the challenges associated with refactoring legacy code, and how can they be overcome?
Discuss the importance of testing in the context of refactoring.
Explain the ‘Rename Method’ refactoring technique with an example.
Describe how ‘Data Clumps’ can be refactored and the benefits of this refactoring.
UML : Week 3 and 4
Tutorial Sheet: Understanding UML
Multiple Choice Questions
What is the primary purpose of UML? A. Code generation B. Modeling software systems C. Database management D. Networking protocols
UML was standardized by which organization? A. IEEE B. W3C C. OMG D. ISO
Which UML diagram is used to represent the functionality of a system from an end-user’s perspective? A. Sequence Diagram B. Class Diagram C. Use Case Diagram D. Activity Diagram
In a class diagram, what does an arrow with a hollow diamond represent? A. Dependency B. Association C. Aggregation D. Inheritance
Which of these is not a type of interaction diagram in UML? A. Sequence Diagram B. Communication Diagram C. Use Case Diagram D. Timing Diagram
What does a ‘lifeline’ represent in a sequence diagram? A. Duration of an object’s existence B. Message sent between objects C. Inheritance between classes D. Database interaction
What is an ‘actor’ in a use case diagram? A. A function or method B. A system or user interacting with the system C. A database entity D. A programming class
In a class diagram, what is typically not included? A. Class attributes B. Class operations C. User interface design D. Relationships between classes
What kind of diagram would you use to show the different states an object can be in? A. State Diagram B. Activity Diagram C. Class Diagram D. Package Diagram
What does a ‘fork’ node in an activity diagram represent? A. Decision-making B. Start of the activity C. End of the activity D. Split of flow into parallel paths
Which UML diagram shows the architecture of a system and its components? A. Deployment Diagram B. Class Diagram C. Component Diagram D. Object Diagram
In a state diagram, what does a transition represent? A. A change in the state of an object B. A static condition of an object C. An error state D. A final state of an object
Which diagram type helps in visualizing workflows and operations in business process modeling? A. Use Case Diagram B. Sequence Diagram C. Activity Diagram D. State Machine Diagram
What does a dashed line in a sequence diagram represent? A. Asynchronous call B. Synchronous call C. Return control from a method D. Object creation
Which UML diagram type is best suited for modeling the physical deployment of a system? A. Class Diagram B. Sequence Diagram C. Deployment Diagram D. Activity Diagram
In UML, an ‘extend’ relationship in a use case diagram is used to represent what? A. Mandatory inclusion of another use case B. Optional or conditional inclusion of another use case C. A generalization between two use cases D. A static relationship between two actors
What does an ‘abstract’ class in a UML class diagram indicate? A. The class cannot be instantiated B. The class is incomplete and must be derived C. The class contains only static methods D. The class is a template for other classes
In UML, what does a package diagram primarily show? A. The dynamic behavior of a system B. The sequence of method calls in a system C. The organization and dependencies among different system
elements D. The physical layout of a system
Which diagram would be most useful for illustrating the decision points in a process? A. Class Diagram B. State Diagram C. Activity Diagram D. Sequence Diagram
A diagram showing the objects in a system at a particular instance of time is known as? A. Class Diagram B. Object Diagram C. Component Diagram D. Deployment Diagram
What is the purpose of ‘swimlanes’ in an activity diagram? A. To indicate parallel processes B. To represent responsibilities of different organizational units C. To show synchronization of processes D. To depict the lifeline of an object
In a class diagram, how is a dependency relationship typically represented? A. Solid line with an arrow B. Dashed line with an arrow C. Solid line without arrows D. Dashed line without arrows
What type of UML diagram is primarily used for database design? A. Use Case Diagram B. Activity Diagram C. Class Diagram D. Sequence Diagram
In UML, which diagram would be best to describe different user interactions with a system? A. Class Diagram B. Use Case Diagram C. State Diagram D. Activity Diagram
A ‘component diagram’ in UML is used to show what? A. The dynamic behavior of a system B. The physical deployment of software components C. The layout of GUI components D. The organization and relationships between software components
In a sequence diagram, what does a ‘found message’ represent? A. A message that is sent to a non-existent object B. The initial message that starts the interaction C. A message received from an external system D. A message that is lost during transmission
Which UML diagram is most useful for modeling the lifecycle of an object? A. Class Diagram B. State Diagram C. Sequence Diagram D. Use Case Diagram
In a use case diagram, the relationship between an actor and a use case is generally represented by what? A. A solid line B. A dashed line C. An arrow D. A double line
What does an ‘artifact’ in a UML deployment diagram typically represent? A. A physical hardware component B. A software component or file C. An unresolved issue in the design D. A user of the system
In UML, which of the following is not a type of structural diagram? A. Class Diagram B. Sequence Diagram C. Object Diagram D. Component Diagram
Short Answer Questions
Explain the difference between ‘aggregation’ and ‘composition’ in UML class diagrams.
Describe how a use case diagram can help in understanding system requirements.
Illustrate the concept of ‘polymorphism’ using an example in a UML class diagram.
Discuss the role of ‘sequence diagrams’ in modeling the interactions between objects in a system.
How do ‘state diagrams’ help in understanding the behavior of an object? Give an example.
Explain the significance of ‘deployment diagrams’ in understanding a system’s architecture.
Describe how ‘activity diagrams’ can be used to model a business process. Provide an example.
In UML, how are ‘packages’ used to organize model elements? Give an example.
What is the purpose of ‘interaction overview diagrams’ in UML?
Explain how ‘swimlanes’ in an activity diagram can be used to represent different departments in a business process.
Week 4
Tutorial Sheet: Design Pattern: Creational Patterns
Multiple Choice Questions
What is the primary purpose of Creational Design Patterns? A. To manage application state B. To simplify object creation and increase flexibility C. To handle system security D. To optimize application performance
Which Design Pattern delegates the creation of objects to subclasses? A. Singleton Pattern B. Factory Method Pattern C. Abstract Factory Pattern D. Builder Pattern
In the Factory Method Pattern, what is the primary benefit of polymorphism? A. Increased security B. Improved performance C. Flexibility in object creation D. Simplified code structure
Which of these is NOT a main category of Design Patterns according to the slides? A. Creational Patterns B. Structural Patterns C. Functional Patterns D. Behavioral Patterns
The Singleton Pattern ensures: A. Multiple instances of a class B. A class has only one instance C. A class can be instantiated in multiple ways D. A class does not have any instance
What does the Abstract Factory Pattern provide? A. An interface for creating individual objects B. A way to create families of related objects C. A method for deleting objects D. A pattern for building complex objects step-by-step
What is the main criticism of the Singleton Pattern? A. It leads to overcomplicated code B. It can introduce a global state in the application C. It does not allow object creation D. It is too simple for complex applications
Which pattern is often used in game development for creating different types of enemies? A. Factory Method Pattern B. Singleton Pattern C. Builder Pattern D. Prototype Pattern
In the context of Factory Patterns, what does ‘Encapsulation’ primarily achieve? A. It enhances the performance of the application B. It hides the creation logic of objects C. It makes the application more secure D. It simplifies the debugging process
The Factory Method Pattern in Java often uses which of the following concepts? A. Inheritance B. Serialization C. Multithreading D. Recursion
Which design pattern can be used for managing resources like database connections? A. Abstract Factory Pattern B. Singleton Pattern C. Builder Pattern D. Adapter Pattern
What is a major drawback of using the Singleton Pattern? A. High memory usage B. It makes unit testing difficult C. Incompatibility with multithreaded applications D. Reduced application speed
Which pattern focuses on object composition and forming larger structures? A. Creational Patterns B. Structural Patterns C. Functional Patterns D. Behavioral Patterns
What is the primary role of ‘Abstract Factory’ in the Abstract Factory Pattern? A. To create single objects B. To manage the lifecycle of objects C. To create families of related or dependent objects D. To destroy objects
What is the key difference between the Factory Method and Abstract Factory Patterns? A. Factory Method is about creating single objects, while Abstract Factory is about creating families of objects B. Factory Method creates objects at runtime, while Abstract Factory creates objects at compile-time C. Factory Method is used for complex object creation, while Abstract Factory is used for simple object creation D. Factory Method deals with object destruction, while Abstract Factory deals with object creation
In a UML diagram for the Factory Method Pattern, which relationship typically exists between ‘Creator’ and ‘ConcreteCreator’? A. Aggregation B. Composition C. Inheritance D. Association
The ‘Product’ in the Factory Method Pattern is usually represented as: A. A concrete class B. An abstract
class or interface C. A static method D. A global variable
In the Abstract Factory Pattern, ‘ConcreteFactory1’ and ‘ConcreteFactory2’ typically: A. Create the same type of products B. Create different families of related products C. Act as Singleton classes D. Implement the same concrete class
Which pattern provides a global point of access to its instance? A. Abstract Factory Pattern B. Builder Pattern C. Singleton Pattern D. Prototype Pattern
In which scenario is the Singleton Pattern NOT ideal? A. Managing application-wide configurations B. Coordinating actions across a system C. When multiple instances of a class are needed D. Managing a shared resource pool like a database connection
What is the advantage of using an Abstract Factory for cross-platform UI creation? A. It reduces the application’s memory usage B. It ensures UI consistency across different platforms C. It increases the execution speed of the application D. It simplifies the process of writing platform-specific code
When is using the Singleton Pattern considered a ‘code smell’? A. When it’s used to manage a global state B. When it’s used in multithreaded applications C. When it’s combined with other design patterns D. When it’s used in small, simple applications
Which pattern is specifically targeted at simplifying object creation mechanisms? A. Structural Patterns B. Creational Patterns C. Behavioral Patterns D. Concurrency Patterns
The Factory Method Pattern is mainly concerned with: A. Creating complex objects step by step B. Delegating instantiation to subclasses C. Managing global state across the application D. Ensuring a class has only one instance
The Abstract Factory Pattern is particularly useful for: A. Ensuring a class has only one instance B. Creating a suite of related products C. Managing global application state D. Building complex objects from simple ones
In the context of design patterns, what does ‘Polymorphism’ allow? A. Multiple classes inheriting from a single class B. A class to change its behavior at runtime C. Treating objects of different classes as instances of their parent class D. Encapsulation of data within a class
Which pattern is more abstract and high-level: Architectural Patterns or Idioms? A. Architectural Patterns B. Idioms C. They are equally abstract D. Neither is considered abstract
The main criticism of design patterns is that they: A. Are too simple and do not solve complex problems B. Can lead to overcomplicated code when misapplied C. Are only applicable in large-scale applications D. Increase the development time significantly
What does Dependency Injection in the Factory Pattern help with? A. Increasing the security of the application B. Simplifying the creation of objects C. Reducing the application’s memory footprint D. Enhancing the graphical user interface
Why is the Singleton Pattern controversial in terms of testability? A. It makes mocking difficult due to global state B. It requires complex setup for testing C. It is incompatible with most testing frameworks D. It only allows for end-to-end testing
Short Answer Questions
Describe how the Singleton Pattern can be implemented in a multithreaded environment. Include a basic code example.
Explain the concept of ‘Creational Patterns’ in design patterns and provide two examples.
With reference to the provided UML diagram, explain the Factory Method Pattern.
Discuss the differences between the Factory Method and Abstract Factory Patterns, citing examples.
Explain the concept of polymorphism in the context of the Factory Method Pattern with a code snippet.
Provide a real-world example of where you might use the Singleton Pattern and discuss its potential drawbacks.
Using the provided UML diagram, explain the Abstract Factory Pattern and its components.
Discuss how the Abstract Factory Pattern can be used for theme switching in an application, with a brief code example.
Elaborate on the criticisms of the Singleton Pattern, particularly focusing on its impact on application design.
Compare and contrast the Singleton and Factory Patterns in terms of their use cases and limitations.
Week 5
Tutorial Sheet Part 1
Multiple Choice Questions
What is the primary focus of Behavioral Design Patterns in software engineering? A. Data storage optimization B. Effective communication and assignment of responsibilities among objects C. User interface design D. Algorithm optimization
In the Strategy Pattern, what is the primary benefit of making algorithms interchangeable? A. Improves algorithm efficiency B. Allows dynamic changes in algorithm behavior C. Reduces the overall code size D. Increases data security
Why does the Strategy Pattern prefer composition over inheritance? A. To simplify code debugging B. To enhance performance C. To offer greater flexibility in changing behavior D. To reduce memory usage
In the context of the Strategy Pattern, what issue arises from adding a
flymethod to the Duck class for all ducks? A. Increases the complexity of the Duck class B. Not all ducks should have the ability to fly C. Violates the single responsibility principle D. Makes it difficult to implement new duck typesWhat is the main advantage of encapsulating
flyandquackbehaviors in the Strategy Pattern? A. Simplifies the Duck class implementation B. Ensures all ducks have uniform behavior C. Allows the behaviors to vary independently of the duck types D. Reduces the memory footprint of duck objectsIn the Observer Pattern, what role does the
Subjectplay? A. Notifies a list of observers about its state changes B. Acts as a container for multiple subjects C. Observes and updates the state of other objects D. Manages the communication between different observersWhich model in the Observer Pattern involves the subject sending detailed data to observers? A. Push Model B. Pull Model C. Event-Driven Model D. Reactive Model
What is a key benefit of the Observer Pattern in terms of software design? A. Increases the speed of data processing B. Reduces coupling between the subject and observers C. Simplifies the data storage mechanism D. Enhances the graphical interface of applications
In the Command Pattern, what is the role of the
Invoker? A. Executes the command B. Receives and processes the request C. Encapsulates a request as an object D. Sends the command to the appropriate receiverWhat is a significant benefit of using the Command Pattern in user interfaces? A. Reduces memory usage B. Allows commands to be added or changed dynamically C. Simplifies the internal logic of commands D. Enhances the speed of command execution
Questions with Diagrams or Source Code
- Draw a UML diagram representing the Strategy Pattern used in the duck example. Include
Duck,FlyBehavior,FlyWithWings,FlyNoWay,QuackBehavior,Quack, andSqueak.
@startuml
' UML diagram code for the Strategy Pattern
@enduml
- Implement a simple Java class for
MallardDuckextending theDuckclass using the Strategy Pattern.
public class MallardDuck extends Duck {
// Java code implementation
}- Create a UML diagram illustrating the basic structure of the Observer Pattern. Include
Subject,Observer,registerObserver,removeObserver, andnotifyObservers.
@startuml
' UML diagram code for the Observer Pattern
@enduml
- Provide a Java interface for
ObserverandObservablein the Observer Pattern.
public interface Observer {
// Java code for Observer interface
}
public interface Observable {
// Java code for Observable interface
}- Sketch a UML diagram for the Command Pattern with
Invoker,Command, andReceiver.
@startuml
' UML diagram code for the Command Pattern
@enduml
- **Write
a Java class LightOnCommand implementing the Command interface for a smart home system.**
public class LightOnCommand implements Command {
// Java code for LightOnCommand
}- Design a UML diagram for the State Pattern using a
Turnstileas an example. Include statesClosed,Open, and transitions likepay_okandenter.
@startuml
' UML diagram code for the State Pattern with Turnstile
@enduml
- Implement the
Stateinterface andClosedStateclass in Java for the State Pattern example.
public interface State {
// Java code for State interface
}
public class ClosedState implements State {
// Java code for ClosedState
}- Create a UML diagram for the Template Method Pattern showing
AbstractClassandConcreteClass.
@startuml
' UML diagram code for the Template Method Pattern
@enduml
- Write an abstract Java class
Gamewith a template methodplaythat includes stepsinitialize,startPlay, andendPlay.
public abstract class Game {
// Java code for Game class
}Additional Questions
Explain how the State Pattern simplifies state management in complex systems.
Describe the benefits of using the Template Method Pattern in software frameworks.
Discuss how the Iterator Pattern enhances maintainability and readability in handling collections.
Provide an example of how the Command Pattern can be used in a transactional system.
Illustrate how the Observer Pattern can be applied in a real-world application like weather monitoring.
Explain the difference between the Push and Pull models in the Observer Pattern.
Describe a scenario where the Strategy Pattern can significantly improve code flexibility.
Discuss the advantages of decoupling the iteration logic from the collection objects using the Iterator Pattern.
Explain how the Template Method Pattern adheres to the Open/Closed Principle.
Discuss the application of the Command Pattern in user interface design, with an example.
True/False Questions
The Strategy Pattern typically uses inheritance to achieve dynamic behavior. (True/False)
In the Observer Pattern, observers are tightly coupled with the subject. (True/False)
The Command Pattern primarily focuses on enhancing algorithm efficiency. (True/False)
The State Pattern helps in managing states and behaviors of objects in a memory-efficient way. (True/False)
The Template Method Pattern allows subclasses to change the algorithm’s structure. (True/False)
The Iterator Pattern provides a uniform way to traverse different data structures. (True/False)
The Observer Pattern is inefficient in scenarios where state changes are frequent and numerous. (True/False)
The Command Pattern supports undoable operations. (True/False)
In the State Pattern, the behavior of an object depends on its history of states. (True/False)
The Template Method Pattern can incorporate hooks for additional flexibility. (True/False)
Part 2: Structural Design Patterns Tutorial Sheet
Multiple Choice Questions
What is the main purpose of the Adapter Pattern? A. To decouple an abstraction from its implementation B. To make two incompatible interfaces work together C. To provide a unified interface to a set of interfaces in a subsystem D. To control access to a particular object
Which pattern is known as a ‘wrapper’? A. Facade B. Proxy C. Adapter D. Decorator
In the Adapter Pattern, what role does the ‘Adapter’ class play? A. It is an interface that defines the domain-specific operations B. It adapts an existing interface to another interface C. It provides a simplified interface to a complex system D. It acts as a surrogate for another class
Consider the following UML diagram:
@startuml
interface Target {
+request(): void
}
class Adapter {
-adaptee: Adaptee
+request(): void
}
class Adaptee {
+specificRequest(): void
}
Target <|.. Adapter
Adapter -left-> Adaptee: adapts
@enduml
What does the arrow from ‘Adapter’ to ‘Adaptee’ signify? A. Inheritance B. Dependency C. Association D. Realization
What does the Facade Pattern primarily provide? A. Additional functionalities to an object dynamically B. A way to access the underlying object through a surrogate C. A simplified interface to a complex system D. A mechanism to adapt different interfaces
The Proxy Pattern is mainly used for: A. Simplifying complex operations B. Adding new functionalities to an object C. Controlling access to an object D. Adapting different interfaces
In the Decorator Pattern, what does the ‘Decorator’ class do? A. It replaces the functionality of the ‘Component’ class B. It controls the access to the ‘Component’ class C. It provides a unified interface to a set of interfaces D. It adds responsibilities to the ‘Component’ class dynamically
Which pattern involves composing objects into tree structures to represent part-whole hierarchies? A. Composite B. Adapter C. Proxy D. Bridge
What is a key advantage of the Decorator Pattern? A. It simplifies complex interfaces B. It allows for dynamic addition of responsibilities C. It controls access to different objects D. It adapts incompatible interfaces
Which pattern is most suitable for adding new functionalities to objects at runtime? A. Adapter B. Facade C. Proxy D. Decorator
Consider this Java code snippet for the Adapter Pattern:
public interface MediaPlayer {
void play(String audioType, String fileName);
}
public class MediaAdapter implements MediaPlayer {
// Implementation
}What is the role of 'MediaAdapter' in this context?
A. It acts as a Proxy to control access to MediaPlayer
B. It is a Decorator adding new functionality to MediaPlayer
C. It adapts the interface of MediaPlayer to another interface
D. It provides a simplified interface to MediaPlayer
- In the Facade Pattern, the Facade class: A. Acts as a substitute for a set of other classes B. Dynamically adds new behaviors to a set of classes C. Adapts the interface of one class to match another
Provides a single interface to a complex subsystem
The Proxy Pattern is typically used for: A. Dynamically extending an object’s functionality B. Creating complex set of objects with simpler interfaces C. Controlling and managing access to an object D. Connecting incompatible interfaces
Which pattern would be most appropriate for creating a unified interface for a complex system involving multiple networked devices? A. Adapter B. Facade C. Composite D. Decorator
In the Decorator Pattern, the relationship between the ‘Decorator’ and the ‘Component’ it decorates is best described as: A. Aggregation B. Inheritance C. Association D. Composition
What is a characteristic feature of the Composite Pattern? A. It allows objects to be composed into tree structures B. It provides a unified interface to a complex subsystem C. It adapts the interface of one class to another D. It controls access to another object
The primary purpose of the Adapter Pattern is to: A. Control access to an object B. Simplify complex interfaces C. Allow incompatible interfaces to work together D. Add new functionality to an object at runtime
In the context of the Proxy Pattern, the ‘Proxy’ class is responsible for: A. Adding new functionalities to the ‘Subject’ class B. Simplifying the interface of the ‘Subject’ class C. Acting as an intermediary for access to the ‘Subject’ class D. Changing the interface of the ‘Subject’ class
Which pattern would you use to add scrollbars to a window in a GUI application without altering the window’s class? A. Composite B. Adapter C. Decorator D. Facade
In a UML diagram for the Decorator Pattern, what type of relationship is shown between the ‘Decorator’ and the ‘Component’? A. Generalization B. Dependency C. Association D. Aggregation
How does the Composite Pattern treat individual objects and compositions of objects? A. It treats them differently based on their types B. It treats them uniformly C. It only allows individual objects to be treated as composites D. It does not allow individual objects to participate in compositions
The Facade Pattern is most similar to which other pattern in terms of providing a simplified interface? A. Adapter B. Decorator C. Proxy D. Composite
What is a common use case for the Proxy Pattern? A. To add new responsibilities to an object dynamically B. To provide a simplified interface to a complex subsystem C. To enable two incompatible interfaces to work together D. To control access to an object, possibly adding extra functionality
Which design pattern is most suitable for handling permissions in software systems? A. Decorator B. Adapter C. Proxy D. Facade
In the Adapter Pattern, the ‘Target’ interface represents: A. The final interface that the client expects B. The original interface that needs to be adapted C. The interface of the object being adapted D. The interface of the adapter class itself
Which of the following best describes the relationship between ‘Decorator’ and ‘Component’ in the Decorator Pattern? A. The ‘Decorator’ is a type of ‘Component’ B. The ‘Decorator’ controls access to ‘Component’ C. The ‘Decorator’ adapts the interface of ‘Component’ D. The ‘Decorator’ simplifies the interface of ‘Component’
In a UML diagram for the Facade Pattern, what does the arrow from the ‘Facade’ to a ‘Subsystem Class’ typically represent? A. Inheritance B. Dependency C. Association D. Realization
If you need to control the creation of expensive objects and manage their lifecycle, which pattern would be the most appropriate? A. Adapter B. Decorator C. Facade D. Proxy
Which pattern would be best to use when you have several related classes that share common functionality, but you want to modify them individually at runtime? A. Adapter B. Composite C. Decorator D. Proxy
In the context of the Composite Pattern, a ‘Leaf’ can be best described as: A. A class that contains many objects of the Component type B. A class that adds additional responsibilities to objects
C. An individual object without any children D. An interface that defines operations for both composites and leaves
Short Answer Questions
Describe the role of the ‘Target’ interface in the Adapter Pattern with an example.
Explain how the Facade Pattern can simplify interactions with a complex subsystem.
In what scenario would you choose the Proxy Pattern over the Decorator Pattern? Provide an example.
Discuss the advantages and disadvantages of using the Decorator Pattern in software design.
How does the Composite Pattern manage the complexity of tree-like structures? Give an example.
Explain the differences between the Adapter and Facade Patterns with examples.
How can the Proxy Pattern be used to add a security layer to a software system? Illustrate with an example.
Describe a real-world scenario where the Decorator Pattern would be an ideal solution.
Discuss how the Composite Pattern can be used to simplify client code when dealing with complex object structures.
Compare and contrast the use cases for the Decorator and Composite Patterns in object-oriented design.