In a language that supports the OOP paradigm, we have the following code that serializes the content of a shopping cart to JSON format:

class ShoppingCart
  private content : Dictionary<Int, String> 

  public function serialize() : String
    return new JsonSerializer().serialize(content.clone())
  end function
end class

class JsonSerializer  
  public function serialize(value : Dictionary<Int, String>) : String
    // Code that serializes dictionary to JSON format and returns it as string
  end function
end class

A client wants to allow loosely coupled plugins to be able to serialize the shopping cart content to their own formats (e.g., XML). Select lines of code that, together, would extend the code above to allow this.

(Select all acceptable answers.)

class JsonSerializer
Should be changed to:
class JsonSerializer inherits SerializerInterface
Add the interface definition:
interface SerializerInterface
  public function serialize(Dictionary<Int, String>) : String
end interface
Add a definition for an enumeration that will be extended as new plugins are added (e.g., to add XML serialization):
enumeration SerializerType
end enumeration
Change the ShoppingCart's serialize implementation, so that it’s like the one below (e.g., to add XML serialization):
public function serialize(serializer : SerializerType) : String
  if(serializer == SerializerType.json)
    return new JsonSerializer().serialize(content.clone())
  else if(serializer == SerializerType.xml)
    return new XmlSerializer().serialize(content.clone())
    throw Error("Not implemented error!");
  end if    
end function
Change the ShoppingCart's serialize implementation to:
public function serialize(serializer : SerializerInterface) : String
  return serializer.serialize(content.clone())
end function
Change ShoppingCart's access modifiers of data members from private to public. Such as:
private content : Dictionary<Int, String>
public content : Dictionary<Int, String>
so that plugins can directly manipulate and save the shopping cart.

OOP & Design Patterns Inheritance Public


Would you like to see our other questions?

We have 1000+ premium hand-crafted questions for 160+ job skills and 20+ coding languages. We prefer questions with small samples of actual work over academic problems or brain teasers.

Visit our question library
Private Concierge

Send us an email with an explanation of your testing needs and a list of candidates. We will create an appropriate test, invite your candidates, review their results, and send you a detailed report.

Contact Private Concierge

Would you like to see our tests? The following tests contain OOP & Design Patterns related questions:
On the TestDome Blog

Screening Applicants: The Good, the Bad and the Ugly

Since we’re all biased and we use incorrect proxies, why not just outsource hiring to experts or recruitment agencies? After all, they’ve been screening people for many years, so they must know how to do it right?

Not really. I was surprised to discover that many experts disagree with each other. Everybody praises their pet method and criticizes the others. Many of these methods look legitimate, but are based on...

Dashboard Start Trial Sign In Home Tour Tests Questions Pricing