Find Output of C# Programs - MCQ Questions with Answers

Here's a collection of 25 C# program snippets designed to test understanding various C# concepts. Each snippet is followed by a question about its output, with multiple-choice answers and explanations.

1. What will be the output of the following C# code?

using System;
class Program {
    static void Main() {
        Console.WriteLine("Hello, World!");
    }
}
a) Hello, World
b) Hello, World!
c) Compilation error
d) None of the above

2. What will be the output of the following C# code?

using System;
class Program {
    static void Main() {
        int x = 10, y = 20;
        Console.WriteLine(x + " + " + y + " = " + (x + y));
    }
}
a) 10 + 20 = 30
b) 1020 = 30
c) 30
d) Compilation error

3. What will be the output of the following C# code using a try-catch block?

using System;
class Program {
    static void Main() {
        try {
            int[] arr = new int[5];
            Console.WriteLine(arr[10]);
        } catch (IndexOutOfRangeException) {
                Console.WriteLine("Index was out of range!");
        }
    }
 }   
a) 0
b) Index was out of range!
c) Error
d) No output

4. What will be the output of the following C# code involving asynchronous programming?

using System;
using System.Threading.Tasks;
class Program {
    static async Task Main() {
        await Task.Run(() => {
            for (int i = 0; i < 5; i++) {
                Console.Write(i + " ");
            }
        });
    }
}
a) 0 1 2 3 4
b) 4 3 2 1 0
c) Random order of numbers from 0 to 4
d) Compilation error

5. What will be the output of the following C# code using string interpolation?

using System;
class Program {
    static void Main() {
        int age = 30;
        Console.WriteLine($"You are {age} years old.");
    }
}
a) You are 30 years old.
b) You are {age} years old.
c) You are age years old.
d) Compilation error

6. What will be the output of the following C# code using LINQ?

using System;
using System.Linq;
class Program {
    static void Main() {
        int[] numbers = { 1, 2, 3, 4, 5 };
        var result = numbers.Where(n => n > 3).Sum();
        Console.WriteLine(result);
    }
}
a) 5
b) 9
c) 12
d) 15

7. What will be the output of the following C# code with a custom exception?

using System;
class MyException : Exception {
public MyException(string message) : base(message) { }
}
class Program {
static void Main() {
    try {
        throw new MyException("Custom exception thrown");
        } catch (MyException ex) {
            Console.WriteLine(ex.Message);
        }
    }
}
a) Custom exception thrown
b) Error
c) Exception caught
d) None of the above

8. What will be the output of the following C# code using inheritance?

using System;
class Animal {
    public virtual void Speak() {
        Console.WriteLine("Animal speaks");
    }
}
class Dog : Animal {
    public override void Speak() {
        Console.WriteLine("Dog barks");
    }
}
class Program {
    static void Main() {
        Animal myAnimal = new Dog();
        myAnimal.Speak();
    }
}
a) Animal speaks
b) Dog barks
c) No output
d) Compilation error

9. What will be the output of the following C# code using constructors in a class hierarchy?

using System;
class Base {
    public Base() {
        Console.WriteLine("Base Constructor");
    }
}
class Derived : Base {
    public Derived() {
        Console.WriteLine("Derived Constructor");
    }
}
class Program {
    static void Main() {
        new Derived();
    }
}
a) Base Constructor \n Derived Constructor
b) Derived Constructor \n Base Constructor
c) Derived Constructor
d) Base Constructor

10. What will be the output of the following C# code using an interface?

using System;
interface IExample {
    void Show();
}
class Example : IExample {
    public void Show() {
        Console.WriteLine("Interface method implemented");
    }
}
class Program {
    static void Main() {
        IExample ex = new Example();
        ex.Show();
    }
}
a) Interface method implemented
b) Method Show called
c) Error
d) None of the above

11. What will be the output of the following C# code involving method overloading?

using System;
class Program {
    static void Print(int number) {
        Console.WriteLine("Integer: " + number);
    }
    static void Print(double number) {
        Console.WriteLine("Double: " + number);
    }
    static void Main() {
        Print(123);
        Print(123.456);
    }
}
a) Integer: 123 \n Double: 123.456
b) Double: 123 \n Integer: 123.456
c) Integer: 123 \n Integer: 123.456
d) Compilation error

12. What will be the output of the following C# code using a nullable type?

using System;
class Program {
    static void Main() {
        int? num = null;
        Console.WriteLine(num.HasValue ? num.Value.ToString() : "No value");
    }
}
a) 0
b) No value
c) null
d) Error

13. What will be the output of the following C# code using enum?

