Thuta Learning
C#
ExercisesProgrammingBeginner

Practice Exercises: OOP + Collections + LINQ

C#Lesson 3340 min

What you'll walk away with

  • Explain the core concept and runtime behavior of Practice Exercises: OOP + Collections + LINQ
  • Test normal, boundary, and invalid cases independently
  • Write readable, type-safe, and testable C# code

Explain it simply

Practice Exercises: OOP + Collections + LINQ, in plain language

This exercise set is designed to combine and practice the OOP concepts (classes, inheritance, polymorphism), collections (List, Dictionary), and LINQ covered in the Intermediate and Advanced chapters. These tasks are tougher than Practice Exercises: Fundamentals — you'll need to design your own classes and query collections with LINQ. Once you can get through this level, you'll feel a lot more confident tackling mini-projects. Build and run each task as its own separate console app project.

Problem → Why → Solution

The problem — why this concept matters

Counting more classes and interfaces as better OOP leads to over-engineering.

A common mistake

Assuming one successful happy path proves the code is correct, maintainable, and production-ready.

Core concept — understand how it works

OOP and collection exercises should require decisions about responsibility, invariants, interface contracts, collection choice, and query execution. Express behavior in tests and justify inheritance or composition choices.

Mental model
  1. requirement
  2. reason and implement
  3. test and explain

Explain the core concept and runtime behavior of Practice Exercises: OOP + Collections + LINQ

Core syntax or pattern

csharp
// Task 1 starter
class Animal
{
    public string Name { get; set; }
    public virtual void MakeSound()
    {
        Console.WriteLine($"{Name} makes a sound");
    }
}

class Dog : Animal
{
    public override void MakeSound() => Console.WriteLine($"{Name} says Woof!");
}

class Cat : Animal
{
    public override void MakeSound() => Console.WriteLine($"{Name} says Meow!");
}

// Task 2 starter
Dictionary<string, int> stock = new Dictionary<string, int>
{
    { "Pen", 25 }, { "Notebook", 8 }, { "Eraser", 3 }, { "Ruler", 40 }
};
var lowStock = stock.Where(s => s.Value < 10);
foreach (var item in lowStock)
    Console.WriteLine($"{item.Key}: {item.Value}");

// Task 3 starter
class Student
{
    public string Name { get; set; }
    public List<int> Scores { get; set; }
}

List<Student> students = new List<Student>
{
    new Student { Name = "Aye", Scores = new List<int> { 80, 90, 70 } },
    new Student { Name = "Min", Scores = new List<int> { 60, 75, 65 } }
};
// use students.OrderByDescending(s => s.Scores.Average()) to find the top student

OOP and collection exercises should require decisions about responsibility, invariants, interface contracts, collection choice, and query execution. Express behavior in tests and justify inheritance or composition choices.

Read the code step by step

  1. requirement// Task 1 starter

    This exercise set is designed to combine and practice the OOP concepts (classes, inheritance, polymorphism), collections (List, Dictionary), and LINQ covered in the Intermediate and Advanced chapters. These tasks are tougher than Practice Exercises: Fundamentals — you'll need to design your own classes and query collections with LINQ. Once you can get through this level, you'll feel a lot more confident tackling mini-projects. Build and run each task as its own separate console app project.

  2. reason and implementDictionary<string, int> stock = new Dictionary<string...

    OOP and collection exercises should require decisions about responsibility, invariants, interface contracts, collection choice, and query execution. Express behavior in tests and justify inheritance or composition choices.

  3. test and explain// use students.OrderByDescending(s => s.Scores.Avera...

    Build an inventory system with a Product entity, repository interface, dictionary lookup, and LINQ low-stock report, testing duplicates, missing items, and negative stock.

Execution flow
  1. requirement
  2. reason and implement
  3. test and explain

Success

Explain the core concept and runtime behavior of Practice Exercises: OOP + Collections + LINQ

Failure

Counting more classes and interfaces as better OOP leads to over-engineering.

Let's think about this for a second

This exercise set is designed to combine and practice the OOP concepts (classes, inheritance, polymorphism), collections (List, Dictionary), and LINQ covered in the Intermediate and Advanced chapters. These tasks are tougher than Practice Exercises: Fundamentals — you'll need to design your own classes and query collections with LINQ. Once you can get through this level, you'll feel a lot more confident tackling mini-projects. Build and run each task as its own separate console app project.

Exercises

(1) Write an Animal base class with a Name property and a virtual MakeSound() method, then create two subclasses, Dog and Cat, that override MakeSound(). Add Dog and Cat objects to a List<Animal>, loop through it calling MakeSound(), and confirm that polymorphism is working as expected. (2) Set up a Dictionary<string, int> with product name as the key and stock quantity as the value, then use LINQ's Where() to find and print all products with stock below 10. (3) Write a Student class (Name, List<int> Scores), calculate each student's average score in a List<Student> using LINQ's Average(), and use OrderByDescending() to find the student with the highest average.

