C# Interview Questions And Answers

TOP 100 C# Interview Questions And Answers

Frequently Asked C# Interview Questions And Answers

C# Interview Questions And Answers are as follows-

Q1) What does C# mean?

Answer: C# is a general object-oriented programming language that is safe and is used for generating Microsoft Intermediate Language by using compiled .Net framework.

Q2) Name the types of comments that are available in C#.

Answer: Following are the types of comments that are available in C#:

Single line

Multiple lines

XML comments

Q3) Can we execute multiple catch blocks?

Answer: No, we cannot execute multiple catch blocks. Once a proper catch code is executed, the control is transferred to the final block such that the code follows the final executed code.

Q4) What is the difference between the following terms: Public, and static?

Answer: Following is the table explaining the difference between public, static and void:

Public Static
The variables are accessible anywhere in the application. The variables are accessible globally without any creation of the class instance.

Q5) What is the void?

Answer: The void is a type of modifier which states that the variable or method does not return to any value.

Q6) What do you mean by an object?

Answer: An object is defined as an instance of a class that is used for accessing the methods of the class. The “New” keyword is used for the creation of an object. When an object is created in the memory, the memory will have information on methods, variables, and also the behavior of the class that is created.

Q7) What are constructors?

Answer: A constructor is defined as the member function in the class where the class has a similar name as that of the constructor. It is used for constructing the value for the data members while initiating the class.

Q8) What does “Jagged Arrays” mean?

Answer: Jagged arrays refer to the array that has an element of type array. The dimensions and the sizes of the elements are different. An array of arrays is the other name of the jagged array.

Q9) What is the difference between out and ref parameters?

Answer: The difference between out and ref parameters is that out is a parameter that doesn’t need the initialization of argument before passing to the method while the ref is a parameter that needs the initialization of argument before passing to the method.

Q10) What does “serialization” mean?

Answer: Serialization is defined as the process of converting an object into a stream of bytes whereas the reverse process of creating an object from the stream of bytes is known as de-serialization. An object is said to be serializable when it implements ISerialize Interface.

Q11) What is the difference between read-only and constant?

Answer: The difference between read-only and constant is that read-only is used during run time when the value has to be assigned. Constant variables are used during compilation time for declaration and initialization.

Q12) Can “this” be used in a static method?

Answer: No, “this” cannot be used in a static method as static methods are used for either static variables or static methods.

Q13) C# is a managed or unmanaged code?

Answer: C# is a managed code as the common language runtime can be used for compiling C# code to an intermediate language.

Q14) State the difference between direct cast and ctype.

Answer: The difference between direct cast and ctype is that direct cast is used for the conversion of type of an object that requires the run time which is similar to the specified type in the direct cast. Whereas ctype is used for converting the conversion which is defined for the expression and the type.

Q15) How can one use the singleton design pattern in C#?

Answer: The singleton design pattern is used in C# when the class has one instance and the access is provided globally.

Q16) What do C# attributes mean and what is their significance?

Answer: C# attributes are defined as the entities that are provided through declarative tags. Examples of C# attributes are class, method, etc and the information related to attribute can be retrieved during runtime using Reflection.

Q17) State the difference between the “throw” and “throw ex” in .Net.

Answer: The difference between “throw” and “throw ex” is that “throw” is used for preserving original error stack whereas “throw ex” has a throw point through which it can trace the stack. For more accurate, error-free information, the throw is advisable to use.

Q18) In C#.Net, what do indexers mean?

Answer: In C#, indexers are also known as smart arrays. They allow the instances of the class that are to be indexed in a similar as that of the array.

Q19) What does multicast delegate mean?

Answer: Multicast delegate means a delegate having multiple handlers. Every handler is assigned to a method.

Q20) State the difference between is and as operators in C#.

Answer: The difference between is and as an operator in C# is that is the operator is used for checking the compatibility of an object for a given type and returning to the Boolean for the results.Whereas as the operator is used for casting of an object to a type or to a class.

