ProgrammingModerateProduct-company placement · GATE CS
Which call violates the Law of Demeter (talk only to your immediate collaborators) inside an OrderService method that receives an Order order?
- A. order.getTotal()
- B. order.getCustomer().getAddress().getCity()
- C. this.repository.save(order)
- D. order.shippingCity()
Correct answer: B. order.getCustomer().getAddress().getCity()
The chain reaches through Customer and Address, objects the service did not get directly, so it couples the service to their structure. Asking the order itself (order.shippingCity()) hides that structure.
More OOP with C++ and Java questions
- How many of these statements about Java methods are correct? 1. A final method can be overloaded in a subclass. 2. A static method can be…
- Classes A, B extends A and C extends B all override m(). Inside C, which way can call A's version of m() on the current object?
- Joshua Bloch recommends implementing a singleton as a single-element enum. Which advantage does this give over a class with a private…
- What is printed? cpp struct P { const char s; P(const char s) : s(s) { cout << s; } ~P() { cout << "~" << s; } }; struct B { P b{"B"}; }…
- What is printed? cpp struct B { virtual ~B() { hi(); } virtual void hi() { cout << "B"; } }; struct D : B { ~D() { hi(); } void hi()…
- What is printed? cpp struct B { virtual void f(int x = 1) { cout << "B" << x; } }; struct D : B { void f(int x = 2) override { cout << "D"…