LearnFrenzy
Sign in Take a free test

OOPs · Topic 9 of 10

SOLID and Class Relationships: questions with answers

The five SOLID principles, association, aggregation, composition and dependency in UML, multiplicities, coupling and cohesion, composition over inheritance and common design smells.

Questions
45
Timed sets
3
Answers run or computed
10
Your accuracy
–

What this topic covers

12 subtopics
  • Class relationships7
  • Dependency inversion7
  • Open/closed5
  • Coupling and cohesion4
  • Liskov substitution4
  • Design principles3
  • Design smells3
  • Interface segregation3
  • Single responsibility3
  • UML3
  • Composition over inheritance2
  • Responsibility assignment1
  • Beginner 15
  • Intermediate 15
  • Advanced 15

About this topic

SOLID is five guidelines for classes that are easy to change: Single responsibility, Open/closed, Liskov substitution, Interface segregation and Dependency inversion. Class relationships describe how objects connect: association (knows about), aggregation (has, but parts live independently – hollow diamond), composition (owns, parts die with the whole – filled diamond) and dependency (uses – dashed arrow). Most questions here show a UML class diagram or a short design scenario.

Free preview

Sample questions

Try each one first, then open the answer for the explanation and why the other options are wrong.

Question 1 Single responsibility Beginner Practice

What does the Single Responsibility Principle say?

Each method should be one line long
Only one object of a class may exist
A class should have only one method
A class should have only one reason to change – one responsibility, owned by one group of stakeholders
Show answer and explanation
Correct answer D

Explanation

If tax rules, PDF layout and database schema all live in one Invoice class, three different teams change it for three reasons. Splitting it keeps changes local.

Why the other options are wrong

  • AUnrelated.
  • BThat is the Singleton pattern.
  • CMethods per class is not the point.
Question 2 Liskov substitution Intermediate Practice Code executed

A read-only list extends ArrayList. What does this print?

Java Main.java
import java.util.*;

public class Main {
    static class ReadOnlyList<E> extends ArrayList<E> {
        ReadOnlyList(Collection<E> c) { super(c); }
        @Override public boolean add(E e) { throw new UnsupportedOperationException("read-only"); }
    }

    static int addDefault(List<String> list) {
        list.add("default");
        return list.size();
    }

    public static void main(String[] args) {
        System.out.print(addDefault(new ArrayList<>(List.of("a"))) + " ");
        try {
            System.out.print(addDefault(new ReadOnlyList<>(List.of("a"))));
        } catch (UnsupportedOperationException e) {
            System.out.print(e.getMessage());
        }
        System.out.println();
    }
}
Compile error
2 read-only
2 2
read-only read-only
Show answer and explanation
Correct answer B

Explanation

addDefault was written against List and works for a normal ArrayList, but the subclass refuses an operation the base type promises – a Liskov violation that only shows up at run time. Separate read-only and writable types (or composition) avoid it.

Why the other options are wrong

  • AIt compiles.
  • CThe subclass throws.
  • DThe plain list works.
Question 3 Open/closed Advanced Practice Code executed

New channels are added by wrapping, not by editing Notifier. What does this print?

Python main.py
class Notifier:
    def send(self, msg):
        return [f"email:{msg}"]

class SmsDecorator:
    def __init__(self, inner):
        self.inner = inner
    def send(self, msg):
        return self.inner.send(msg) + [f"sms:{msg}"]

class SlackDecorator:
    def __init__(self, inner):
        self.inner = inner
    def send(self, msg):
        return [f"slack:{msg}"] + self.inner.send(msg)

n = SlackDecorator(SmsDecorator(Notifier()))
print(n.send("down"))
['slack:down']
['email:down']
['email:down', 'sms:down', 'slack:down']
['slack:down', 'email:down', 'sms:down']
Show answer and explanation
Correct answer D

Explanation

Each decorator adds behaviour around the object it wraps, and they can be stacked in any combination at run time – no subclass per combination, no change to Notifier. Order matters: Slack prepends, SMS appends.

Why the other options are wrong

  • AInner notifiers still run.
  • BBoth decorators add entries.
  • CSlack adds its entry before the inner result.
42 more questions in the timed setsSame format as the samples, with a timer, a question palette and a full review at the end.
Start practising

OOPs

All 10 topics

Topics, levels and full mocks
  1. 01 Classes, Objects and Constructors45 questions · 3 levels
  2. 02 Encapsulation and Access Modifiers45 questions · 3 levels
  3. 03 Inheritance45 questions · 3 levels
  4. 04 Polymorphism45 questions · 3 levels
  5. 05 Abstract Classes and Interfaces45 questions · 3 levels
  6. 06 Object Lifecycle and Copying45 questions · 3 levels
  7. 07 static, final, this and super45 questions · 3 levels
  8. 08 Exception Handling45 questions · 3 levels
  9. 09 SOLID and Class Relationships45 questions · 3 levels
  10. 10 Design Patterns45 questions · 3 levels

Search LearnFrenzy

Esc

Quick links