Q21) By using non-default values, how can one create an array?

Answer: By using the command Enumerable. Repeat. a new array can be created in non-default values.

Q22) How can one use nullable types in .Net?

Answer: Nullable types are defined as the types which can either take the normal value or the null value.

Q23) Differentiate between a Struct and a class.

Answer: The difference between a Struct and class is explained in the following table:

Struct Class
Inheritance is not supported Inheritance is supported
By default, the members are public By default, the members are private
Small isolated models can use struct Large complex objects can use class

Q24) What do you mean by object and class?

Answer: An object is defined as an instance of the class. An object is a block of memory that is stored in various forms like variables, array, and a collection. A class is defined as a data-structure that brings all the instances together in a single unit. It is an encapsulation of properties and methods that are used for representing a real-time entity.

Q25) What are the fundamental concepts of OOP?

Answer: OOP stands for Object-Oriented Programming and the following are the fundamental concepts:

Encapsulation: It is the internal representation of an object which is hidden from the outside view of an object’s definition. Only required information is accessed while the other data are hidden.

Abstraction: It is defined as the process of identifying critical behavior, eliminating the irrelevant details and data of an object.

Inheritance: It is defined as the process of creating new classes from another class. This can be achieved by accessing, modifying, and extending the behavior of the objects in their parent class.

Polymorphism: It is a term used for describing one name in many forms. It can be achieved through the same name but different implementations.

Q26) What do the terms managed and unmanaged code mean?

Answer: Managed code is defined as the code which is executed by CLR where CLR stands for Common Language Runtime. It is said to be managed because, in .Net frame, the unused memory is cleared by using a garbage collector. Unmanaged code is defined as the code which is executed by the application of runtime which does not include the frame of .Net. It is used for taking care of performance operations, memory, and security.

Q27) What is an interface?

Answer: An interface is defined as the class with no implementation. Following are the things that an interface:

Events

Properties

Declaration of method

Q28) Name the classes in C#.

Answer: Following are the different types of classes in C#:

Partial class: It is denoted by the keyword Partial and is used for allowing members that need to be divided and shared with multiple .cs files.

Sealed class: It is denoted by the keyword Sealed and is used for members of the sealed class. It cannot be inherited.

Abstract class: It is denoted by the keyword abstract and it is the only inherited class.

Static class: It is denoted by the keyword static and it defined as the class which does not allow inheritance.

Q29) What are the steps to be followed for code compilation in C#?

Answer: Following are the four steps that need to be followed for code compilation in C#:

By using the C# compiler, the source code is compiled into managed code.

The newly created codes are combined into assemblies.

CLR (Common Language Runtime) is loaded.

The execution of assembly is done by CLR.

Q30) What are the different types of serialization?

Answer: Following are the three different types of serialization:

XML serialization: All the public properties are serialized to the XML document.

SOAP: Any system that understands SOAP can use this serialization. System.Runtime.Serialization is the place where all the classes reside.

Binary serialization: It is used for the conversion of any code into binary form.

Q31) What does the XSD file mean?

Answer: The XSD stands for XML Schema Definition. It is used for giving a structure for the XML file. For the conversion of the files to XSD format, Xsd.exe tool is used.

Q32) What does thread pooling mean?

Answer: The collection of threads is known as thread pooling. These threads find application when the tasks are to be performed without disturbing the primary threads. To manage the operations of the threads in the pool, System.Threading.ThreadPool namespace is used.

Q33) What does race condition mean?

Answer: A race condition is defined as a situation in which two threads are accessing the same source and are trying to change at the same time. The prediction of the thread which could come first is difficult.

Q34) What does deadlock mean?

Answer: Deadlock is defined as the situation in which the process does not complete the execution as two or more processes are waiting for the completion of the process. This situation mainly arises during multi-threading.

Q35) Name the different states of a thread.

Answer: Following are the different states of s thread:

