Design Patterns Coursera Quiz Answers – Networking Funda

All Weeks Design Patterns Coursera Quiz Answers

Design Patterns Coursera Quiz Answers

Week 1: What is a Design Pattern?

Q1. When is the best time to use a design pattern? Choose two answers.

  • For a problem that is unique to your program.
  • For a commonly-encountered issue.
  • When fixing spaghetti code
  • When explaining a solution to your fellow developers

Q2. What is the purpose of the Singleton pattern? Select the two correct answers.

  • to enforce instantiation of only one object of a class
  • to provide global access to an object
  • to enforce collaboration of a class with only one other class
  • to provide simple classes with only one method

Q3. What does it mean to “let the subclass decide” in the Factory Method Pattern?

  • the subclass defines the methods for concrete instantiation. As such, the type of object is determined by which subclass is instantiated.
  • the subclass decides which object to create, but calls a method that is defined in the superclass to instantiate the class
  • the subclass will pass a parameter into a factory that determines which object is instantiated.

Q4. What do we call the creation of an object, for example, with the ‘new’ operator in Java?

  • class creation
  • object realization
  • manifestation
  • concrete instantiation.

Q5. What are the advantages of the Facade pattern? Select the three correct answers.

  • The client and the subsystem are more loosely coupled
  • The complexity of the subsystem is hidden
  • The Facade class redirects requests as needed
  • The subsystem can handle more clients

Q6. Which of the following diagrams shows the Adapter pattern?

Design Patterns Coursera Quiz Answers - Networking Funda
  • b)
  • a)
  • c)
  • d)

Q7. Which of these are the best applications for a Composite Pattern? Choose the three correct answers.

  • Music in a playlist
  • Elements in a user-interface dialog
  • Files and folders
  • Students in a class

Q8. Which of these is NOT a common application of the Proxy Pattern?

  • information proxy
  • remote proxy
  • virtual proxy
  • protection proxy

Q9. How does a Decorator Pattern work? Choose one.

  • expands the methods of a class with inheritance
  • builds a behaviour by stacking objects
  • encapsulates a class to give it a different interface
  • adding features to a class with a new class

Q10. What are the object types that are used in the Composite Pattern? Select the two correct answers.

  • leaf
  • branch
  • root
  • trunk
  • composite

Q11. Many different clients need to create a similar object. You would like to outsource this concrete instantiation to a dedicated class. Which technique will you use, in one word?

Enter answer here

Q12. How do you enforce the creation of only one Singleton object? Select the two correct answers.

  • Specify in the comments that only one Singleton object is to be instantiated.
  • Give the Singleton class a private constructor
  • Throw an exception if a Singleton object is already instantiated
  • Write a method that can create a new Singleton object or return the existing one.

Week 2: Behavioural Patterns

Q1. Choose the most appropriately implemented Template pattern.

Some UML reminders that will help you:

  1. a private method or variable is denoted by a – as in -boilWater().
  1. a method, variable, or class that is abstract is denoted by italics (as in PastaTemplate)
Design Patterns Coursera Quiz Answers - Networking Funda
  • a)
  • b)
  • c)
  • d)

Q2. What is the correct situation for the use of a Chain of Responsibility pattern?

  • You need a set of objects to each contribute information on responding to a request.
  • You have multiple potential handlers, but only one will deal with the request.
  • You need to pass a message to multiple receivers.
  • You need to delegate a set of tasks to a hierarchy of objects.

Q3. What is the purpose of encapsulating state in an object in the State Pattern? Choose the three that are correct.

  • it turns the context into a client of the state.
  • it allows the current state object to decide how to achieve behaviours specific to the state of the context.
  • it removes large conditionals that are difficult to maintain.
  • it allows the current state to be copied from one instance to another

Q4. What design principles is the Command Pattern using?

  • Encapsulation, generalization, information hiding
  • Generalization, information hiding, loose coupling
  • Encapsulation, information hiding, loose coupling
  • Encapsulation, generalization, loose coupling

Q5. Which are the minimum requirements of the Observer pattern? Choose the three that are correct.

  • methods to add or remove observers
  • a state variable to determine if observers have been notified.
  • method to notify observers
  • update method in observers

