Dotnet Programming Interview Questions and Answers
Introduction
TheΒ DOTNETΒ is aΒ software framework. It is developed byΒ Microsoft. It includes a largeΒ libraryΒ and also providesΒ language inter-operability across someΒ programming languages. Language inter-operability refers the capability of two different languages to interact and operate on the same kind of data structures.
The programs written forΒ DOT NETΒ execute in a software environment. The name of the software environment is Common Language Runtime (CLR). It is the virtual machine component. The compiled code is converted into machine code at first. Then it is executed by computerβs CPU. The CLR provides additional services like exception handling, memory management, type safety, garbage collection, thread management etc.
The DOT NET FrameworkβsΒ Base Class LibraryΒ offersΒ user interface,Β database connectivity,Β data access, cryptography,Β web applicationΒ development, numericΒ algorithms, network communications etc. Programmers produce software by combining their ownΒ source codeΒ with theΒ DOT NET Framework and other libraries. The DOT NET Framework is projected to be used by most new applications created for the Windows platform. Microsoft also produces an integratedΒ largely for DOT NET software calledΒ Visual Studio.
“Explore a comprehensive collection of .NET programming interview questions and expertly crafted answers. Whether you’re a seasoned developer or preparing for a job interview, this resource covers essential topics in C# and .NET development. Dive into discussions on key concepts, design patterns, ASP.NET, and more. Elevate your knowledge and ace your next .NET programming interview with confidence.”
Dotnet Programming Interview Questions
- In .NET, you can use theΒ string.IndexOf()Β method to find the first occurrence of a specific string within another string. The method returns the index of the first occurrence of the specified string, or -1 if the specified string is not found.
- Here is an example of how to use theΒ IndexOf()Β method to find the first occurrence of the string entered by the user in the input string:
Console.WriteLine("Enter the input string:");
string inputString = Console.ReadLine();
Console.WriteLine("Enter the search string:");
string searchString = Console.ReadLine();
int index = inputString.IndexOf(searchString);
if (index != -1)
Console.WriteLine("The first occurrence of '" + searchString + "' is at index " + index);
else
Console.WriteLine("The string '" + searchString + "' was not found in the input string."); - You can also use theΒ LastIndexOf()Β method to find the last occurrence of a specific string within another string.
- Additionally, you can use theΒ string.Contains()Β method to check if a string contains a specific substring or not, it returns a boolean value.
Console.WriteLine("Enter the input string:");
string inputString = Console.ReadLine();
Console.WriteLine("Enter the search string:");
string searchString = Console.ReadLine();
bool check = inputString.Contains(searchString);
if (check)
Console.WriteLine("The input string contains '" + searchString + "'.");
else
Console.WriteLine("The input string does not contain '" + searchString + "'."); - Please note that in both examplesΒ Console.ReadLine()Β is used to get the input from the user.
Β
Here is one way to create a reversed pyramid pattern using C# in the .Net framework:
using System;
class Program
{
static void Main(string[] args)
{
int n = 5;
for (int i = n; i >= 1; i--)
{
for (int j = 1; j <= n - i; j++)
{
Console.Write(" ");
}
for (int j = 1; j <= 2 * i - 1; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
}
} - This program uses nested loops to create the pattern. The outer loop controls the number of rows in the pyramid, and the inner loops control the number of spaces and asterisks (*) in each row. The number of spaces before the asterisks is calculated based on the current row number.
This will Output :
*********
*******
*****
***
* - Note:Β Console.Write()Β is used to print the characters in the same line,Β Console.WriteLine()Β is used to print the characters in the new line.
- In object-oriented programming (OOP), encapsulation is one of the four fundamental principles, along with inheritance, polymorphism, and abstraction. Encapsulation refers to the practice of hiding the implementation details of an object and exposing only the necessary information to the outside world through interfaces and methods. This allows for greater flexibility and maintainability of the code, as the implementation details can be changed without affecting the code that uses the object.
Here is an example of how encapsulation is used in C#:
using System;
class BankAccount
{
private decimal balance;
private string accountNumber;
public BankAccount(string accountNumber)
{
this.accountNumber = accountNumber;
}
public void Deposit(decimal amount)
{
balance += amount;
}
public void Withdraw(decimal amount)
{
if (amount > balance)
{
throw new Exception("Insufficient funds");
}
balance -= amount;
}
public decimal GetBalance()
{
return balance;
}
}
class Program
{
static void Main(string[] args)
{
var account = new BankAccount("123456");
account.Deposit(100);
Console.WriteLine("Balance: " + account.GetBalance());
account.Withdraw(50);
Console.WriteLine("Balance: " + account.GetBalance());
}
} - In this example, the BankAccount class encapsulates the balance and account number properties, as well as the Deposit, Withdraw, and GetBalance methods. The balance and account number properties are marked as private, meaning that they cannot be accessed directly by code outside of the BankAccount class. Instead, code outside of the class can use the Deposit, Withdraw, and GetBalance methods to interact with the balance and account number.
This will Output :
Balance: 100
Balance: 50 Β
Here is a simple program to reverse a string in C#:
using System;
namespace ReverseString
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a string to reverse: ");
string input = Console.ReadLine();
char[] charArray = input.ToCharArray();
Array.Reverse(charArray);
string reversedString = new string(charArray);
Console.WriteLine("Reversed string: " + reversedString);
Console.ReadKey();
}
}
} - In this program, we first get a string input from the user using theΒ Console.ReadLine()Β method. Then, we convert the string into a character array using theΒ ToCharArray()Β method.
- Next, we use theΒ Array.ReverseΒ method to reverse the order of elements in the character array. Finally, we create a new string from the reversed character array using theΒ stringΒ constructor, and print it to the console.
This will Output :
Enter a string to reverse:
hello
Reversed string: olleh Β
Here is an example of how to check whether a number is even or odd in C#:
using System;
class Program
{
static void Main(string[] args)
{
int number = 5;
if (number % 2 == 0)
{
Console.WriteLine("The number " + number + " is even.");
}
else
{
Console.WriteLine("The number " + number + " is odd.");
}
}
} In this example, theΒ ifΒ statement checks whether the remainder of the division of theΒ numberΒ variable by 2 is equal to 0. If it is, the number is even, otherwise it is odd.
This will Output :
The number 5 is odd. Β
- To count the number of occurrences of a specific substring in a string in C#, you can use theΒ string.IndexOf()Β method to search for the substring in the main string and then iterate through the main string until no more occurrences of the substring are found.
Here’s an example of counting the number of occurrences of the substring “pp” in the string “apple”:
string input = "apple";
int count = 0;
int index = input.IndexOf("pp");
while (index != -1)
{
count++;
index = input.IndexOf("pp", index + 1);
}
Console.WriteLine("'pp' appears {0} times in '{1}'", count, input); - In this example, we use theΒ IndexOf()Β method to find the first occurrence of “pp” in the input string. If an occurrence is found, the value of the index variable is updated and the while loop continues. The loop continues to execute as long as the value of index is not -1, which means that an occurrence of the substring was found.
The output of the above code will be
'pp' appears 1 times in 'apple' If you want to print the ‘pp’ from the string “apple” then you can use the following code
string input = "apple";
int index = input.IndexOf("pp");
if (index != -1)
{
Console.WriteLine("'pp' found in '{0}' at index {1}", input, index);
}
else
{
Console.WriteLine("'pp' not found in '{0}'", input);
} This will Output :
'pp' found in 'apple' at index 1 Β
- In C#, to check for the occurrence of a string without using theΒ staticΒ keyword, you can create an instance of a class and use its method to check for the occurrence of a string.
Here is an example:
class StringChecker
{
public int OccurrenceCount(string originalString, string searchString)
{
int count = 0;
int index = 0;
while ((index = originalString.IndexOf(searchString, index)) != -1)
{
index++;
count++;
}
return count;
}
}
class Program
{
static void Main()
{
StringChecker sc = new StringChecker();
string originalString = "This is a test string";
string searchString = "is";
int count = sc.OccurrenceCount(originalString, searchString);
Console.WriteLine("The string '{0}' occurs {1} times in the original string.", searchString, count);
}
} - In this example, theΒ StringCheckerΒ class has a methodΒ OccurrenceCountΒ that takes two strings as input and returns the number of times the second string occurs in the first string. TheΒ MainΒ method creates an instance of theΒ StringCheckerΒ class, and calls theΒ OccurrenceCountΒ method to check for the occurrence of the string “is” in the original string “This is a test string“. The result is then printed to the console.
When you run this program, it will output:
The string 'is' occurs 2 times in the original string. Β
Here is an example of a star pattern program in C#:
using System;
namespace StarPattern
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter number of rows: ");
int rows = int.Parse(Console.ReadLine());
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
}
}
} - This program will prompt the user to enter the number of rows, and then it will print a triangle pattern of stars, with each row containing one more star than the previous row.
When you run this program, it will output:
Enter number of rows:
3
*
**
*** Β
Here’s an example of string concatenation in C#:
using System;
class Program
{
static void Main(string[] args)
{
string firstName = "John";
string lastName = "Doe";
string fullName = firstName + " " + lastName;
Console.WriteLine("Full name: " + fullName);
}
} - In this example, two strings,Β firstNameΒ andΒ lastName, are defined and concatenated using the + operator to create a full name string,Β fullName.Β The concatenated string is then displayed on the console usingΒ Console.WriteLine.
This will Output :
Full name: John Doe Β
Here’s an example of how to find the length of a string in C#:
using System;
class Program
{
static void Main(string[] args)
{
string message = "Hello World";
int length = message.Length;
Console.WriteLine("The length of the string '" + message + "' is: " + length);
}
} - In this example, a stringΒ messageΒ is defined and its length is found using theΒ LengthΒ property of theΒ stringΒ type. The length of the string is then displayed on the console usingΒ Console.WriteLine.
This will Output
The length of the string 'Hello World' is: 11 Β
Here’s an example of printing only even numbers in C#:
using System;
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 10; i++)
{
if (i % 2 == 0)
{
Console.WriteLine(i);
}
}
}
} - In this example, aΒ forΒ loop is used to iterate from 1 to 10. For each iteration, the value of i is checked to see if it is even using the modulo operator (%). If the result ofΒ i % 2Β is 0, then i is even and its value is printed on the console usingΒ Console.WriteLine.
This will Output :
2
4
6
8
10 Β
Here’s an example of using arrays in C#:
class Program
{
static void Main(string[] args)
{
int[] numbers = new int[5];
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
numbers[3] = 4;
numbers[4] = 5;
Console.WriteLine("The numbers in the array are:");
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine(numbers[i]);
}
}
} - In this example, an arrayΒ numbersΒ of typeΒ intΒ is declared and initialized with 5 elements. The elements of the array are assigned values one by one. Then, aΒ forΒ loop is used to iterate over the elements of the array and print their values on the console usingΒ Console.WriteLine.
This will Output :
The numbers in the array are:
1
2
3
4
5 Β
Here’s an example of printing the numbers from 1 to 10 in C#:
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Numbers from 1 to 10:");
for (int i = 1; i <= 10; i++)
{
Console.WriteLine(i);
}
}
} - In this example, aΒ forΒ loop is used to iterate from 1 to 10. For each iteration, the value ofΒ iΒ is printed on the console usingΒ Console.WriteLine.
This will Output :
Numbers from 1 to 10:
1
2
3
4
5
6
7
8
9
10 Β
Here’s an example of a program to check if a year is a leap year in C#:
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a year:");
int year = Convert.ToInt32(Console.ReadLine());
if (DateTime.IsLeapYear(year))
{
Console.WriteLine(year + " is a leap year.");
}
else
{
Console.WriteLine(year + " is not a leap year.");
}
}
} - In this example, the user is prompted to enter a year usingΒ Console.WriteLineΒ andΒ Console.ReadLine.Β The input year is then converted to an integer usingΒ Convert.ToInt32.Β TheΒ DateTime.IsLeapYearΒ method is used to check if the year is a leap year. If it is, a message indicating that it is a leap year is displayed on the console usingΒ Console.WriteLine.Β If it is not, a message indicating that it is not a leap year is displayed.
The output of the code will be:
Enter a year:
2020
2020 is a leap year. Β
Here’s a simple program in C# that checks whether a number is a prime number or not:
using System;
namespace PrimeNumber
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number: ");
int num = int.Parse(Console.ReadLine());
bool isPrime = true;
for (int i = 2; i <= Math.Sqrt(num); i++)
{
if (num % i == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
Console.WriteLine(num + " is a prime number.");
else
Console.WriteLine(num + " is not a prime number.");
}
}
} - The program first prompts the user to enter a number. It then uses a for loop to check if the number is divisible by any number other than 1 and itself. If the number is divisible by any other number, it means that it is not a prime number. Finally, the program displays a message indicating whether the number is a prime number or not.
This will Output :
Enter a number:
7
7 is a prime number. Tags:
.net interview questions.net interview questions and answersasp.net interview questionsasp.net interview questions and answersasp.net mvc interview questions and answersc# interview questionsc# interview questions and answersc# interview questions and answers for experiencedc# interview questions with answersinterview questions and answersmvc interview questions and answersoops interview questions and answersprogramming interview questions and answersVenkatesan Prabu
An accomplished tech entrepreneur and educator, widely recognized for over a decade of leadership in Microsoft technologies, prestigious MVP accolades, and a deep commitment to student empowerment through hands-on training, internships, and career-focused mentoring. I have published numerous blogs and technical articles, especially in areas like Artificial Intelligence, to bridge the gap between industry practices and academic learning.
