• +91 9723535972
  • info@interviewmaterial.com

CSharp Interview Questions and Answers

CSharp Interview Questions and Answers

Question - 91 : - Explain different states of a Thread in C#?

Answer - 91 : -

A thread in C# can have any of the following states:

  • Aborted – The thread is dead but not stopped
  • Running – The thread is executing
  • Stopped – The thread has stopped execution
  • Suspended – The thread has been suspended
  • Unstarted – The thread is created but has not started execution yet
  • WaitSleepJoin – The thread calls sleep, calls wait on another object, and calls join on some other thread

Question - 92 : - Why do we use Async and Await in C#?

Answer - 92 : - Processes belonging to asynchronous programming run independently of the main or other processes. In C#, using Async and Await keywords for creating asynchronous methods.

Question - 93 : - What is an Indexer in C#, and how do you create one?

Answer - 93 : -

Also known as an indexed property, an indexer is a class property allowing accessing a member variable of some class using features of an array. Used for treating an object as an array, indexer allows using classes more intuitively. Although not an essential part of the object-oriented programming, indexers are a smart way of using arrays. As such, they are also called smart arrays. Defining an indexer enables creating classes that act like virtual arrays. Instances of such classes can be accessed using the [] array access operator. The general syntax for creating an indexer in C# is:

< modifier > <
return type > this[argument list] {
get {
// the get block code
}
set {
// the set block code
}
}

Question - 94 : - What is the Race condition in C#?

Answer - 94 : - When two threads access the same resource and try to change it at the same time, we have a race condition. It is almost impossible to predict which thread succeeds in accessing the resource first. When two threads try to write a value to the same resource, the last value written is saved.

Question - 95 : - What do you understand by Get and Set Accessor properties?

Answer - 95 : - Made using properties, Get and Set are called accessors in C#. A property enables reading and writing to the value of a private field. Accessors are used for accessing such private fields. While we use the Get property for returning the value of a property, use the Set property for setting the value.

Question - 96 : - Give a detailed explanation of the differences between ref and out keywords.

Answer - 96 : -

In any C# function, there can be three types of parameters, namely in, out and ref. Although both out and ref are treated differently at the run time, they receive the same treatment during the compile time. It is not possible to pass properties as an out or ref parameter. Following are the differences between ref and out keywords:

  • Initializing the Argument or Parameter – While it is not compulsory to initialize an argument or parameter before passing to an out parameter, the same needs to be initialized before passing it to the ref parameter.
  • Initializing the Value of the Parameter – Using ref doesn’t necessitate for assigning or initializing the value of a parameter before returning to the calling method. When using out, however, it is mandatory to use a called method for assigning or initializing a value of a parameter before returning to the calling method.
  • Usefulness – When the called method requires modifying the passed parameter, passing a parameter value by Ref is useful. Declaring a parameter to an out method is appropriate when multiple values are required to be returned from a function or method.
  • Initializing a Parameter Value in Calling Method – It is a compulsion to initialize a parameter value within the calling method while using out. However, the same is optional while using the ref parameter.
  • Data Passing – Using out allows for passing data only in a unidirectional way. However, data can be passed in a bidirectional manner when using ref.

Question - 97 : - What is Singleton Design Patterns in C#? Explain their implementation using an example.

Answer - 97 : -

Console.WriteLine("Multiplication : " + Calculate.Instance.Multiplication());
Console.WriteLine("Division : " + Calculate.Instance.Division());
Console.ReadLine();
}
}
public sealed class Calculate {
private Calculate() {}
private static Calculate instance = null;
public static Calculate Instance {
get {
if (instance == null) {
instance = new Calculate();
}
return instance;
}
}
public double ValueOne {
get;
set;
}
public double ValueTwo {
get;
set;
}
public double Addition() {
return ValueOne + ValueTwo;
}
public double Subtraction() {
return ValueOne - ValueTwo;
}
public double Multiplication() {
return ValueOne * ValueTwo;
}
public double Division() {
return ValueOne / ValueTwo;
}
}
}
A Singleton Design Pattern ensures that a class has one and only one instance and provides a global point of access to the same. There are numerous ways of implementing the Singleton Design Patterns in C#. Following are the typical characteristics of a Singleton Pattern:
 
  • A public static means of getting the reference to the single instance created
  • A single constructor, private and parameter-less
  • A static variable holding a reference to the single instance created
  • The class is sealed

Question - 98 : - Write a C# program to find if a positive integer is prime or not?

Answer - 98 : -

 static void Main(string[] args) 
    if (FindPrime(47)) 
    { 
        Console.WriteLine("Prime"); 
    } 
    else 
    { 
        Console.WriteLine("Not Prime"); 
    } 
    Console.ReadLine(); 
}   
internal static bool FindPrime(int number) 
    if (number == 1) return false; 
    if (number == 2) return true; 
    if (number % 2 == 0) return false; 
     var squareRoot = (int)Math.Floor(Math.Sqrt(number)); 
     for (int i = 3; i <= squareRoot; i += 2) 
    { 
        if (number % i == 0) return false; 
    } 
     return true; 
}

Question - 99 : - Write a C# program to find the substring from a given string.

Answer - 99 : -

internal static void findallsubstring(string str) 
   for (int i = 0; i < str.Length; ++i) 
   { 
       StringBuilder subString = new StringBuilder(str.Length - i); 
       for (int j = i; j < str.Length; ++j) 
       { 
           subString.Append(str[j]); 
           Console.Write(subString + " "); 
       } 
   } 
}

Question - 100 : - Write a program in C# Sharp to find if a given string is palindrome or not?

Answer - 100 : -

internal static void chkPalindrome(string str) 
   bool flag = false; 
   for (int i = 0, j = str.Length - 1; i < str.Length / 2; i++, j--) 
   { 
       if (str[i] != str[j]) 
       { 
           flag = false; 
           break; 
       } 
       else 
           flag = true; 
   } 
   if (flag) 
   { 
       Console.WriteLine("Palindrome"); 
   } 
   else 
       Console.WriteLine("Not Palindrome");
Output: 

Input: Key Output: Not Palindrome
Input: step on no pets Output: Palindrome 


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners