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

Event-driven design pattern in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Event-Driven Mastery
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 event subscription example?

Consider this C# code where a class publishes an event and another class subscribes to it. What will be printed when TriggerEvent is called?

C Sharp (C#)
using System;

public class Publisher
{
    public event EventHandler? OnChange;

    public void TriggerEvent()
    {
        OnChange?.Invoke(this, EventArgs.Empty);
    }
}

public class Subscriber
{
    public void Subscribe(Publisher pub)
    {
        pub.OnChange += HandleEvent;
    }

    private void HandleEvent(object? sender, EventArgs e)
    {
        Console.WriteLine("Event received!");
    }
}

public class Program
{
    public static void Main()
    {
        Publisher pub = new Publisher();
        Subscriber sub = new Subscriber();
        sub.Subscribe(pub);
        pub.TriggerEvent();
    }
}
ANullReferenceException at runtime
BEvent received!
CNo output
DCompilation error due to event handler
Attempts:
2 left
💡 Hint

Think about what happens when TriggerEvent calls OnChange?.Invoke and if any subscriber is attached.

🧠 Conceptual
intermediate
1:30remaining
Which statement best describes the event-driven design pattern?

Choose the best description of the event-driven design pattern in programming.

AA pattern that requires polling to check for changes in state continuously.
BA pattern where functions are called sequentially without any event notifications.
CA pattern where components communicate by sending and receiving events asynchronously.
DA pattern that only works with synchronous method calls and no callbacks.
Attempts:
2 left
💡 Hint

Think about how components react to changes or actions in event-driven systems.

🔧 Debug
advanced
2:00remaining
What error does this event code produce?

Examine the following C# code snippet. What error will occur when compiling or running it?

C Sharp (C#)
using System;

public class Publisher
{
    public event EventHandler OnChange;

    public void TriggerEvent()
    {
        OnChange(this, EventArgs.Empty);
    }
}

public class Program
{
    public static void Main()
    {
        Publisher pub = new Publisher();
        pub.TriggerEvent();
    }
}
ANullReferenceException at runtime
BCompilation error: event must be invoked with ?. operator
CNo error, prints nothing
DInvalidOperationException at runtime
Attempts:
2 left
💡 Hint

Consider what happens if no subscriber is attached to the event and the event is invoked directly.

📝 Syntax
advanced
2:00remaining
Which option correctly declares and raises a custom event in C#?

Choose the correct code snippet that declares a custom event and raises it properly.

A
public event EventHandler MyEvent;

protected virtual void OnMyEvent()
{
    MyEvent?.Invoke(this, EventArgs.Empty);
}
B
public event EventHandler MyEvent;

protected void OnMyEvent()
{
    MyEvent.Invoke();
}
C
public event EventHandler MyEvent;

protected void OnMyEvent()
{
    MyEvent(this);
}
D
public EventHandler MyEvent;

protected void OnMyEvent()
{
    MyEvent(this, EventArgs.Empty);
}
Attempts:
2 left
💡 Hint

Remember that events should be declared with the event keyword and invoked safely.

🚀 Application
expert
2:30remaining
How many times will the event handler run in this scenario?

Given this C# code where the same handler is subscribed twice to an event, how many times will the handler run when the event is triggered once?

C Sharp (C#)
using System;

public class Publisher
{
    public event EventHandler? OnChange;

    public void TriggerEvent()
    {
        OnChange?.Invoke(this, EventArgs.Empty);
    }
}

public class Program
{
    public static void Main()
    {
        Publisher pub = new Publisher();
        EventHandler handler = (sender, e) => Console.WriteLine("Handler called");
        pub.OnChange += handler;
        pub.OnChange += handler;
        pub.TriggerEvent();
    }
}
A1
BCompilation error due to duplicate subscription
C0
D2
Attempts:
2 left
💡 Hint

Think about what happens if the same method is added twice to an event's invocation list.