C Interview Questions and Answers Common C Questions
C Interview Questions and Answers Common C Questions
Data types
What are the different data types in c#?
What are reference type and values types?
What is Nullable type?
Managed and Unmanaged code
What is managed and unmanaged code?
Value types
What is a value type?
What is Enum?
What is Struct ?
Reference types
What is a reference type?
Explain class?
Explain what is the use of delegate?
Features in different versions
Explain the new features added in different versions of C#?
Exception handling
Explain Exception handling in C#?
What is System.Exception?
Reflection
What are Attributes?
Garbage Collection
What is Garbage collection?
Serialization
What is Serialization?
What is the difference between Dispose and Finalize methods?
Encapsulation
Access modifiers?
Assembly
What is Assembly ?
What is ILDASM?
What is Assembly Version number?
What is strongly named assembly?
How do you sign an Assembly with a Strong Name?
The Assembly Manifest?
The Global Assembly Cache?
Assembly Info.cs?
Generics
What are Generics?
Type conversion
Casting in C#?
What are the “as” and “is” operators in C#?
Loops
What is the difference between “continue” and “break” statements in C#?
Difference between string and StringBuilder class in C#?
Differences between abstract class and interface
Differences between enum and struct
Difference between readonly and const
Differences between IEnumerable and IQueryable
Difference between Array and ArrayList
What is extension method?
Classes
What is a sealed class in c#?
What are partial classes?
Collections
How do you sort elements in an array?
What is IComparer interface?
LINQ
What is LINQ?
What is LINQ Data Provider?
What is LINQ to Objects?
What is LINQ to SQL?
What is DataContext?
What is LINQ to XML?
What is IQueryable<T> interface?
About
These are some of the important questions which are commonly asked in C#
interviews.These will also help to revise the basic concepts of the language.For other
information related to C# and .NET you can refer codecompiled.It has resources
related to .NET.
Data types
What are the different data types in c#?
Data type allows the programmer to use different types of data in a application.There
are different data types for different data used in a application.Data types defines the
range of values a variable of the data type can use and the different operations which
can be performed on the value.
Every variable in C# has a specific data type. This is because C# is a strongly typed
language.C# has many predefined data types.There are main two types in C# value
type and reference type.Every data type belongs to one of these.For example integer
is a value type.
If required we can create custom data types such as a class and structure.Some of the
predefined data types are:
Numeric
Integer Float
Data Type Description range Data description range
Type
byte Unsigned integer 8 bit Float Single 32-bit
floating-
point
values.
sbyte Signed integer 8 bit Double Double 64-bit
floating-
point values
int Signed integer 32 bit Char Unicode 16-bit
character.
uint Unsigned integer 32 bit Decimal Decimal 128-bit
monetary
values
short Signed integer 16 bit
Value types
What is a value type?
Types in C# can be broadly categorized into:
● reference types contains the value
● value types contains the reference of memory address instead of value
What is Enum?
Enum is a set of named constants.The first enumerator has the value 0 and every next
enumerator's value is incremented by 1.Enum can be of any integer type such as
int,float,byte.Enum (also called enumertion) is declared using the enum keyword.
For example you can declare an enum called Employee type as:
enum EmployeeType {
FullTime;
Contractor;
Intern;
};
In this enum the enumerator FullTime has the value 0.The values of Contractor and
Intern enumerators are 1 and 2 respectively.
What is Struct ?
The struct is a datatype like class.Struct is lightweight compared to class.It is used
when we don’t want to use reference type such as class because of performance
overhead.
We declare struct using the struct keyword as:
public struct Sample
{
public int a,b;
public Sample(int x, int y)
{
a = x;
b= y;
}
}
Reference types
What is a reference type?
Reference type variables contains the reference instead of actual data.So two different
reference variables can point to the same memory location.This could have other
effects such as the value of the reference variable being modified by a different
variable.
Some examples of reference types are:
● Class
● Interface
● Delegate
● Array
Explain class?
A class allows to create custom types by grouping together variables and
methods.Class is defined by using the class keyword.
Following is a sample class having some of the class members:
public class Sample
{
public string field1 = string.Empty;
//constructor
public Sample()
{
}
//method
public void Method1(int param1)
{
}
//variable
private int _prop1;
//property
public int Prop1
{
get { return _prop1;; }
set { _prop1;= value; }
}
}
Version 2.0 Version 3.0 Version 4.0 Version 5.0 version 6.0 Version
7.0
Generics Implicitly typed Dynamic Async Expression out
local variables binding (late features Bodied variables
binding) Methods
Lambda
expressions
Partial Methods
Exception handling
Explain Exception handling in C#?
Error is any undesirable behaviour in the program.Exceptions are runtime errors.To
handle the runtime errors or exceptions exception handling is used in C#.C# uses the
following keywords for Exception handling:
try defines a try block.Contains logic which could throw exception.
catch defines a catch block.Contains logic which handles the exception
thrown by the catch block
finally defines a finally block.This block always executes whether the
exception is thrown or not.You can define logic which you always want to
execute in this block.
throw keyword used to throw a new exception that is handled by
try..catch..finally blocks.
What is System.Exception?
When a runtime error occurs an exception is thrown. System.Exception is the base
class for all exceptions.Exception contains information about the error.Some of the
useful properties defined by the System.Exception class are:
StackTrace Information about the call stack.
InnerException Exception that caused the current exception.
Message Describes exception.
HelpLink Link to the information about exception.
HResult Numerical value for the exception.
Source Application that caused the exception.
TargetSite Method which threw the exception.
Data User-defined information about the exception.
Reflection
What are Attributes?
Attributes are used to attach metadata to different program elements such as class,
namespace, assembly.
There are predefined attributes provided by C# and custom attributes which developer
can define.
All attributes are derived from the class "System.Attribute" class.
To apply attribute to a program element you add the attribute in square brackets
before the element such as class.For example to add the Obsolete method to a method
you can use:
[Obsolete]
public static void OldMethod() {
}
The information provided by the attribute can be used at compile time and run time to
get more information about the program element. For example serialization attribute
specifies that the program element can be serialized.
Garbage Collection
What is Garbage collection?
When a new object is created it is allocated fixed memory from heap.When the
variable is no longer required then the memory which it occupied is reclaimed and
becomes available.This allocation and deallocation is the job of the garbage collector.
Serialization
What is Serialization?
Serialization is the process of converting object into a format which can be stored in
database or memory or transmitted across a network.This allows the object to be
recreated from the serialized state.
NET provides two types of serialization:
XML
Binary
Finalize:
Automatically called by the garbage collector after the object goes out of
scope.
Belongs to object class.
Encapsulation
Access modifiers?
Access modifiers are keywords for specifying the accessibility of a class or class
members.
public There is no restriction on accessibility.
protected Accessible to the class or the derived class.
internal Accessible to the assembly in which the type is defined.
private Accessible to the class in which the type is defined.
protected internal Accessible to the assembly or the derived class.
Assembly
What is Assembly ?
When a .NET application is compiled ,assembly is generated.Assembly consists of
IL(intermediate language) code.IL is converted to machine code at the execution
time.Apart from IL Assembly also contains Metadata.The metadata describes the
types contained in the assembly and also the assembly details such as the version of
the assembly.There are two main types of assemblies:
private stored inside a folder of the application which uses the assembly.So it
cannot be shared between multiple applications without copying.
shared (public) Stored in the GAC(Global Assembly Cache).Can shared
between different applications without copying to the applications. Strong
name needs to be created for the shared assembly.
What is ILDASM?
ILDASM stands for Intermediate Language Disassembler.It is used for viewing the
IL code in an assembly.For example to view the Intermediate Language Code in a
assembly called Sample.exe use the ildasm as:
ildasm Sample.exe
ildasm.exe is located in the BIN directory of the .NET SDK installation folder. So
you need to add this location to the path variable.
What is Assembly Version number?
Every assembly has a version number which is consists of four decimal pieces:
Major.Minor.Build.Revision
What is strongly named assembly?
A strongly named assembly is used to uniquely identify that assembly, and also
prevents assembly tampering.Strong assembly name consists of the following:
Assembly name,
Assembly version number
Culture information (optional)
Public key
Digital signature.
How do you sign an Assembly with a Strong Name?
To strongly sign an assembly you require a private key corresponding to the public
key.Public key is also distributed with the assembly.The Strong Name tool (Sn.exe)
is used for strongly signing assemblies.
The Assembly Manifest?
Assembly manifest is assembly metadata describing the assembly.It is stored in a PE
file(.dll or .exe) with MSIL code.
It consists of:
● Assembly Name
● Version Number
● Assembly Culture
● Strong name
● List of files in the assembly
}
}
Classes
What is a sealed class in c#?
Sealed class is used to prevent further inheritance .If you mark a class as sealed then
it cannot be further inherited.
What are partial classes?
In the case of a partial class the class definition is defined in multiple files.All the
parts defined in the different files are combined when the application is compiled.
What is this keyword?
Collections
How do you sort elements in an array?
You can easily sort elements in a array using the sort() function of the Array
class.Array is the base class of all arrays.So to sort elements in a array called arrItems
you will use:
Array.sort(arrItems)
The sort method of the Array class has overloads.One overload allows you to pass the
IComparer interface.Using an implementation of this interface you can sort the array
based on any property.
In the following example we are sorting Array called intArrayObj.
int[] intArrayObj = { 4, 7, 2, 0 };
var intComparer=new IntComparer();
Array.Sort(intArrayObj, intComparer);