Unstarted: The thread is created

Running: The execution of the thread starts

WaitSleepJoin: The thread is asked to sleep, asks other thread to wait and asks the other thread to join

Suspended: The thread has been suspended

Aborted: The thread is dead but didn’t change the state

Stopped: The thread has been stopped

Q36) What are the properties of the thread class?

Answer: Following are the properties of the thread class:

IsAlive: When the thread is active, it contains the true value

Name: It is used for setting the name for a thread and also for returning the name of the thread

Priority: The value is returned to the task set as per operating system

IsBackground: It is the deciding factor for a thread whether it should be in the background or the foreground

ThreadState: This explains the state of the thread

Q37) Name a few methods used for handling the multi-threaded operations.

Answer: Following are the few methods that are used for handling the multi-threaded operations:

Start

Sleep

Abort

Suspend

Resume

Join

Q38) What does the term thread mean?

Answer: A thread is defined as a set of instructions that are executed and which enables the program for performing concurrent processing. C# has just one thread and that is by default.

Q39) What does a generic class mean?

Answer: Generic class is used for creating classes or objects that don’t have a specific data type. The data type is assigned during the runtime.

Q40) Name different types of delegates.

Answer: Following are the three types of delegates:

Single delegate: It is used for calling only a single delegate

Multicast delegate: This delegate is used for calling multiple methods. For subscribing and unsubscribing + and – operators are used

Generic delegate: For a delegate to be defined, there is no requirement of instance. The generic delegate is of three types:

  • Action
  • Funcs
  • Predicate

Q41) Name a few basic string operations.

Answer: Following are the basic string operations:

Concatenate: System.String.Concat is the command used

Modify: It is mainly used for replacing and trimming of the string

Compare: Two parameters are considered for comparison. Original string and the string to be compared are used. System.StringComparison() is the command used for comparison

Search: For searching a particular string, Startwith and Endswith methods are used

Q42) What are the properties of an array?

Answer: Following are the properties of an array:

Length: Used for getting the total number of elements in an array

IsFixedSize: Used for understanding whether the array has a fixed size or not

IsReadOnly: Used for understanding whether the array is read-only or not

Q43) What does C# I/O classes mean?

Answer: In C# the various operations related to files such as creating, deleting, opening, closing, etc are done with the help of a namespace called System.IO namespace.

Q44) Name some of the I/O classes.

Answer: Following are the I/O classes of C#:

File: It is used for manipulating the file

StreamWriter: The characters in a stream are wrote using StreamWriter

StreamReader: The characters in a stream are read using StreamReader

StringWriter: Writing of string buffer is done using StringWriter

StringReader: Reading of string buffer is done using StringReader

Path: Path related operations are conducted using path class

Q45) What are the advantages of C#?

Answer: Following are the advantages of C#:

It is easy to learn.

It is related to object-orientation.

It is also related to component orientation.

It is a part of .Net framework.

Q46) Give examples for types of comments in C#.

Answer: Following are the examples for types of comments in C#:

Single line comment

//

Multiline comments

/**/

XML comments

///

Q47) Name the IDEs that are provided by Microsoft for the development of C#.

Answer: Following are the IDEs that are provided by Microsoft for the development of C#:

VCE (Visual Studio Express)

VS (Visual Studio)

Visual Web Developer

Q48) What are the types of errors in C#?

Answer: Following are the two types of error in C#:

Compile-time error

Run time error

Q49) Name the different types of value types in C#.

Answer: Following are the different types of value in C#:

float

long

int

decimal

double

byte

enum

Q50) Name the different types of reference in C#.

Answer: Following are the different types of reference in C#:

object

class

interface

string

Q51) Define C# and list the features.

Ans- C# is an object-oriented, typed safe and manage language that is compiled by .NET framework and was developed by Microsoft back in 2000. The idea was that it will be used in the development of all kinds of software aiming at different platforms like Windows, Web, mobile using only one programming language. Fast forward to the present day it is one of the most popular programming languages worldwide, millions of developers use this language to build all kinds of software.

