Introduction

The DOT NET 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.



Accordion Content
  • 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.

Categorized in: