100 Practice Questions with Answers in C#
From Beginner to Intermediate + OOP • Easy and Clear Solutions
This document contains 100 practice questions with direct answers in C# language. The questions are
organized by topic so you can practice step by step.
Hello World and Basics
1. 1. Write a program to print Hello World.
using System;
class Program
{
static void Main()
{
[Link]("Hello, World!");
}
}
2. 2. Write a program to print your name.
using System;
class Program
{
static void Main()
{
string name = "Ali";
[Link](name);
}
}
3. 3. Take name input and print it.
using System;
class Program
{
static void Main()
{
string name = [Link]();
[Link](name);
}
}
4. 4. Take one integer input and print it.
using System;
class Program
{
static void Main()
{
int num = [Link]([Link]());
[Link](num);
}
}
5. 5. Add two numbers and print result.
using System;
class Program
{
static void Main()
{
int a = [Link]([Link]());
int b = [Link]([Link]());
[Link](a + b);
}
}
6. 6. Subtract two numbers.
using System;
class Program
{
static void Main()
{
int a = [Link]([Link]());
int b = [Link]([Link]());
[Link](a - b);
}
}
7. 7. Multiply two numbers.
using System;
class Program
{
static void Main()
{
int a = [Link]([Link]());
int b = [Link]([Link]());
[Link](a * b);
}
}
8. 8. Divide two numbers.
using System;
class Program
{
static void Main()
{
double a = [Link]([Link]());
double b = [Link]([Link]());
[Link](a / b);
}
}
9. 9. Find remainder of two numbers.
using System;
class Program
{
static void Main()
{
int a = [Link]([Link]());
int b = [Link]([Link]());
[Link](a % b);
}
}
10. 10. Print different data types.
using System;
class Program
{
static void Main()
{
int x = 10;
double y = 5.5;
char z = "A"[0];
bool flag = true;
[Link](x);
[Link](y);
[Link](z);
[Link](flag);
}
}
Typecasting
11. 11. Convert double to int.
using System;
class Program
{
static void Main()
{
double price = 19.99;
int value = (int)price;
[Link](value);
}
}
12. 12. Convert int to double.
using System;
class Program
{
static void Main()
{
int num = 5;
double value = (double)num;
[Link](value);
}
}
13. 13. Convert int to string.
using System;
class Program
{
static void Main()
{
int age = 20;
string text = [Link]();
[Link](text);
}
}
14. 14. Convert string to int.
using System;
class Program
{
static void Main()
{
string x = "10";
int y = [Link](x);
[Link](y);
}
}
15. 15. Convert string to double.
using System;
class Program
{
static void Main()
{
string x = "5.5";
double y = [Link](x);
[Link](y);
}
}
16. 16. Convert char to int.
using System;
class Program
{
static void Main()
{
char ch = "A"[0];
int value = (int)ch;
[Link](value);
}
}
17. 17. Convert int to char.
using System;
class Program
{
static void Main()
{
int value = 66;
char ch = (char)value;
[Link](ch);
}
}
18. 18. Convert input string to int.
using System;
class Program
{
static void Main()
{
string text = [Link]();
int num = [Link](text);
[Link](num);
}
}
19. 19. Safe string to int using try-catch.
using System;
class Program
{
static void Main()
{
string value = "123";
try
{
int num = [Link](value);
[Link](num);
}
catch (FormatException)
{
[Link]("Invalid number");
}
}
}
20. 20. Convert int division into decimal result.
using System;
class Program
{
static void Main()
{
int a = 5, b = 2;
double result = (double)a / b;
[Link](result);
}
}
Conditional Statements
21. 21. Check even or odd.
using System;
class Program
{
static void Main()
{
int num = [Link]([Link]());
if (num % 2 == 0) [Link]("Even"); else [Link]("Odd");
}
}
22. 22. Check positive, negative, or zero.
using System;
class Program
{
static void Main()
{
int num = [Link]([Link]());
if (num > 0) [Link]("Positive"); else if (num < 0) [Link]("Negative");
else [Link]("Zero");
}
}
23. 23. Check voting eligibility.
using System;
class Program
{
static void Main()
{
int age = [Link]([Link]());
if (age >= 18) [Link]("Eligible"); else [Link]("Not Eligible");
}
}
24. 24. Find greater of two numbers.
using System;
class Program
{
static void Main()
{
int a = [Link]([Link]());
int b = [Link]([Link]());
if (a > b) [Link](a); else if (b > a) [Link](b); else
[Link]("Equal");
}
}
25. 25. Find greatest of three numbers.
using System;
class Program
{
static void Main()
{
int a = [Link]([Link]());
int b = [Link]([Link]());
int c = [Link]([Link]());
if (a >= b && a >= c) [Link](a); else if (b >= a && b >= c) [Link](b);
else [Link](c);
}
}
26. 26. Grade calculator.
using System;
class Program
{
static void Main()
{
int marks = [Link]([Link]());
if (marks >= 80) [Link]("A"); else if (marks >= 60) [Link]("B"); else
if (marks >= 40) [Link]("C"); else [Link]("Fail");
}
}
27. 27. Check leap year.
using System;
class Program
{
static void Main()
{
int year = [Link]([Link]());
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) [Link]("Leap Year");
else [Link]("Not Leap Year");
}
}
28. 28. Simple login system.
using System;
class Program
{
static void Main()
{
string username = [Link]();
string password = [Link]();
if (username == "admin" && password == "1234") [Link]("Login successful"); else
[Link]("Invalid login");
}
}
29. 29. Check character type.
using System;
class Program
{
static void Main()
{
char ch = [Link]([Link]());
if ([Link](ch)) [Link]("Digit"); else if ([Link](ch))
[Link]("Alphabet"); else [Link]("Special Character");
}
}
30. 30. Find absolute value using if.
using System;
class Program
{
static void Main()
{
int num = [Link]([Link]());
if (num < 0) [Link](-num); else [Link](num);
}
}
Loops
31. 31. Print numbers from 1 to 10 using for.
using System;
class Program
{
static void Main()
{
for (int i = 1; i <= 10; i++)
[Link](i);
}
}
32. 32. Print numbers from 10 to 1 using while.
using System;
class Program
{
static void Main()
{
int i = 10;
while (i >= 1)
{
[Link](i);
i--;
}
}
}
33. 33. Print even numbers from 1 to 20.
using System;
class Program
{
static void Main()
{
for (int i = 1; i <= 20; i++)
if (i % 2 == 0) [Link](i);
}
}
34. 34. Print odd numbers from 1 to 20.
using System;
class Program
{
static void Main()
{
for (int i = 1; i <= 20; i++)
if (i % 2 != 0) [Link](i);
}
}
35. 35. Sum first 10 natural numbers.
using System;
class Program
{
static void Main()
{
int total = 0;
for (int i = 1; i <= 10; i++) total += i;
[Link](total);
}
}
36. 36. Multiplication table of a number.
using System;
class Program
{
static void Main()
{
int num = [Link]([Link]());
for (int i = 1; i <= 10; i++)
[Link](num + " x " + i + " = " + (num * i));
}
}
37. 37. Factorial using while loop.
using System;
class Program
{
static void Main()
{
int num = [Link]([Link]());
int fact = 1;
int i = 1;
while (i <= num)
{
fact *= i;
i++;
}
[Link](fact);
}
}
38. 38. Count vowels in a string.
using System;
class Program
{
static void Main()
{
string text = [Link]().ToLower();
int count = 0;
foreach (char ch in text)
if (ch == "a"[0] || ch == "e"[0] || ch == "i"[0] || ch == "o"[0] || ch == "u"[0])
count++;
[Link](count);
}
}
39. 39. Prime number check.
using System;
class Program
{
static void Main()
{
int num = [Link]([Link]());
bool isPrime = true;
if (num < 2) isPrime = false;
for (int i = 2; i <= [Link](num); i++)
{
if (num % i == 0)
{
isPrime = false;
break;
}
}
[Link](isPrime ? "Prime" : "Not Prime");
}
}
40. 40. Print star pattern.
using System;
class Program
{
static void Main()
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
[Link]("*");
[Link]();
}
}
}
Arrays, Lists, Tuples, Sets, Dictionary
41. 41. Create and print an array.
using System;
class Program
{
static void Main()
{
int[] numbers = { 10, 20, 30, 40 };
foreach (int num in numbers) [Link](num);
}
}
42. 42. Access first and last element of array.
using System;
class Program
{
static void Main()
{
string[] fruits = { "apple", "banana", "mango" };
[Link](fruits[0]);
[Link](fruits[2]);
}
}
43. 43. Change second element of array.
using System;
class Program
{
static void Main()
{
string[] fruits = { "apple", "banana", "mango" };
fruits[1] = "orange";
[Link](fruits[1]);
}
}
44. 44. Find length of array.
using System;
class Program
{
static void Main()
{
int[] numbers = { 1, 2, 3, 4 };
[Link]([Link]);
}
}
45. 45. Find sum of array.
using System;
class Program
{
static void Main()
{
int[] numbers = { 5, 10, 15, 20 };
int sum = 0;
foreach (int num in numbers) sum += num;
[Link](sum);
}
}
46. 46. Create and print List<T>.
using System;
using [Link];
class Program
{
static void Main()
{
List<int> numbers = new List<int> { 10, 20, 30 };
foreach (int num in numbers) [Link](num);
}
}
47. 47. Add and remove in List<T>.
using System;
using [Link];
class Program
{
static void Main()
{
List<string> fruits = new List<string> { "apple", "banana", "mango" };
[Link]("orange");
[Link]("banana");
foreach (string fruit in fruits) [Link](fruit);
}
}
48. 48. Create and use tuple.
using System;
class Program
{
static void Main()
{
(string name, int age) student = ("Ali", 20);
[Link]([Link]);
[Link]([Link]);
}
}
49. 49. Create and print HashSet.
using System;
using [Link];
class Program
{
static void Main()
{
HashSet<int> set = new HashSet<int> { 1, 2, 2, 3 };
foreach (int num in set) [Link](num);
}
}
50. 50. Create and print Dictionary.
using System;
using [Link];
class Program
{
static void Main()
{
Dictionary<string, int> student = new Dictionary<string, int>();
[Link]("age", 20);
[Link]("marks", 85);
foreach (var item in student) [Link]([Link] + " : " + [Link]);
}
}
More Collection Practice
51. 51. Find maximum in array.
using System;
class Program
{
static void Main()
{
int[] numbers = { 50, 10, 30, 20, 40 };
int max = numbers[0];
foreach (int num in numbers) if (num > max) max = num;
[Link](max);
}
}
52. 52. Search item in array.
using System;
class Program
{
static void Main()
{
string[] items = { "pen", "book", "bag" };
bool found = false;
foreach (string item in items) if (item == "book") found = true;
[Link](found ? "Found" : "Not Found");
}
}
53. 53. Count occurrences in array.
using System;
class Program
{
static void Main()
{
int[] numbers = { 1, 2, 2, 3, 2, 4 };
int count = 0;
foreach (int num in numbers) if (num == 2) count++;
[Link](count);
}
}
54. 54. 2D array print.
using System;
class Program
{
static void Main()
{
int[,] matrix = { {1,2,3}, {4,5,6}, {7,8,9} };
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
[Link](matrix[i, j] + " ");
[Link]();
}
}
}
55. 55. Remove duplicates using HashSet.
using System;
using [Link];
class Program
{
static void Main()
{
List<int> numbers = new List<int> { 1, 2, 2, 3, 4, 4 };
HashSet<int> unique = new HashSet<int>(numbers);
foreach (int num in unique) [Link](num);
}
}
56. 56. Union of two sets.
using System;
using [Link];
class Program
{
static void Main()
{
HashSet<int> a = new HashSet<int> { 1, 2, 3 };
HashSet<int> b = new HashSet<int> { 3, 4, 5 };
[Link](b);
foreach (int num in a) [Link](num);
}
}
57. 57. Intersection of two sets.
using System;
using [Link];
class Program
{
static void Main()
{
HashSet<int> a = new HashSet<int> { 1, 2, 3, 4 };
HashSet<int> b = new HashSet<int> { 3, 4, 5, 6 };
[Link](b);
foreach (int num in a) [Link](num);
}
}
58. 58. Difference of two sets.
using System;
using [Link];
class Program
{
static void Main()
{
HashSet<int> a = new HashSet<int> { 1, 2, 3, 4 };
HashSet<int> b = new HashSet<int> { 3, 4, 5, 6 };
[Link](b);
foreach (int num in a) [Link](num);
}
}
59. 59. Access value from Dictionary.
using System;
using [Link];
class Program
{
static void Main()
{
Dictionary<string, string> student = new Dictionary<string, string>();
[Link]("name", "Ali");
[Link](student["name"]);
}
}
60. 60. Loop through Dictionary.
using System;
using [Link];
class Program
{
static void Main()
{
Dictionary<string, int> student = new Dictionary<string, int>();
[Link]("age", 20);
[Link]("marks", 90);
foreach (var item in student) [Link]([Link] + " : " + [Link]);
}
}
Methods and Exception Handling
61. 61. Write a method to print Hello.
using System;
class Program
{
static void Greet()
{
[Link]("Hello");
}
static void Main()
{
Greet();
}
}
62. 62. Write a method to print a name.
using System;
class Program
{
static void ShowName(string name)
{
[Link](name);
}
static void Main()
{
ShowName("Ali");
}
}
63. 63. Write a method to add two numbers.
using System;
class Program
{
static int Add(int a, int b)
{
return a + b;
}
static void Main()
{
[Link](Add(5, 3));
}
}
64. 64. Write a method to subtract two numbers.
using System;
class Program
{
static int Subtract(int a, int b)
{
return a - b;
}
static void Main()
{
[Link](Subtract(10, 4));
}
}
65. 65. Method to find square of a number.
using System;
class Program
{
static int Square(int num)
{
return num * num;
}
static void Main()
{
[Link](Square(4));
}
}
66. 66. Method to find cube of a number.
using System;
class Program
{
static int Cube(int num)
{
return num * num * num;
}
static void Main()
{
[Link](Cube(3));
}
}
67. 67. Method to check even or odd.
using System;
class Program
{
static string CheckEvenOdd(int num)
{
if (num % 2 == 0) return "Even";
return "Odd";
}
static void Main()
{
[Link](CheckEvenOdd(7));
}
}
68. 68. Recursive factorial method.
using System;
class Program
{
static int Factorial(int n)
{
if (n == 0 || n == 1) return 1;
return n * Factorial(n - 1);
}
static void Main()
{
[Link](Factorial(5));
}
}
69. 69. Handle divide by zero using try-catch.
using System;
class Program
{
static void Main()
{
try
{
int a = 10, b = 0;
[Link](a / b);
}
catch (DivideByZeroException)
{
[Link]("Cannot divide by zero");
}
}
}
70. 70. Handle invalid number format.
using System;
class Program
{
static void Main()
{
try
{
int num = [Link]("abc");
[Link](num);
}
catch (FormatException)
{
[Link]("Invalid number");
}
}
}
OOP: Classes, Encapsulation, Inheritance
71. 71. Create class and object.
using System;
class Student
{
public string Name;
public int Age;
}
class Program
{
static void Main()
{
Student s = new Student();
[Link] = "Ali";
[Link] = 20;
[Link]([Link]);
[Link]([Link]);
}
}
72. 72. Class with method.
using System;
class Student
{
public string Name;
public void ShowName()
{
[Link](Name);
}
}
class Program
{
static void Main()
{
Student s = new Student();
[Link] = "Sara";
[Link]();
}
}
73. 73. Use constructor.
using System;
class Student
{
public string Name;
public int Age;
public Student(string name, int age)
{
Name = name;
Age = age;
}
}
class Program
{
static void Main()
{
Student s = new Student("Ali", 20);
[Link]([Link]);
[Link]([Link]);
}
}
74. 74. Encapsulation with getter and setter methods.
using System;
class Student
{
private int age;
public void SetAge(int a)
{
age = a;
}
public int GetAge()
{
return age;
}
}
class Program
{
static void Main()
{
Student s = new Student();
[Link](20);
[Link]([Link]());
}
}
75. 75. Encapsulation using property.
using System;
class Student
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
class Program
{
static void Main()
{
Student s = new Student();
[Link] = "Ali";
[Link]([Link]);
}
}
76. 76. Encapsulation with validation.
using System;
class Student
{
private int age;
public int Age
{
get { return age; }
set { if (value >= 0) age = value; }
}
}
class Program
{
static void Main()
{
Student s = new Student();
[Link] = 20;
[Link]([Link]);
}
}
77. 77. Simple inheritance.
using System;
class Animal
{
public void Eat()
{
[Link]("Animal is eating");
}
}
class Dog : Animal
{
public void Bark()
{
[Link]("Dog is barking");
}
}
class Program
{
static void Main()
{
Dog d = new Dog();
[Link]();
[Link]();
}
}
78. 78. Base class constructor inheritance.
using System;
class Person
{
public string Name;
public Person(string name)
{
Name = name;
}
}
class Student : Person
{
public Student(string name) : base(name) { }
}
class Program
{
static void Main()
{
Student s = new Student("Ali");
[Link]([Link]);
}
}
79. 79. Multiple objects of a class.
using System;
class Car
{
public string Brand;
}
class Program
{
static void Main()
{
Car c1 = new Car();
[Link] = "Toyota";
Car c2 = new Car();
[Link] = "Honda";
[Link]([Link]);
[Link]([Link]);
}
}
80. 80. Class with method and constructor.
using System;
class Book
{
public string Title;
public Book(string title)
{
Title = title;
}
public void ShowTitle()
{
[Link](Title);
}
}
class Program
{
static void Main()
{
Book b = new Book("C# Basics");
[Link]();
}
}
OOP: Polymorphism, Abstraction, Interfaces, SOLID
81. 81. Method overloading.
using System;
class MathOperations
{
public int Add(int a, int b)
{
return a + b;
}
public double Add(double a, double b)
{
return a + b;
}
}
class Program
{
static void Main()
{
MathOperations m = new MathOperations();
[Link]([Link](2, 3));
[Link]([Link](2.5, 3.5));
}
}
82. 82. Method overriding.
using System;
class Animal
{
public virtual void Sound()
{
[Link]("Animal sound");
}
}
class Dog : Animal
{
public override void Sound()
{
[Link]("Dog barks");
}
}
class Program
{
static void Main()
{
Animal a = new Dog();
[Link]();
}
}
83. 83. Abstract class example.
using System;
abstract class Animal
{
public abstract void Sound();
}
class Dog : Animal
{
public override void Sound()
{
[Link]("Dog barks");
}
}
class Program
{
static void Main()
{
Animal a = new Dog();
[Link]();
}
}
84. 84. Abstract class with normal method.
using System;
abstract class Shape
{
public abstract double Area();
public void Show()
{
[Link]("This is a shape");
}
}
class Circle : Shape
{
public double radius = 5;
public override double Area()
{
return 3.14 * radius * radius;
}
}
class Program
{
static void Main()
{
Circle c = new Circle();
[Link]();
[Link]([Link]());
}
}
85. 85. Simple interface example.
using System;
interface IAnimal
{
void Sound();
}
class Dog : IAnimal
{
public void Sound()
{
[Link]("Dog barks");
}
}
class Program
{
static void Main()
{
Dog d = new Dog();
[Link]();
}
}
86. 86. Multiple interfaces.
using System;
interface IPrint
{
void Print();
}
interface IScan
{
void Scan();
}
class Machine : IPrint, IScan
{
public void Print()
{
[Link]("Printing");
}
public void Scan()
{
[Link]("Scanning");
}
}
class Program
{
static void Main()
{
Machine m = new Machine();
[Link]();
[Link]();
}
}
87. 87. SRP example.
using System;
class Report
{
public string GetContent()
{
return "Report Content";
}
}
class ReportPrinter
{
public void Print(string content)
{
[Link](content);
}
}
88. 88. OCP example.
using System;
abstract class Discount
{
public abstract double GetDiscount(double amount);
}
class RegularDiscount : Discount
{
public override double GetDiscount(double amount)
{
return amount * 0.10;
}
}
class VIPDiscount : Discount
{
public override double GetDiscount(double amount)
{
return amount * 0.20;
}
}
89. 89. ISP example.
using System;
interface IPrint
{
void Print();
}
interface IScan
{
void Scan();
}
class Printer : IPrint
{
public void Print()
{
[Link]("Printing");
}
}
90. 90. DIP example.
using System;
interface IMessageService
{
void SendMessage(string message);
}
class EmailService : IMessageService
{
public void SendMessage(string message)
{
[Link]("Email: " + message);
}
}
class Notification
{
private IMessageService service;
public Notification(IMessageService service)
{
[Link] = service;
}
public void Notify(string message)
{
[Link](message);
}
}
More Mixed OOP and Exception Practice
91. 91. Create student class with private field and property.
using System;
class Student
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
class Program
{
static void Main()
{
Student s = new Student();
[Link] = "Ali";
[Link]([Link]);
}
}
92. 92. Inheritance with overridden method.
using System;
class Vehicle
{
public virtual void Start()
{
[Link]("Vehicle starts");
}
}
class Car : Vehicle
{
public override void Start()
{
[Link]("Car starts");
}
}
class Program
{
static void Main()
{
Vehicle v = new Car();
[Link]();
}
}
93. 93. Interface based notification.
using System;
interface IMessageService
{
void SendMessage(string message);
}
class SmsService : IMessageService
{
public void SendMessage(string message)
{
[Link]("SMS: " + message);
}
}
class Program
{
static void Main()
{
IMessageService service = new SmsService();
[Link]("Hello");
}
}
94. 94. Throw custom exception.
using System;
class Program
{
static void Main()
{
int age = -5;
try
{
if (age < 0)
throw new Exception("Age cannot be negative");
}
catch (Exception ex)
{
[Link]([Link]);
}
}
}
95. 95. Use finally block.
using System;
class Program
{
static void Main()
{
try
{
int num = [Link]("100");
[Link](num);
}
catch (FormatException)
{
[Link]("Invalid input");
}
finally
{
[Link]("Program ended");
}
}
}
96. 96. File not found handling.
using System;
using [Link];
class Program
{
static void Main()
{
try
{
string content = [Link]("[Link]");
[Link](content);
}
catch (FileNotFoundException)
{
[Link]("File not found");
}
}
}
97. 97. Reverse counting using do-while.
using System;
class Program
{
static void Main()
{
int i = 10;
do
{
[Link](i);
i--;
} while (i >= 1);
}
}
98. 98. Use break in loop.
using System;
class Program
{
static void Main()
{
for (int i = 1; i <= 10; i++)
{
if (i == 6) break;
[Link](i);
}
}
}
99. 99. Use continue in loop.
using System;
class Program
{
static void Main()
{
for (int i = 1; i <= 10; i++)
{
if (i == 5) continue;
[Link](i);
}
}
}
100. 100. Big mixed result program.
using System;
class Program
{
static void Main()
{
int[] marks = { 80, 75, 90, 85 };
int total = 0;
foreach (int mark in marks) total += mark;
double percentage = (double)total / [Link];
if (percentage >= 80) [Link]("Grade A");
else if (percentage >= 60) [Link]("Grade B");
else if (percentage >= 40) [Link]("Grade C");
else [Link]("Fail");
}
}
How to Practice These 100 Questions
• First read the question and identify the topic.
• Then try to write the answer yourself before looking at the code.
• After that, compare your answer with the provided solution.
• Practice the same question in short and long form.
• Repeat the most common question patterns daily.