C# Online Quiz

Welcome to our C# online quiz designed to test your knowledge of C#, a versatile programming language developed by Microsoft as part of its .NET initiative. This quiz covers a variety of topics, from basic syntax and data types to advanced concepts like delegates and events. Whether you are preparing for a job interview, a certification, or just want to challenge yourself, these multiple-choice questions are crafted to help enhance your understanding of C#.

1. What is the default value of the bool data type in C#?

a) 0
b) false
c) true
d) null

2. Which collection type in C# does not allow duplicate elements?

a) List
b) ArrayList
c) HashSet
d) Dictionary

3. What is the output of the following C# code?

using System;
class Program {
    static void Main() {
        int x = 10, y = 20;
        Console.WriteLine(x > y ? "X is greater" : "Y is greater");
    }
}
a) X is greater
b) Y is greater
c) 0
d) 1

4. How do you create a multi-dimensional array in C#?

a) int[,] arr = new int[3,3];
b) int[][] arr = new int[3][3];
c) int[] arr = new int{3,3};
d) int(,) arr = new int[3,3];

5. What does the following C# code snippet output?

using System;
public class Program {
    public static void Main() {
        int i = 1;
        i += 2;
        Console.WriteLine(i);
    }
}
a) 1
b) 2
c) 3
d) 4

6. In C#, which keyword is used to specify that a class cannot be inherited?

a) static
b) Sealed
c) Final
d) Const

7. What will the following C# code snippet return?

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

8. What is the purpose of the Dispose method in C#?

a) To allocate memory
b) To release unmanaged resources
c) To delete objects from memory
d) To remove temporary files

9. Which of the following correctly declares a delegate in C#?

a) delegate int MyDelegate(string s);
b) delegate void MyDelegate(string s);
c) function MyDelegate(string s);
d) MyDelegate function(string s);

10. What is the output of the following C# code?

using System;
public class Program {
    public static void Main() {
        int[] numbers = { 1, 2, 3, 4, 5 };
        Console.WriteLine(numbers.Length);
    }
}
a) 4
b) 5
c) 6
d) Error

11. In C#, which method is used to compare two strings for equality?

a) String.Compare()
b) String.Equals()
c) String.Same()
d) String.Equality()

12. How do you handle exceptions in C#?

a) try {} catch {}
b) try {} except {}
c) do {} while (exception)
d) if (exception) {}

13. What does the static keyword indicate when used with a method in C#?

a) The method can be called without an object
b) The method can handle static data only
c) The method will not throw exceptions
d) The method can be overridden

14. What is encapsulation in C#?

a) The process of combining elements to create a new entity
b) The inclusion of one class within another
c) The mechanism of restricting direct access to some of the object's components
d) A way to transmit data using encryption

15. What will the following C# code snippet output?

using System;
public class Program {
    public static void Main() {
        double result = 10.0 / 4;
        Console.WriteLine(result);
    }
}
a) 2
b) 2.5
c) 2.0
d) 3

16. What is polymorphism in C#?

a) Changing the way something is viewed
b) Allowing methods to do more than one thing
c) Using a single symbol to represent multiple different types
d) None of the above

17. What is the output of the following C# code?

using System;
public class Program {
    public static void Main() {
        int x = 10;
        int y = 0;
        int z = x / y;
        Console.WriteLine(z);
    }
}
a) 0
b) 10
c) An exception is thrown
d) 1

18. What does the following C# code snippet output?

using System;
public class Program {
    public static void Main() {
        char[] letters = {'a', 'b', 'c', 'd', 'e'};
        Console.WriteLine(letters[0]);
    }
}
a) a
b) b
c) c
d) d

19. Which of the following is the correct way to declare a nullable int in C#?

a) int? num = null;
b) nullable int num = null;
c) int num = null;
d) int num? = null;

20. How can you ensure a method in C# is overridden in a derived class?

a) Use the final keyword on the method.
b) Use the static keyword on the method.
c) Use the virtual keyword on the method in the base class and override in the derived class.
d) Use the sealed keyword on the method.

21. What is the output of the following C# code?

using System;
public class Program {
    public static void Main() {
        Console.WriteLine("C# Quiz" + " Test");
    }
}
a) C#QuizTest
b) C# Quiz Test
c) C#Test
d) C#Quiz

22. What does the finally block do in a C# try-catch statement?

a) It runs only if an exception is not caught.
b) It runs only if an exception is caught.
c) It runs regardless of whether an exception is caught or not.
d) It prevents the propagation of an exception.

23. What does the ?? operator do in C#?

a) It is a bitwise operator.
b) It checks for nullability and returns the left-hand operand if it is not null; otherwise, it returns the right-hand operand.
c) It compares two variables for equality.
d) It increments a variable by one.

24. How do you declare an abstract class in C#?

a) Using the abstract keyword before the class definition.
b) Using the virtual keyword before the class definition.
c) Using the sealed keyword before the class definition.
d) Using the static keyword before the class definition.

25. What is the output of the following C# code?

using System;
public class Program {
    public static void Main() {
        int? num = null;
        Console.WriteLine(num.HasValue);
    }
}
a) true
b) false
c) null
d) 0

Comments