Code Example

csharp
// Task 1 starter
class Animal
{
    public string Name { get; set; }
    public virtual void MakeSound()
    {
        Console.WriteLine($"{Name} makes a sound");
    }
}

class Dog : Animal
{
    public override void MakeSound() => Console.WriteLine($"{Name} says Woof!");
}

class Cat : Animal
{
    public override void MakeSound() => Console.WriteLine($"{Name} says Meow!");
}

// Task 2 starter
Dictionary<string, int> stock = new Dictionary<string, int>
{
    { "Pen", 25 }, { "Notebook", 8 }, { "Eraser", 3 }, { "Ruler", 40 }
};
var lowStock = stock.Where(s => s.Value < 10);
foreach (var item in lowStock)
    Console.WriteLine($"{item.Key}: {item.Value}");

// Task 3 starter
class Student
{
    public string Name { get; set; }
    public List<int> Scores { get; set; }
}

List<Student> students = new List<Student>
{
    new Student { Name = "Aye", Scores = new List<int> { 80, 90, 70 } },
    new Student { Name = "Min", Scores = new List<int> { 60, 75, 65 } }
};
// use students.OrderByDescending(s => s.Scores.Average()) to find the top student
You should see
All three tasks will produce the expected output: the correct Dog/Cat sounds appearing, the list of low-stock products, and the student with the highest average score identified.

5-Minute Challenge

Add a Bird class to Task 1, override MakeSound(), include a Bird object in the List<Animal>, and re-run the loop to see it in action (5 minutes).

A Quick Heads-Up

Remember that polymorphism only works when you mark the base class method virtual AND mark the derived class method override — you need both.

Real-world usage

How to apply it in a real project

Build an inventory system with a Product entity, repository interface, dictionary lookup, and LINQ low-stock report, testing duplicates, missing items, and negative stock.

Mini challenge

Build it yourself

Build an inventory system with a Product entity, repository interface, dictionary lookup, and LINQ low-stock report, testing duplicates, missing items, and negative stock.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
csharp
// Task 1 starter
class Animal
{
    public string Name { get; set; }
    public virtual void MakeSound()
    {
        Console.WriteLine($"{Name} makes a sound");
    }
}

class Dog : Animal
{
    public override void MakeSound() => Console.WriteLine($"{Name} says Woof!");
}

class Cat : Animal
{
    public override void MakeSound() => Console.WriteLine($"{Name} says Meow!");
}

// Task 2 starter
Dictionary<string, int> stock = new Dictionary<string, int>
{
    { "Pen", 25 }, { "Notebook", 8 }, { "Eraser", 3 }, { "Ruler", 40 }
};
var lowStock = stock.Where(s => s.Value < 10);
foreach (var item in lowStock)
    Console.WriteLine($"{item.Key}: {item.Value}");

// Task 3 starter
class Student
{
    public string Name { get; set; }
    public List<int> Scores { get; set; }
}

List<Student> students = new List<Student>
{
    new Student { Name = "Aye", Scores = new List<int> { 80, 90, 70 } },
    new Student { Name = "Min", Scores = new List<int> { 60, 75, 65 } }
};
// use students.OrderByDescending(s => s.Scores.Average()) to find the top student

Expected: Explain the core concept and runtime behavior of Practice Exercises: OOP + Collections + LINQ

Show hint

Build an inventory system with a Product entity, repository interface, dictionary lookup, and LINQ low-stock report, testing duplicates, missing items, and negative stock.

Quick quiz 1 / 3

What best describes the core idea of Practice Exercises: OOP + Collections + LINQ?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • requirement → reason and implement → test and explain
  • OOP and collection exercises should require decisions about responsibility, invariants, interface contracts, collection choice, and query execution. Express behavior in tests and justify inheritance or composition choices.
  • Counting more classes and interfaces as better OOP leads to over-engineering.
  • Build an inventory system with a Product entity, repository interface, dictionary lookup, and LINQ low-stock report, testing duplicates, missing items, and negative stock.

Unit testing C# in .NET — Microsoft Learn

Easy traps

  • Counting more classes and interfaces as better OOP leads to over-engineering.
  • Assuming one successful happy path proves the code is correct, maintainable, and production-ready.

Hands-on Exercise

Build an inventory system with a Product entity, repository interface, dictionary lookup, and LINQ low-stock report, testing duplicates, missing items, and negative stock.

You'll know it worked when: All three tasks will produce the expected output: the correct Dog/Cat sounds appearing, the list of low-stock products, and the student with the highest average score identified.

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Practice Exercises: OOP + Collections + LINQ | Thuta Learning