0

programming languages Online Quiz - 183

Description: programming languages Online Quiz - 183
Number of Questions: 20
Created by:
Tags: programming languages
Attempted 0/20 Correct 0 Score 0

You have been reading the Gang of Four pattern book again and you suddenly notice a similarity between a design pattern and publish-subscribe messaging. What design pattern is similar to publish-subscribe messaging?

  1. Publisher pattern

  2. Flyweight pattern

  3. Observer pattern

  4. Chain of Responsibility pattern

  5. Subscribe pattern

  6. Proxy pattern.


Correct Option: C

In which of the following situations would you use the Observer pattern?

  1. When you need to have objects notified of events but you don't know which objects would have such needs, or if you will need to add more objects to receive such notification, at a later date.

  2. You want one object to monitor when the state of another object but you don't want the object being monitored to need to send any messages regarding its state.

  3. When the instances of your class can be use interchangeable and you want to reduce the number of instances created in order to improve performance.

  4. When you need to co-ordinate state changes between other objects by using one object.

  5. You are building an online auction site to sell rare and collectable toys. You want customers to be notified of bids on items they are bidding for in as close to real time as possible. You would use the Observer pattern to notify the customer objects of c


Correct Option: A,E

You need to access a complex object in a recursive way…building the object from other objects. This is an example of which pattern?

  1. Abstract Factory

  2. Factory Method

  3. Builder

  4. Composite

  5. Recursive Builder


Correct Option: D

When would you use the Flyweight pattern?

  1. When you need classes to be notified of events but you don't know which classes or if you will need to add more at a later date.

  2. When the instances of your class can be used interchangeably and you want to reduce the number of instances created in order to improve performance.

  3. When the instances of your class cannot be used interchangeable and you need to convert them so that they are interchangeable

  4. When the instances of your class can be use interchangeable and you want to reduce the number of instances created in order to improve performance


Correct Option: B

When would you use the Mediator pattern?

  1. When you need to co-ordinate state changes between other objects by using one object.

  2. When you need to add functionality to a class without changing its interface.

  3. When you need create a separation between abstractions and classes that implement those abstractions.

  4. You need a class that will be used in lots of different applications where the logic will only change slightly.


Correct Option: A

What are the benefits of using the Service Locator pattern?

  1. It is used primary by Servlets to locate other services such as JMS. A good example of this pattern would be a web based login facility. If a client enters an incorrect password their need to be redirected to a different but their session needs to be main

  2. Multiple clients can reuse the same Service Locator pattern.

  3. The Service Locator pattern can improve performance by introducing a caching facility.

  4. There is a slight loss of performance when using the Service Locator pattern but this is out weighed by the improved maintainability of the code.

  5. None of the definitions of the Service Locator pattern are accurate.

  6. The Service Locator pattern is used to hide the complexities of initial object creation, EJB lookups and object re-creation.


Correct Option: B,C,F

What is the difference between the abstract factory pattern and the factory method pattern?

  1. The factory method makes objects that should be used together. This is not the case for the abstract factory.

  2. The abstract factory pattern provides an interface for creating a family of objects whereas factory method provides an interface for creating one object.

  3. In the abstract factory pattern the objects that the factory makes are to be used together. This is not necessarily true in the factory method pattern.

  4. The factory method pattern is used when the class does not know the class of the object it must create. But in the abstract factory this is known in advance.

  5. The factory method and abstract factory are essentially the same pattern but two different names are used to describe them depending on the circumstances when they are implemented.


Correct Option: B

You're designing a paint application and as part of the user interface you have a toolbar along the left hand side of the screen. Each of the icons on the toolbar has different actions when you are using different tools. The way you've structured it, the application is required to pass commands from one object to another. When the appropriate object receives the command, it handles the request. This is an example of which pattern?

  1. Command

  2. Chain of Responsibility

  3. Adapter

  4. Interpreter

  5. Strategy


Correct Option: B

Which design pattern can be used to create a family of dependent objects?

  1. Factory Method

  2. Prototype

  3. Builder

  4. Abstract Factory

  5. Singleton


Correct Option: D

You can traverse through the elements of many Java Collection objects because they provide a way to access their elements sequentially. What design pattern is used here?

  1. Visitor

  2. Observer

  3. Builder

  4. Proxy

  5. Iterator