Some of the features of C#(sharp) are:

Use of Constructors and Destructors

Easy to grasp

General Purpose and Object-Oriented

Structured language

Platform independent for compilation

Part of .NET framework

 

Q52) List down the access modifiers available in C#.

Ans- Following are the access modifiers available for general use:

Public- When an attribute or method is defined as public it can be accessed from any part of code.

Private- A private attribute or method can be accessed from within the class itself.

Protected- When a user defines a method or attribute as protected then it can be accessed only within that class and the one inheriting the class.

Internal- When an attribute or method is defined as internal then it will be accessed from that class at the current assembly position.

Protected Internal- When you define an attribute or method as protected internal, then it’s access restricted to classes within the current project assembly or different types defined by that class.

 

Q52) List down the different IDE’s Provided by Microsoft for C# Development.

Ans- Below listed are few IDE’s for C# development:

Visual Studio Express (VCE)

Visual Studio (VS)

Visual Web Developer

MonoDevelop

browxy

Q53) What are the different approaches of passing parameters to a method?

Ans- There are three ways of passing parameters to a method:

Value Parameters-  Under this method the actual value of an argument is copied to the formal parameter of the function. In, this case the changes made into the formal parameter of the function have no effect on the actual value of the argument.

Reference Parameters- This method copies the argument referring to the memory location into the formal parameter. Meaning changes made to the parameter affect the argument.

Output Parameters- This method returns more than one value

 

Q54) Distinguish between finally and finalize blocks?

Ans- finally block is called after the execution of try and catch blocks, It is used for exception handling whether or not the exception has been caught this block of code gets executed. Generally, this block of code has a cleaner code.

The finalize method is called just before the garbage collection. Main priorities are to perform clean up operation for unmanaged code, it is automatically invoked when an instance is not subsequently called.

Q54) What is Managed or Unmanaged Code?

Ans- Managed code is one which is executed by CLR (Common Language Runtime) in simple terms it means all the application code is dependent on the .NET platform, considered as overseen in view of them. Unmanaged code is any code that is executed by runtime application of some other structure different from .NET platform. The runtime of application will deal with memory, security and other execution activities.

Q55) Define sealed classes in C#?

Ans- You create sealed classes in situations when you want to restrict the class to be inherited. For doing this sealed modifiers are used. If you forcefully specify a sealed class as a base class then a compilation error occurs.

Q56) Define a Partial class?

Ans- A partial class is the only one that essentially splits the definition of a class into multiple classes in either same source code files or multiple source code files. One can create a class definition in multiple files but that is compiled as one class at run-time and when an instance of this class is created, one can access all methods from every source file with the same object. It is indicated by the keyword ‘partial’.

Q57)  List down the fundamental OOP concepts?

Ans- There are four fundamental OOP (Object Oriented Programming) concept they are listed as follows:

Inheritance-  Ever heard of this dialogue from relatives “you look exactly like your father/mother” the reason behind this is called ‘inheritance’. From the Programming aspect, It generally means “inheriting or transfer of characteristics from parent to child class without any modification”. The new class is called the derived/child class and the one from which it is derived is called a parent/base class.

Polymorphism- You all must have used GPS for navigating the route, Isn’t it amazing how many different routes you come across for the same destination depending on the traffic, from a programming point of view this is called ‘polymorphism’. It is one such OOP methodology where one task can be performed in several different ways. To put it in simple words, it is a property of an object which allows it to take multiple forms.

Encapsulation- In a raw form, encapsulation basically means binding up of data in a single class.A class shouldn’t be directly accessed but be prefixed in an underscore.

Abstraction- Suppose you booked a movie ticket from bookmyshow using net banking or any other process. You don’t know the procedure of how the pin is generated or how the verification is done. This is called ‘abstraction’ from the programming aspect, it basically means you only show the implementation details of a particular process and hide the details from the user. It is used to simplify complex problems by modeling classes appropriate to the problem.An abstract class cannot be instantiated which simply means you cannot create objects for this type of class. It can only be used for inheriting the functionalities.