Q6. When are you most likely to need a Mediator pattern?

  • When your class is sending a request that might be handled by one of several handlers.
  • When you are coordinating the activities of a set of related classes.
  • When you want to de-couple a class that is requesting a service from one that is providing it.
  • When you have two classes with different interfaces that you must connect.

Q7. Marlon is coding part of the software that follows a similar sequence of steps. Depending on the type of object, these steps will be implemented in slightly different ways, but their order is always the same. Which design pattern could Marlon use?

  • Template pattern
  • Mediator pattern
  • Command pattern
  • State pattern

Q8. What are the important roles in the Command Pattern?

  • Delegate, Command, Requester
  • Command, Queue, Receiver
  • Sender, Receiver, Invoker
  • Command, Receiver, Invoker

Q9. Select the best UML class diagram representation of the Chain of Responsibility pattern.

Design Patterns Coursera Quiz Answers - Networking Funda
  • a)
  • b)
  • c)
  • d)

Q10. You have a machine performing a complex manufacturing task, with different sensors and different components of the machine represented by different classes. Which design pattern will you use to arrange the parts?

  • Template
  • Command
  • Mediator
  • Chain of Responsibility

Q11. You have a security system class, and it has 3 modes: normal, lockdown, and open. Which pattern would you use to model the behaviour in these different modes?

  • Template
  • Observer
  • Mediator
  • State

Q12. One of your classes represents a mailbox, while another is the owner of the mailbox. The person would like to know when new mail arrives. Which design pattern will you probably use?

  • Mediator
  • State
  • Command
  • Observer

Week 3: Model-View-Controller Pattern

Q1. What does MVC Stand for? Use spaces between each word, no upper case letters, and no punctuation.

Model View Controller

Q2. Select the two elements of the open/closed principle:

  • Closed for modification
  • Open for extension
  • Open for maintenance
  • Open for modification
  • Closed for maintenance.
  • Closed for extension.

Q3. What is the best description of the Dependency Inversion principle?

  • Client objects depend on an Adaptor Pattern to interface with the rest of the system.
  • Client objects depend on generalizations instead of concrete objects.
  • Client objects are dependent on a service interface that directs their requests.
  • Service objects subscribe to their prospective client objects as Observers, watching for a request.

Q4. Which of these statements is true about the Composing Objects principle?

  1. it provides behaviour with aggregation instead of inheritance

2. it leads to tighter coupling

  • The first statement is true
  • The second statement is true
  • Neither statement is true
  • Both statements are true

Q5. Which of these UML diagrams demonstrates the Interface Segregation principle?

Design Patterns Coursera Quiz Answers - Networking Funda
  • a)
  • b)
  • c)
  • d)

Q6. Which of these code examples violates the Principle of Least Knowledge, or Law of Demeter?

1 public class O {
2 M I = new M();
3
4 public void anOperation2() {
5 this.I.N.anOperation();
6 }
7 }
1 public class Class1 {
2 public void N() {
3 System.out.println("Method N invoked");
4 }
5 }
6
7 public class Class2 {
8 public void M(Class1 P) {
9 P.N();
10 System.out.println("Method M invoked");
1 public class O {
2 public void M() {
3 this.N();
4 System.out.println("Method M invoked");
5 }
6
7 public void N() {
8 System.out.println("Method N invoked");
9 }
10 }
1 public class P {
2 public void N() {
3 System.out.println("Method N invoked");
4 }
5 }
6
7 public class O {
8 public void M() {
9 P I = new P();
10 I.N();

Q7. How can Comments be considered a code smell?

  • They can’t! Comments help clarify code.
  • Excessive commenting can be a coverup for bad code
  • Too many comments make the files too large to compile.
  • When a comment is used to explain the rationale behind a design decision

Q8. What is the primitive obsession code smell about?

  • Code that contains many low-level objects, without using OO principles like aggregation or inheritance.
  • Overuse of primitive data types like int, long, float
  • Using many different primitive types instead of settling on a few that together capture that appropriate level of detail for your system.
  • Using key-value pairs instead of abstract data types.

Q9. You have a class that you keep adding to. Whenever you add new functionality, it just seems like the most natural place to put it, but it is starting to become a problem! Which code smell is this?

  • Long Method
  • Large Class
  • Divergent Change
  • Speculative generality

Q10. Why is it important to avoid message chains whenever possible?

  • It’s a workaround to get to private methods, which are important for encapsulation.
  • If an unexpected object is returned, this could easily lead to runtime errors.
  • They lower cohesion in your class.
  • The resulting code is usually rigid and complex.

Q11. Look at the code snippet. Which code smell do you detect?

1 public class Class1 {
2 …
3 public void M(Class2 C) {
4 C.doSomething(x);
5 C.foo(y);
6 C.foo2(z, i);
7 }
8 }
  • Feature Envy
  • Divergent Change
  • Inappropriate Intimacy
  • Long Parameter List

Q12. Joseph was developing a class for his smartphone poker game, and decided that one day he would like to be able to change the picture on the backs of the cards, so he created a Deck superclass. Since his app does not have that feature yet, Deck has only one subclass, RegularDeck. What code smell is this?

  • Refused Bequest
  • Primitive Obsession
  • Speculative Generality
  • Divergent Change

Week 4: Assignment

Q1. Mohsin needs to create various user objects for his University learning platform. What is the act of creating an object called?

  • class creation
  • object invocation
  • concrete instantiation
  • object realization

Q2. Mohsin has a superclass that performs various operations on these user objects – Student, Professor, Assistant, for example. He wants the subclass to determine which object is created. This is sketched below in a UML diagram for the StudentUser class. What is this design pattern called?

Design Patterns Coursera Quiz Answers - Networking Funda
  • Template Pattern
  • Factory Method Pattern
  • Simple Factory
  • Composite Pattern

Q3. Select the correct UML class diagram representation of the Composite Pattern:

Design Patterns Coursera Quiz Answers - Networking Funda
  • a)
  • b)
  • c)
  • d)

