0
0
C Sharp (C#)programming~20 mins

Aggregate functions (Count, Sum, Average) in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Aggregate Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Count example?

Consider this C# code that counts elements in a list:

var numbers = new List<int> {1, 2, 3, 4, 5};
int count = numbers.Count(n => n > 3);
Console.WriteLine(count);

What will be printed?

C Sharp (C#)
var numbers = new List<int> {1, 2, 3, 4, 5};
int count = numbers.Count(n => n > 3);
Console.WriteLine(count);
A2
B3
C5
D4
Attempts:
2 left
💡 Hint

Count counts how many items satisfy the condition.

Predict Output
intermediate
2:00remaining
What is the sum of these numbers?

Look at this code that sums numbers:

var values = new List<int> {10, 20, 30};
int total = values.Sum();
Console.WriteLine(total);

What is printed?

C Sharp (C#)
var values = new List<int> {10, 20, 30};
int total = values.Sum();
Console.WriteLine(total);
A0
B60
C100
D30
Attempts:
2 left
💡 Hint

Sum adds all numbers in the list.

Predict Output
advanced
2:00remaining
What is the average value computed here?

Analyze this code that calculates average:

var scores = new List<double> {4.0, 5.0, 6.0};
double avg = scores.Average();
Console.WriteLine(avg);

What will be printed?

C Sharp (C#)
var scores = new List<double> {4.0, 5.0, 6.0};
double avg = scores.Average();
Console.WriteLine(avg);
A5
B15
C4
D6
Attempts:
2 left
💡 Hint

Average is total sum divided by count.

Predict Output
advanced
2:00remaining
What error occurs when calling Average on an empty list?

Consider this code:

var emptyList = new List<int>();
double avg = emptyList.Average();
Console.WriteLine(avg);

What happens when this runs?

C Sharp (C#)
var emptyList = new List<int>();
double avg = emptyList.Average();
Console.WriteLine(avg);
A0
Bnull
CInvalidOperationException
DDivideByZeroException
Attempts:
2 left
💡 Hint

Average on empty collections throws an error.

🧠 Conceptual
expert
3:00remaining
How many items are counted here?

Given this code:

var data = new List<int> {1, 2, 3, 4, 5, 6};
int count = data.Count(n => n % 2 == 0 && n > 3);
Console.WriteLine(count);

How many items satisfy the condition?

C Sharp (C#)
var data = new List<int> {1, 2, 3, 4, 5, 6};
int count = data.Count(n => n % 2 == 0 && n > 3);
Console.WriteLine(count);
A4
B3
C1
D2
Attempts:
2 left
💡 Hint

Count numbers that are even and greater than 3.