Q58) Explain StreamReader/StreamWriter class?

Ans- Streamreader and StreamWriter are classes of namespace.System.IO. Used when we want to read or write charact90, Reader based data,respectively.

members of StreamReader are: close()Read()Readline().
members of Streamwriter are: close()write()writeline().

Q59) List the difference between the Virtual method and the Abstract method?

Ans- Virtual method must always have a default implementation. However, it can be overridden in a derived class by the keyword override.

An Abstract method doesn’t have any implementation. It resides in the abstract class, and also it is mandatory that the derived class must implement abstract class. Use of override keyword is not necessary.

Q60) Illustrate Serialization?

Ans- A process that involves converting some code into its binary format is known as a serialization in C#. In doing so it gives the flexibility where code can be stored easily and or written to a disk or some other storage device. Serialization is used when there is a strict need for not losing the original code.

Serialization in C# is of three types:

Binary Serialization- It is fast and demands less space, converts any code into its binary format. Serialize and restore public and non-public properties.

SOAP- Generates a complete SOAP compliant envelope used by any system by its ability to understand SOAP. Classes under this type of Serialization reside in System.Runtime.Serialization. 

XML Serialization- It serializes all the public properties to XML document. Readability being one factor, an XML document can also be manipulated in various ways. Classes under this type reside in System.sml.Serialization.

Q61) Define Delegate?

Ans- Delegate is a variable that holds the reference to a method. It is a function pointer of the reference type. All Delegates are derived from the System. Delegate namespace. Both Delegate and the method that it refers to can have the same signature.

Q62) List down the differences between “dispose” and “finalize” methods in C#.

Ans- Dispose() is called when we want an object to release unmanaged resources with them. Whereas, Finalize() is used for the same purpose but it doesn’t assure garbage collection of an object.

Q63) Define C# I/O classes? List the commonly used classes?

Ans- C# consists of System.IO namespace which has classes that compute and perform various operations on files like creating, deleting, opening and closing etc.

Few commonly used I/O classes are listed below:

File- Helps in manipulating file.
StreamWriter- Generally used for writting characters to a stream.
StreamReader- Generally used for reading characters to a stream.
StringWriter- Used for writing a string buffer.
Stringreader- Used for reading a string buffer.

 

Q64)  Define thread? Explain about Multithreading?

Ans- Thread is a set of instructions that when executed enables the program to perform concurrent processing. Concurrent processing helps in doing more than one process at a time. By default, C# consists of only thread. Other threads can be created to execute the code in parallel with original thread.

Thread follows a life cycle where it starts whenever a thread is created and gets terminated immediately after the execution. The namespace it follows is System.Threading which has to be included to create threads and use its members.

Threads are created by extending the thread class. Start() method marks the beginning of the thread execution.

 

C# can also execute more than proceeses/task at a time which is done by handling different processes at different time labeled as “multithreading”. Several operations of the same are listed below:

Start

Sleep

Abort

Suspend

Resume 

Join

 

Q65) Explain Synchronous and Asynchronous Operations?

Ans- Synchronization is a way of creating a thread-safe code where only a single thread will access the code in a given time. A synchronous call waits for completion of method and then continous the program flow. Synchronous programming adversely affects the UI operations that normally happens when user tries to perform time-consuming operations since only one thread is used.

In Asynchronous operation, the method call immediately returns allowing the program to perform other operations while the method called completes its share of work in certain circumstances.

Q66) Explain Deadlock?

Ans- A deadlock is a situation that arises when a process isn’t able to complete it’s execution because two or more than two processes are waiting for each other to finish. This usually occurs in multi-threading. In this, a shared resource is being held up by a process and another process is waiting for the first process to get over or release it, and the thread holding the locked item is waiting for another process to complete.

