CSharp Interview Questions and Answers
Question - 71 : - What are the different collection classes in C#?
Answer - 71 : -
Collection classes are classes that are mainly used for data storage and retrieval. These collection classes will serve many purposes like allocating dynamic memory during run time and you can even access the items of the collection using the index value that makes the search easier and faster. These collection classes belong to the object class.
There are many collection classes which are as follows:
Array list: it refers to the ordered collection of the objects that are indexed individually. You can use it as an alternative to the array. Using index you can easily add or remove the items off the list and it will resize itself automatically. It works well for dynamic memory allocation, adding or searching items in the list.
Hash table: if you want to access the item of the hash table then you can use the key-value to refer to the original assigned value to the variable. Each item in the hash table is stored as a key/value pair and the item is referenced with its key value.
Stack: it works on the concept of last-in and first-out collection of the objects. Whenever you add an item to the list it is called pushing and when you remove the item off the list it is called popping.
Sorted list: this collection class uses the combination of key and the index to access the item in a list.
Queue: this collection works on the concept of first-in and first-out collection of the object. Adding an item to the list is call enqueue and removing the item off the list is call deque.
BitArray: this collection class is used to represent the array in binary form (0 and 1). You can use this collection class when you do not know the number and the items can be accessed by using integer indexes that start from zero.
Question - 72 : - Define interface class in C#? Explain with an example.
Answer - 72 : -
An interface class is completely an abstract class that contains abstract methods and properties. By default, the members of the interface class are abstract and public with no fields defined. If you want to access the interface methods then the interface must be implemented by another class using ‘:’ symbol. If you want to define the body of the methods that can only be implemented in the implementing class.
For example:
// Interface
Interface IAnimal {
void Sound(); // interface method (without body)
}
class Pig : IAnimal // Pig class "implements" the IAnimal interface
{
public void Sound()
{
Console.WriteLine("The pig says: wee wee"); // The body of Sound() is provided her
}
}
class Program
{
static void Main(string[] args)
{
Pig myPig = new Pig(); // Create a Pig object
myPig.animalSound();
}}
Question - 73 : - Explain the concept of thread in C#.
Answer - 73 : -
A thread can be defined as the execution flow of any program and defines a unique flow of control. You can manage these threads' execution time so that their execution does not overlap the execution of other threads and prevent deadlock or to maintain efficient usage of resources. Threads are lightweight programs that save the CPU consumption and increase the efficiency of the application. The thread cycle starts with the creation of the object of system.threading.thread class and ends when the thread terminates.
System.threading.thread class allows you to handle multiple threads and the first thread always runs in a process called the main thread. Whenever you run a program in C#, the main thread runs automatically.
Question - 74 : - Define structure in C# with example.
Answer - 74 : - A structure is a data type of a value type. A struct keyword is used when you are going to define a structure. A structure represents a record and this record can have many attributes that define the structure. You can define a constructor but not destructor for the structure. You can implement one or more interfaces within the structure. You can specify a structure but not as abstract, virtual, or protected. If you do not use the new operator the fields of the structure remain unassigned and you cannot use the object till you initialize the fields.
Question - 75 : - What do you mean by user control and custom control in C#?
Answer - 75 : -
User controls are very easy to create and are very much the same as the ASP control files. You cannot place a user control on the toolbox and cannot even drag-drop it. They have unique design and individual code behind these controls. Ascx is the file extension for user control.
You can create custom code as the compiled code and can be added to the toolbox. You can include these controls to the web forms easily. Custom controls can be added to multiple applications efficiently. If you want to add a private custom control then you can copy it to dll and then to the bin directory of your web application and use its reference there.
Question - 76 : - C# program to remove an element from the queue.
Answer - 76 : -
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Application
{
class DemoProgram
{
static void Main(string[] args)
{
Queue qs = new Queue();
qs.Enqueue(1);
qs.Enqueue(2);
qs.Enqueue(3);
foreach (Object ob in qs)
{
Console.WriteLine(ob);
}
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("Total number of elements in the Queue " + qs.Count);
Console.WriteLine("Does the Queue contain " + qs.Contains(3));
Console.ReadKey();
}
}
}
Question - 77 : - How to find if a number is a palindrome or not in C#.
Answer - 77 : -
using System;
public class PalindromeNum
{
public static void Main(string[] args)
{
int n,r,num=0,Dem;
Console.Write("Enter the Number: ");
n = int.Parse(Console.ReadLine());
dem=n;
while(n>0)
{
r=n%10;
num=(num*10)+r;
n=n/10;
}
if(dem==num)
Console.Write("Number is Palindrome.");
else
Console.Write("Number is not Palindrome");
}
}
Question - 78 : - How will you differentiate between a Class and a Struct?
Answer - 78 : -
Although both class and structure are user-defined data types, they are different in several fundamental ways. A class is a reference type and stores on the heap. Struct, on the other hand, is a value type and is, therefore, stored on the stack. While the structure doesn’t support inheritance and polymorphism, the class provides support for both. A class can be of an abstract type, but a structure can’t. All members of a class are private by default, while members of a struct are public by default. Another distinction between class and struct is based on memory management. The former supports garbage collection while the latter doesn’t.
Question - 79 : - Compare Virtual methods and Abstract methods.
Answer - 79 : - Any Virtual method must have a default implementation, and it can be overridden in the derived class using the override keyword. On the contrary, an Abstract method doesn’t have an implementation, and it resides in the abstract class. The derived class must implement the abstract method. Though not necessary, we can use an override keyword here.
Question - 80 : - What are Namespaces in C#?
Answer - 80 : - Use of namespaces is for organizing large code projects. The most widely used namespace in C# is System. Namespaces are created using the namespace keyword. It is possible to use one namespace in another, known as Nested Namespaces.