Q4. Yola is programming for a grocery store system. She has a complex SalesData class that updates inventories and tracks sales figures, and a lightweight AccessSales class that will give select sales data to a user, depending on their credentials. AccessSales delegates to SalesData when more complex data is needed. This situation is shown below. Which Pattern is this?

Design Patterns Coursera Quiz Answers - Networking Funda
  • Proxy Pattern
  • Singleton Pattern
  • Decorator Pattern
  • Facade Pattern

Q5. Which of these UML class diagrams shows the Facade pattern?

Design Patterns Coursera Quiz Answers - Networking Funda
  • a)
  • b)
  • c)
  • d)

Q6. What is the difference between the Factory Method and a Simple Factory?

  • In the factory method pattern, the factory itself must be instantiated before it starts creating objects. This is usually done with a dedicated method.
  • In Factory Method, concrete instantiation is done in a designated method, where a Simply Factory creates objects for external clients
  • A simple factory instantiates only one kind of object.
  • Simple factories cannot be subclassed.

Q7. José wants to build behaviours by stacking objects and calling their behaviours with an interface. When he makes a call on this interface, the stack of objects all perform their functions in order, and the exact combination of behaviours he needs depends what objects he stacked and in which order. Which Design Pattern best fits this need?

  • Singleton Pattern
  • Composite Pattern
  • Decorator Pattern
  • Factory Method Pattern

Q8. You need to connect to a third-party library, but you think it might change later, so you want to keep the connection loosely coupled by having your object call a consistent interface. Which Design Pattern do you need?

  • Proxy
  • Adapter
  • Decorator
  • Facade

Q9. Which of these diagrams shows the State pattern?

Design Patterns Coursera Quiz Answers - Networking Funda
  • a)
  • b)
  • c)
  • d)

Q10. Which of these design principles best describes the Proxy pattern?

  • generalization, because a proxy is a general version of the real subject
  • decomposition, because the Proxy object has different concerns than the subject
  • encapsulation, because the Proxy hides some of the detail of the subject
  • separation of concerns, because the Proxy object has different concerns from the subject

Q11. Ashley has a method in her class that needs to makes a request. This request could be handled by one of several handlers. Which design pattern does she need?

  • Facade
  • Decorator
  • Chain of Responsibility
  • Template

Q12. Colin is designing a class for managing transactions in software for a banking machine software. Each transaction has many of the same steps, like reading the card, getting a PIN, and returning the card. Other steps are particular to the type of transaction. Which pattern does he need?

  • State
  • Mediator
  • Template
  • MVC

Q13. Which of these is NOT a good use of the Command pattern?

  • Building macros, for example in an image manipulation program
  • Sending a command to a third-party service or library
  • Building a user-interface that can be used to perform operations
  • Supporting undo/redo queues of commands