using System;
enum Days { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
class Program {
    static void Main() {
        Days day = Days.Wed;
        Console.WriteLine((int)day);
    }
}
a) Wed
b) 3
c) 2
d) 4

14. What will be the output of the following C# code using delegates?

using System;
delegate void MyDelegate(string msg);
class Program {
    static void ShowMessage(string message) {
        Console.WriteLine(message);
    }
    static void Main() {
        MyDelegate del = ShowMessage;
        del("Hello, Delegates!");
    }
}
a) Hello, Delegates!
b) ShowMessage
c) Error
d) None of the above

15. What will be the output of the following C# code involving a switch statement?

using System;
class Program {
    static void Main() {
        int number = 3;
        switch (number) {
            case 1:
            Console.WriteLine("One");
            break;
            case 2:
            Console.WriteLine("Two");
            break;
            case 3:
            Console.WriteLine("Three");
            break;
            default:
            Console.WriteLine("Default");
            break;
        }
    }
}
a) One
b) Two
c) Three
d) Default

16. What will be the output of the following C# code using array and foreach loop?

using System;
class Program {
    static void Main() {
        int[] numbers = { 1, 2, 3, 4, 5 };
        foreach (int number in numbers) {
            Console.Write(number + " ");
        }
    }
}
a) 1 2 3 4 5
b) 5 4 3 2 1
c) Error
d) None of the above

17. What will be the output of the following C# code using the DateTime structure?

using System;
class Program {
    static void Main() {
        DateTime dt = new DateTime(2020, 1, 1);
        Console.WriteLine(dt.ToString("yyyy-MM-dd"));
    }
}
a) 1/1/2020
b) 2020-01-01
c) 2020/1/1
d) Error

18. What will be the output of the following C# code using delegates?

using System;
delegate void Del(string message);
class Program {
    static void Main() {
        Del handler = DelegateMethod;
        handler("Hello, world!");
    }
    static void DelegateMethod(string message) {
        Console.WriteLine(message);
    }
}
a) Hello, world!
b) DelegateMethod
c) Error
d) None of the above

19. What will be the output of the following C# code using structure?

using System;
struct Point {
    public int X, Y;
    public Point(int x, int y) {
        X = x;
        Y = y;
    }
}
class Program {
    static void Main() {
        Point p = new Point(1, 2);
        Console.WriteLine("X: " + p.X + ", Y: " + p.Y);
    }
}
a) X: 1, Y: 2
b) X: 0, Y: 0
c) Error
d) None of the above

20. What will be the output of the following C# code using events?

using System;
public delegate void MyEventHandler(string message);
class MyEvent {
    public event MyEventHandler SomeEvent;
    public void RaiseEvent() {
        if (SomeEvent != null) {
            SomeEvent("Event triggered!");
        }
    }
}
class Program {
    static void Handler(string message) {
        Console.WriteLine(message);
    }
    static void Main() {
        MyEvent evt = new MyEvent();
        evt.SomeEvent += Handler;
        evt.RaiseEvent();
    }
}
a) Event triggered!
b) Handler
c) Error
d) None of the above

21. What will be the output of the following C# code using the async and await keywords?

using System;
using System.Threading.Tasks;
class Program {
    static async Task Main() {
        await Task.Run(() => Console.WriteLine("Task completed!"));
    }
}
a) Task completed!
b) Task
c) Error
d) None of the above

22. What will be the output of the following C# code using a static class and method?

using System;
static class Utilities {
    public static void Print() {
        Console.WriteLine("Printing from a static class!");
    }
}
class Program {
    static void Main() {
        Utilities.Print();
    }
}
a) Printing from a static class!
b) Print
c) Error
d) None of the above

23. What will be the output of the following C# code involving LINQ and Lambda expressions?

using System;
using System.Collections.Generic;
using System.Linq;
class Program {
    static void Main() {
        List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
        var filteredNumbers = numbers.Where(n => n % 2 == 0);
        foreach (var num in filteredNumbers) {
            Console.WriteLine(num);
        }
    }
}
a) 1 3 5
b) 2 4
c) 1 2 3 4 5
d) None of the above

24. What will be the output of the following C# code using nullable types?

using System;
class Program {
    static void Main() {
        int? num = null;
        Console.WriteLine(num ?? 0);
    }
}
a) 0
b) null
c) Error
d) None of the above

25. What will be the output of the following C# code using exception filtering?

using System;
class Program {
    static void Main() {
        try {
            throw new InvalidOperationException("Invalid operation");
        } catch (Exception ex) when (ex.Message.Contains("Invalid")) {
                Console.WriteLine("Caught invalid operation");
        }
    }
}
a) Caught invalid operation
b) Invalid operation
c) Error
d) None of the above

Comments