ProgrammingModerateAmazon/Microsoft SDE MCQ · Java placement
A Printer interface declares print(), scan(), fax() and staple(). A cheap printer class must implement all four and throws UnsupportedOperationException from three of them. Which SOLID principle is violated most directly?
- A. Single Responsibility Principle
- B. Open/Closed Principle
- C. Dependency Inversion Principle
- D. Interface Segregation Principle
Correct answer: D. Interface Segregation Principle
Interface Segregation says clients should not be forced to depend on methods they do not use; the fix is smaller role interfaces such as Printable and Scannable. Throwing from inherited operations also hurts substitutability, but the root cause is the fat interface.
More OOP with C++ and Java questions
- What is printed? java String a = "Hi"; String b = new String("Hi"); String c = b.intern(); StringBuilder d = new StringBuilder("Hi")…
- What is printed? java abstract class Shape { Shape() { System.out.print(area() + " "); } abstract double area(); } class Sq extends Shape…
- What is printed? java List<Integer> l = new ArrayList<>(List.of(5, 6, 7)); Iterator<Integer> it = l.iterator(); while (it.hasNext()) if…
- What does this Java program print? class A { String n = "A"; String get() { return n; } } class B extends A { String n = "B"; String get()…
- What does this Java program print? class P { P() { init(); } void init() { System.out.print("P "); } } class C extends P { int x = 5; C()…
- What does this Java code print? Integer a = 127, b = 127, c = 128, d = 128; System.out.println((a == b) + " " + (c == d) + " " +…