Q) What is C#?

A) C# (pronounced as ‘C sharp’) is a programming language developed by Microsoft. C# is fully object oriented language.


Q) Explain few characteristics of C#(The interviewer usually starts interview by asking this question)

A) C# language is mainly used because it is easy to write, read and maintain. It has several characteristics like below:

Simple: It is quiet simple when compared to C and C++

Modern features: As it is latest programming language it is embedded with Automatic garbage collection, modern approach of debugging and great security.

Object Oriented: C# supports all OOPS concepts like Encapsulation, Inheritance and Polymorphism

Type Safety: Due to its type safety compile and run-time errors are very less.


Q) Write a simple program in C#.

A)The below class is a sample program to write in console application.

class SampleClass
{
public static void Main()
{
System.Console.WriteLine("C# language is very easy to learn");
}
}

Q) What are command line arguments?

A) Command line arguments are parameters which are supplied to Main method at the time of invoking it for execution. Below is the sample program which accepts command line arguments and prints them.

Class SampleArguments
{
public static voice Main(string[] args)
{
Console.Write("Below are the first two arguments");
Console.WriteLine(""+args[0]);
Console.WriteLine(""+args[1]);
}
}

Q) What are the keywords in C#?

A)Keywords implement special feature of languages. They are reserved and cannot be used. The following are few most commonly used keywords in C#.

bool, byte, float, string, double, short(These are keywords used for Data types)

break, case,if, else, continue, for, foreach(These are keywords used in conditional and enumarative statements)

private, public, static, protected(These are keywords used in access specifiers)


Q) What is the use of using statement in C#?

A)The using keyword is used to obtain a resource and use it and then automatically dispose it when the execution is completed.


Q) What is variable in C#?

A)A variable is nothing but a storage location which is used to store data value.
A variable in C# is always associated with datatypes.

Example:

int area;
string areaName;

Q) What is datatype? And what are its types?(This is an import question the interviewer may ask)

A)Datatypes specify the type of the data which is stored along with its size. There are basically two types of datatypes:

  1. Value types: These value types directly store the data in them.
  2. Reference types: The reference type store the address of data instead of directly storing data.

Note: If you want to give more detail you can add below examples for this question in interview


Q) Give few examples for value type and reference types.

A)Below are few examples of value type datatypes:
int: int is used to store integer values(numbers)
byte: byte is used to store very small integer value(max 255)
decimal: decimal is used to store integer values with precision(example : 2.154,etc)
bool: Boolean is used to store bit value(true or false)

Below are few examples of reference type datatypes:
Object: Used to store any value. It is a predefined reference type
string: string is used to store alphanumeric values.It is a predefined reference type
Note:(There are user defined data types which are of reference type like types created using classes, interfaces and delegates)


Q) What is operator? And what are type of operators in C#?

A)An operator is a symbol that is used to perform some mathematical or logical manipulations. C# supports many operators. Below are the examples:

Arithmetic Operators:(like +,-,*, etc)
Relational Operators:(<,>,<=,>=,etc)
Logical Operators:And(&&),OR(||),NOT(!)
Increment/Decrement Operators:+=,-+,etc
Conditional Operators:if else operators, Ternary Operator(Given below)
Bitwise Operators:&(bitwise logical AND),|(bitwise logical OR)


Q) What are Conversions and its types in C#?(This question is generally asked for experienced people)

A) There are situations when we want to convert data from one type to another(like from integer to string). These cases can be handled using conversions. There are two type of conversions:

  1. Implicit Conversions: Implicit conversions are conversions which are always successful and done without losing any data.(Example converting a byte to int i.e., from small value to large value)
  2. Explicit Conversions: Explicit conversions are conversions in which we have to force the compiler to perform operation using cast as these conversions may loose data while converting(Example: when we convert decimal to integer, we will loose the decimal precision values)

Q) What is casting or typecasting?

A)When we do explicit conversion, we have to force compiler to perform such conversion by using an expression. These expressions are nothing but casting from one datatype to other.

Example: In below scenario, we are casting a decimal value to integer.

decimal d=5.202;
int a=(int)d;

In above case a value is 5. Here d is which is decimal is casted to integer.


Q)What are the looping statements in C#?

A)The below are the four looping statements in C#:
while statement
do while statement
for statement
foreach statement


Q) What is the difference between Finalize() and Dispose() methods in C#?

A)Dispose() is called when we want for an object to release any unmanaged resources with them. On the other hand Finalize() is used for the same purpose but it doesn’t assure the garbage collection of an object.


Q) What is the difference between while and do while?

A)The only difference between while and do while is do while first executes the set of code before checking condition whereas while first checks the condition and executes.(Indicating do while code is executed at least once irrespective of condition)


Q) What is the difference between Array and Arraylist?(Here interviewer wants to make sure you are not confused with Array and Arraylist)

A)In an array, we can have items of the same type only but in Arraylist we can have items with different data type.

The size of Array is fixed whereas an Arraylist doesn’t have a fixed size.


Q) What is the difference between for and foreach?

A)for loop is achieved with the help of initializing an index and incrementing or decrementing that index value until a particular condition is satisfied, whereas foreach holds the set of data and loops for each of its element.


Q) What are continue and break statements mean in C#?

A)The continue keyword is used to skip the current executing iteration and move to next iteration, where as break keyword is used to completely exit from that loop


Q) What are output parameters?

A)As the name indicates output parameters are used to pass results back to calling method. This is achieved by declaring the parameters with out keyword.


Q) What’s the difference between the System.Array.CopyTo() and System.Array.Clone() in C#?

A)Both the methods perform a shallow copy.

Using Clone() method we creates a new array object containing all the elements in the original array, whereas using CopyTo() method all the elements of existing array copies into another existing array


Q) What is difference between constants and read-only in C#?

A)Constant variables are declared and initialized at compile time. The value can’t be changed afterwards. Whereas Read only is used only when we want to assign the value at run time


Q) What are sealed classes in C#?

A)Sealed classes are created when we want to restrict the class to be inherited. Sealed modifier used to prevent derivation from a class


Q) What are reference types in C#?

A)Below are the list of reference types in C# :

Class
String
Interface
Object


Q) List out two different types of errors in C#?

A)Below are the types of errors in C# –

Compile Time Error
Run Time Error


Q) What is the difference between ref & out parameters in C#?(This question is one of interviewers favorite)

A)ref must be initialized before passing to the method whereas out parameter needs not to be initialized before passing to a method

out parameter must and should be assigned with a value in method whereas for ref it is not mandatory


Q) What are Jagged Arrays in C#?

A)An array of arrays is called a Jagged Array. The elements can be of different sizes and dimensions.

C Sharp(C#) Interview Questions Part II