C# Practice Questions
Topics Covered:
C# Variables, Data Types, Functions, Classes, Loops, LINQ, and more
30 Questions
Practice Questions
1. What will be the output of the following C# code?
using System;
public class Program {
public static void Main() {
int i = 1;
while(true) {
if(i % 3 == 0)
break;
Console.Write(i + " ");
i++;
}
}
}
2. Which symbol is used for single-line comments in C#?
3. What are the outputs of the following C# code snippet?
using System;
public class Program {
public static void Main() {
Console.WriteLine(Math.Pow(2, Math.Pow(3, 2)));
Console.WriteLine(Math.Pow(Math.Pow(2, 3), 2));
}
}
4. What is the output of the following C# code?
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
Dictionary dict = new Dictionary();
void InsertItem(string key) {
if(dict.ContainsKey(key))
dict[key]++;
else
dict[key] = 1;
}
InsertItem("Key1");
InsertItem("Key2");
InsertItem("Key2");
InsertItem("Key3");
InsertItem("Key1");
Console.WriteLine(dict.Count);
}
}
5. Which of the following lambda expressions correctly returns true for negative numbers in C#?
6. What is the output of the following C# code?
using System;
public class Human {
public string HumanName;
public Human(string name) { HumanName = name; }
public virtual bool IsEmployee() { return false; }
}
public class IBEmployee : Human {
public string EmpId;
public IBEmployee(string name, string id) : base(name) { EmpId = id; }
public override bool IsEmployee() { return true; }
}
public class Program {
public static void Main() {
IBEmployee employee = new IBEmployee("Mr Employee", "IB007");
Console.WriteLine(employee.HumanName + " " + employee.IsEmployee() + " " + employee.EmpId);
}
}
7. Which property returns the current local date and time in C#?
8. Which method is used to read all lines from a text file in C#?
9. Which of the following statements about C# namespaces is false?
10. Which of the following is the correct way to declare a lambda expression in C# that doubles its input?
11. How can you check if a List in C# is empty?
12. What is the output of the following C# code?
using System;
public class X {
private int num1;
public int num2;
public X() {
num1 = 5;
num2 = 2;
}
public void DisplayValues() {
Console.WriteLine(num1 + " " + num2);
}
}
public class Y : X {
private int num1;
public Y() : base() {
num1 = 1;
num2 = 6;
}
}
public class Program {
public static void Main() {
Y obj = new Y();
obj.DisplayValues();
}
}
13. What is the key difference between arrays and Lists in C#?
14. Which of the following statements about constructors in C# is false?
15. Given two arrays:
char[] list1 = {'s','r','a','s'} and char[] list2 = {'a','a','n','h'}, what is the result of concatenating corresponding elements into a new array of strings?
16. Which namespace in .NET is used for XML serialization?
17. Which LINQ method can be used to count the number of elements in a collection that satisfy a condition?
18. What is the output of the following C# recursive method?
using System;
public class Program {
public static int Test(int x) {
if (x == 0)
return 0;
return x + Test(x - 1);
}
public static void Main() {
Console.WriteLine(Test(5));
}
}
19. Given
List<int> list = new List<int> {3,4,5,2,1,0}, what is the content of the list after executing list.RemoveAt(1)?
20. Which build tool is commonly used for C# projects?
21. Which of the following is the correct syntax for defining a method with an optional parameter in C#?
public void MyMethod(int x, int y = 0) {
// method body
}
22. Which of the following is the correct way to declare a method in C#?
23. What is the result of the expression
4 + 3 * 2 in C#?
24. Which of the following is NOT a feature of C#?
25. Which of the following data types is immutable in C#?
26. What will be the output of the following C# code that calculates the factorial recursively?
using System;
public class Program {
public static int Factorial(int n) {
if(n <= 1)
return 1;
else
return n * Factorial(n - 1);
}
public static void Main() {
Console.WriteLine(Factorial(5));
}
}
27. What is the maximum possible length of an identifier in C#?
28. What is the output of the following C# code using LINQ's Select method?
using System;
using System.Linq;
public class Program {
public static void Main() {
int[] x = {0, 1, 4, 9, 16};
var y = x.Select(a => a + 2);
Console.WriteLine(string.Join(" ", y));
}
}
29. Which of the following variable declarations defines a mutable integer in C#?
30. What will be the output of the following C# code that uses a static field?
using System;
public class Program {
static int count = 0;
public static void Increment() {
count++;
Console.Write(count + " ");
}
public static void Main() {
Increment();
Increment();
Increment();
}
}