Q67) Illustrate Race Condition?

Ans-  A Race Condition occurs in a situation when two threads access the same resource and try to change it at the same time. The thread which accesses the resource first cannot be predicted. Let me take a small example where two threads X1 and X2 are trying to access the same shared resource called T. And if both threads try to write the value to T, then the last value written to T will be saved.

Q68) What is JIT?

Ans. JIT stands for Just In Time. It is a compiler in CLR responsible for the execution of .NET programs of different languages by converting them into machine code. It speeds up the code execution and supports multiple platforms.

Q69) What are the different types of JIT Compilers?

Ans. There are 3 types of JIT Compilers:

  1. Pre-JIT compiler:It compiles all the source code into the machine code in a single compilation cycle, i.e. at the application deployment time.
  2. Normal JIT Compiler:The source code methods required at run-time are compiled into machine code and stored in the cache to be called later.

iii. Econo JIT Compiler: The methods required only at run-time are compiled using this compiler and they are not stored for future use.

Q70) What is BCL?

Ans. BCL stands for Base Class Library. It comprises classes, interface, and value types. It is the foundation for building .NET Framework applications, components, and controls.

Q71) What is FCL?

Ans. FCL stands for Framework Class Library, and is a collection of reusable types, including classes, interfaces, and data types included in the .NET Framework. It is used for developing a wide variety of applications, as it provides access to system functionality.

Q72) What is caching in .NET?

Ans. Caching functionality in .NET Framework allows data storage in memory for rapid access. It helps improve performance by making data available, even if the data source is temporarily unavailable and enhances scalability.

Q73) What are the types of caching in .NET?

Ans. There are 3 types of caches in .NET:

In-Memory Cache

Persistent in-process Cache

Distributed Cache

Q74) What are EXE and DLL?

Ans. EXE is an executable file that works as an application and it runs individually as it contains an entry point. DLL is a Dynamic Link Library which is a supportive file to other applications, and it cannot run individually.

Q75) What is a base class and derived class?

Ans. The base class is a class whose members and functions can be inherited, and the derived class is the class that inherits those members and may also have additional properties.

Q76)  Explain Different Types of Constructors in C#?

Ans. There are 5 types of constructors in C#, as given below:

Default Constructor- It is without any parameters.

Parameterized Constructor- It has one parameter.

Copy Constructor- It creates an object by copying variables from another object.

Static Constructor- It is created using a static keyword and will be invoked only once for all of the instances of the class.

Private Constructor- It is created with a private specifier and does not allow other classes to derive from this class or create an instance of it.

Q77) What is Shadowing?

Ans. Shadowing makes the method of the parent class available to the child class without using the override keyword. It is also known as Method Hiding.

Q78) What is Polymorphism? What are the types of Polymorphism?

Ans. Polymorphism refers to one interface with multiple functions. It means that the same method or property can perform different actions depending on the run-time type of the instance that invokes it.

There are two types of Polymorphism:

  1. Static or compile-time polymorphism
  2. Dynamic or runtime polymorphism

 

Q79) What is the Diamond of Death?

Ans. It is an ambiguity that arises due to multiple inheritances in C#. Two classes B and C inherit from A, and D inherits from both B and C but doesn’t override the method defined in A. The Diamond Problem arises when class B or C has overridden the method differently and D cannot decide to inherit from either B or C.

Q80) What is the Global Assembly Cache (GAC)?

Ans. The Global Assembly Cache is a machine-wide code cache that is stored in a folder in the Windows directory. It stores the .NET assemblies that are specifically designated to be shared by all applications executed on the system.

Related Posts:

For more Interview Questions And Answers click here

2 thoughts on “TOP 100 C# Interview Questions And Answers

  1. Pingback: TOP 150+ ASP.net Interview Questions And Answers - CloudNClear

  2. Pingback: [TOP 100] JSON Interview Questions And Answers - CloudNClear

Comments are closed.