C# Interview Question and Answers with Example - Part 2


Below is top C# interview question and answers with examples for freshers and intermediate developers.



Views: 322

If casting fails what type of exception is thrown?

InvalidCastException

What are the 2 types of data types available in C#?

Value Types
Reference Types

Are Value types sealed?

Yes, Value types are sealed.

What is the base class from which all value types are derived?

System.ValueType

What are the 3 types of comments in C#?

1. Single Line Comments. You define single line comments with // as shown below.
// This is an example for single line comment
2. Multi line comments. You define multi line comments with /* */ as shown below.
/*
This is an example for
Multi Line comments
*/
3. XML Comments. You define XML comments with /// as shown below.
/// This is an example for defining XML comments.

How do you create user defined data types in C#?

You use the struct, class, interface, and enum constructs to create your own custom types. The .NET Framework class library itself is a collection of custom types provided by Microsoft that you can use in your own applications.

What do you mean by saying a "class is a reference type"?

A class is a reference type means when an object of the class is created, the variable to which the object is assigned holds only a reference to that memory. When the object reference is assigned to a new variable, the new variable refers to the original object. Changes made through one variable are reflected in the other variable because they both refer to the same data.

What do you mean by saying a "struct is a value type"?

A struct is a value type mean when a struct is created, the variable to which the struct is assigned holds the struct's actual data. When the struct is assigned to a new variable, it is copied. The new variable and the original variable therefore contain two separate copies of the same data. Changes made to one copy do not affect the other copy.

When do you generally use a class over a struct?

A class is used to model more complex behavior, or data that is intended to be modified after a class object is created. A struct is best suited for small data structures that contain primarily data that is not intended to be modified after the struct is created.

What is an array?

An array is a collection that contains data of the same type.

What are the 3 different types of arrays?

1. Single-Dimensional
2. Multidimensional
3. Jagged

What is Jagged Array?

If element of an array is also an array then it is called as jagged array.

Are arrays value types or reference types?

Arrays are reference types.

What is the base class for Array types?

System.Array

Can you use foreach iteration on arrays in C#?

Yes, we can use foreach iteration on arrays as it implementIEnumerable interface.

What are advantages of using array?

Array is strongly data type as it contains similar type of data.
Arrays do not perform boxing and unboxing.
Type mismatch can be detected as compile type time.

What are disadvantages of using array?

Arrays are fixed in size and cannot grow over time, where ArrayList in System.Collections namespace can grow dynamically.
Arrays are zero index based, and hence a little difficult to work with. The only way to store or retrieve elements from arrays, is to use integral index. Arrays do not provide convenient methods like Add(), Remove() ..etc provided by collection classes found in System.Collections or System.Collections.Generics namespaces, which are very easy to work with.

How to declare and initialize single-dimensional array?

Single-dimensional array can be declare and initialized as shown below.

// only declaration
int[] intArray; // to store integer value
string[] stringArray; // to store string
Employee[] classArray; // to store concrete class

// declaration, initialization and adding value seperately
int[] intArray;
intArray = new int[10];
intArray[0] = 1;
intArray[1] = 2;

// declaration, initialization together and adding values later on
int[] intArray = new int[5];
intArray[0] = 1;
intArray[1] = 2;

// declaration, initialization and adding values together
int[] intArray = new int[] { 10, 20, 12, 30, 40 };
int[] intArray = new int[5] { 10, 20, 12, 30, 40 };

// will throw error when size is not specfied
int[] intArray = new int[];

How to declare and initialize multi-dimensional array?

A multi-dimensional array can be declared and initialized as shown below. Here, [3,2] specifies that array can include 3 rows and 2 columns.

int[,] intArray = new int[3, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
// or 
int[,] intArray = { {1, 1}, {1, 2}, {1, 3} };

How to declare and initialize jagged array?

A jagged array is initialized with two square brackets [][]. The first bracket specifies the size of an array and the second bracket specifies the dimension of the array which is going to be stored as values.
The following jagged array stores a two single dimensional array as a value.

int[][] intJaggedArray = newint[2][];
intJaggedArray[0] = new int[3] { 1, 2, 3 };
intJaggedArray[1] = new int[2] { 4, 5 };

Console.WriteLine(intJaggedArray[0][0]); // 1
Console.WriteLine(intJaggedArray[0][2]); // 3
Console.WriteLine(intJaggedArray[1][1]); // 5

The following jagged array stores a multi-dimensional array as a value. Second bracket [,] indicates multi-dimension.

int[][,] intJaggedArray = newint[3][,];
intJaggedArray[0] = new int[3, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
intJaggedArray[1] = new int[2, 2] { { 3, 4 }, { 5, 6 } };
intJaggedArray[2] = new int[2, 2];

Console.WriteLine(intJaggedArray[0][1, 1]); // 4
Console.WriteLine(intJaggedArray[1][1, 0]); // 5
Console.WriteLine(intJaggedArray[1][1, 1]); // 6

What is collection in C#?

Collection in C# is just like array in C# but provides more functionality like adding, removing functionality in superior manner.

What are collection types?

There are 2 types of collection. Non-generic and Generic collection.

What is non-generic collection?

1) Non-generic collection is found under System.collection namespace.
2) Size can grow dynamically.
3) It can store any type of data [type unsafe] thus causing boxing and unboxing and increase performance overhead.

What is non-generic collection?

1) Generic collection is found under System.collection.generic namespace.
2) Size can grow dynamically.
3) It stores only same type of data [type safe] and thus reduce performance overhead.

Give examples of non-generic collection.

Examples are shown below.
1) ArrayList
2) HashTable
3) SortedList
4) Stack
5) Queue



Views: 322