Questions
18 of 24
1What are the SOLID principles? Explain each briefly.
2Explain the Single Responsibility Principle (SRP) with an example.
3Explain the Open/Closed Principle (OCP). How do you achieve it without modifying existing code?
4Explain the Liskov Substitution Principle (LSP). What happens if it's violated? (Square/Rectangle problem)
5Explain the Interface Segregation Principle (ISP). Why are fat interfaces bad?
6Explain the Dependency Inversion Principle (DIP). How does it relate to Dependency Injection?
7What is the difference between Aggregation and Composition?
8What is the Factory Pattern? How does it help in achieving OCP?
9What is the Abstract Factory Pattern? How does it differ from Factory Method?
10What is the Strategy Pattern? How does it replace complex conditional logic?
11What is the Observer Pattern? Where is it used in real life? (Event handling, Pub/Sub)
12What is the Decorator Pattern? How does it add functionality dynamically?
13What is a Singleton Pattern? What are the thread-safety concerns and how to handle them?
14What is an Anti-Pattern? Can you name a few? (God Object, Spaghetti Code, Golden Hammer)
15What is "Coupling" and "Cohesion"? Explain the relationship between them.
16What are Generics/Templates? How do they improve type safety and performance compared to casting?
17What is Type Erasure (Java) vs Reified Generics (C#)?
18What is Variance in Generics? (Covariance and Contravariance)
19What are Extension Methods (C#) or Default Methods (Java)? When should you use them?
20What is Reflection? How can it be used to break Encapsulation? What are the performance costs?
21What is the "Law of Demeter"? (Don't talk to strangers)
22Explain "Tell, Don't Ask" principle.
23What is the DRY principle? How does OOP help achieve it?
24What is the YAGNI principle?
18 / 24

What is Variance in Generics? (Covariance and Contravariance)

Difficulty: 5/10

Generic Variance

Variance describes how a generic type's compatibility changes when its type parameter has an inheritance relationship. Covariance allows a generic producer of a more derived type to be used where a producer of a base type is expected. Contravariance reverses the direction and is useful for consumers: a consumer capable of accepting a base type can generally be used where a consumer of a derived type is expected. In C#, interfaces and delegates can explicitly declare variance using out and in.

javascript
  1. 1

    Covariance is commonly associated with producers and uses an out-style relationship.

  2. 2

    Contravariance is commonly associated with consumers and uses an in-style relationship.

  3. 3

    C# explicitly supports variance annotations on certain generic interfaces and delegates.

  4. 4

    Java uses use-site wildcards such as ? extends T and ? super T to express variance.

  5. 5

    Mutable collections are generally invariant because allowing both reads and writes can violate type safety.

Follow-up Questions

  • Why are mutable lists usually invariant?
  • Explain PECS in Java.
  • What does out mean in C#?
  • What does in mean in C#?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.