Correct Option: E

A Philadelphia based cable company is using J2EE for their Customer Management system. The system uses a combination of HTML and JSP for presentation. Java Servlets are used as Controllers. All Servlets have access to a ServletContext object, which functions as a Container to the shared resources of the Servlets. The ServletContext object best exemplifies what design pattern?

  1. Iterator

  2. Mediator

  3. Interpreter

  4. State

  5. Singleton


Correct Option: E

Compact Computers is a small computer assembly company. Its online application allows customers to pick and choose accessories to build their own PCs. The accessories are: i. Processor - 800Mhz, 1Ghz, 1.2Ghz ii. HDD - 40 GB, 60 GB, 80 GB iii. Memory - 128 MB, 256 MB, 512 MB Customers choose parts and quantities during the order. For example, a customer could choose a second HDD as a secondary hard drive or purchase additional RAM. What design pattern may be optimal for implementing a suitable design here?

  1. Factory Method

  2. Prototype

  3. Singleton

  4. Template Method

  5. Business Delegate

  6. Builder


Correct Option: B

AI Explanation

To answer this question, let's go through each design pattern option to understand why it is correct or incorrect:

A) Factory Method - The Factory Method pattern is used to create objects of a specific type. In this scenario, the Factory Method pattern may not be the optimal choice as it is more suitable for creating different types of objects based on a common interface or superclass.

B) Prototype - The Prototype pattern is used to create new objects by cloning existing objects. In this scenario, the Prototype pattern could be a suitable choice as it allows customers to select and customize their own PC accessories by cloning and modifying existing configurations.

C) Singleton - The Singleton pattern is used to ensure that only one instance of a class exists throughout the application. In this scenario, the Singleton pattern may not be the optimal choice as it does not directly address the requirement of allowing customers to pick and choose accessories for their PCs.

D) Template Method - The Template Method pattern is used to define the skeleton of an algorithm and allow subclasses to redefine certain steps of the algorithm without changing its structure. In this scenario, the Template Method pattern may not be the optimal choice as it focuses more on defining algorithms rather than allowing customers to pick and choose accessories.

E) Business Delegate - The Business Delegate pattern is used to decouple the client code from the implementation details of the business service. In this scenario, the Business Delegate pattern may not be the optimal choice as it does not directly address the requirement of allowing customers to pick and choose accessories for their PCs.

F) Builder - The Builder pattern is used to construct complex objects step by step. In this scenario, the Builder pattern could be a suitable choice as it allows customers to select and customize their own PC accessories by adding them one by one to build the final PC configuration.

Based on the given options, the most suitable design pattern for implementing the desired functionality is the Prototype pattern (Option B). This pattern allows customers to pick and choose accessories by cloning and modifying existing configurations.

Therefore, the correct answer is B) Prototype.

package constructors; class R { R() { } } class S extends R { int p ; S() { this(10); super(); System.out.println("I am in Q"); } S(int p) { this.p = p; System.out.println("I am in Q with Argument"); } } public class TestConstructor4 { Q q = new Q(); }

  1. Exception

  2. I am in Q I am in Q with Argument

  3. I am in Q

  4. Compiler Error


Correct Option: D

package constructors; class U { U() { System.out.println("I am in U"); } } class V extends U { void V() { System.out.println("I am in V"); } } public class TestConstructor5 { public static void main(String[] args) { V v = new V(); } }

  1. I am in U

  2. Compiler Error

  3. I am in V

  4. I am in U I am in V


Correct Option: A

package constructors; class P { P(String a) { System.out.println("I am in P "+a); } } class Q extends P { Q() { System.out.println("I am in Q"); } } public class TestConstructor2 { Q q = new Q(); }

  1. I am in P null I am in Q

  2. Compiler Error

  3. I am in P I am in Q

  4. I am in Q


Correct Option: B

package constructors; class M { int i; M() { System.out.println("I am in M, No argument Constructor"); this(10); } M(int i) { this.i = i; System.out.println("I am in M, Constructor with Argument"); } } public class TestConstructor3 { M m = new M(); }

  1. I am in M, No argument Constructor I am in M, Constructor with Argument

  2. I am in M, Constructor with Argument I am in M, No argument Constructor

  3. Compiler Error

  4. Exception


Correct Option: C
- Hide questions