Q14. Choose the correct UML class diagram representation of the Observer pattern:

Design Patterns Coursera Quiz Answers - Networking Funda
  • a)
  • b)
  • c)
  • d)

Q15. Which code smell may become a problem with the Mediator design pattern?

  • Large Class
  • Refused Bequest
  • Speculative Generality
  • Inappropriate Intimacy

Q16. Hyun-Ji is developing a program. She wants to create a Student class that behaves differently based on if the student has not registered for classes, is partially registered, fully registered, or fully registered and paid. Which design pattern does she need?

  • Proxy
  • State
  • Template Method
  • Mediator

Q17. Which of these methods is found in a typical Observer class?

  • getState()
  • update()
  • addObserver()
  • notify()

Q18. Fernando is making pizza objects with the Template Method pattern. The make() function is the whole process of making the pizza. Some steps are the same for every pizza – makeDough(), and bake(). The other steps – addSauce(), addToppings() and addCheese() – vary by the pizza. Which of these subclasses shows the proper way to use a template method?

Design Patterns Coursera Quiz Answers - Networking Funda
  • a)
  • b)
  • c)
  • d)

Q19. In the Mediator Pattern, which pattern is often used to make sure the Mediator always receives the information it needs from its collaborators?

  • Observer
  • Chain of Responsibility
  • Template Method
  • Command

Q20. In the MVC Pattern, which of these is usually made into an Observer?

  • View
  • Back-End
  • Controller
  • Model

Q21. Which of these answers does NOT accurately complete the following sentence? “A class is considered closed to modification when…”

  • …it is proven to be stable within your system
  • …all the attributes and behaviours are encapsulated
  • …its collaborators are fixed
  • …it is tested to be functioning properly

Q22. How does the Dependency Inversion Principle improve your software systems?

  • Dependency becomes inverted by having the system depend on the client classes
  • Client classes use an adapter to facilitate communication between itself and the rest of the system
  • Client classes become dependent on high level generalizations rather than dependant on low level concrete classes
  • Client classes become dependant on low-level concrete classes, rather than dependant on high-level generalizations

Q23. Allison has a search algorithm, and she would like to try a different implementation of it in her software. She tries replacing it everywhere it is used and this is a huge task! Which design principle could Allison have used to avoid this situation?

  • Composing Objects Principle
  • Don’t Repeat Yourself
  • Principle of Least Knowledge
  • Dependency Inversion

Q24. Which of the code smells is shown in this code example of a method declaration?

1 private void anOperation(String colour, int x, int y, int z, int speed)
  • Message Chains
  • Large Parameter List
  • Long Method
  • Primitive Obsession

Q25. Which object-oriented design principle do Long Message Chains, a code smell, usually violate?

  • Open/Closed Principle
  • Separation of Concerns
  • Cohesion
  • Principle of Least Knowledge / Law of Demeter

Q26. Which code smell can you detect here?

1 public class Person {
2 int age;
3 int height;
4 String hairColour;
5
6 public int getAge() { return age; }
7 …
8
9 }
  • Feature Envy
  • Primitive Obsession
  • Data Class
  • Data Clump

Q27. What are the components of the MVC pattern?

  • Model, Vision, Command
  • Model, View, Command
  • Member, Vision, Controller
  • Model, View, Controller

Q28. The interface segregation principle encourages you to use which of these object-oriented design principles? Choose the 2 correct answers.

  • decomposition
  • generalization
  • abstraction
  • encapsulation

Q29. Interface Segregation is a good way to avoid which code smell? Choose the best possible answer.

  • Divergent Change
  • Long Method
  • Switch Statements
  • Refused Bequest

Q30. Which of these statements about the Decorator pattern are true?

1. The decorator classes inherit from the basic object which is being decorated

2. Decorator objects can be stacked in different order

  • The first statement is true
  • The second statement is true
  • Neither statement is true
  • Both statements are true
Get All Course Quiz Answers of Software Design and Architecture Specialization

Object-Oriented Design Coursera Quiz Answers

Design Patterns Coursera Quiz Answers

Software Architecture Coursera Quiz Answers

Service-Oriented Architecture Coursera Quiz Answers

Share your love

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Your email address will not be published. Required fields are marked *