C# Interview Questions on Data Types




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.



Is C# a strongly-typed language?

Yes





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

1. Built in data types.

2. User defined data types.



Give some examples for built in datatypes in C#?

1. int

2. float

3. bool



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.

---------------

C# Interview Questions on value types and reference types

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

1. Value Types

2. Reference Types



If you define a user defined data type by using the struct keyword, Is it a a value type or reference type?

Value Type



If you define a user defined data type by using the class keyword, Is it a a value type or reference type?

Reference type



Are Value types sealed?

Yes, Value types are sealed.



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

System.ValueType





Give examples for value types?

Enum

Struct



Give examples for reference types?

Class

Delegate

Array

Interface



What are the differences between value types and reference types?

1. Value types are stored on the stack where as reference types are stored on the managed heap.

2. Value type variables directly contain their values where as reference variables holds only a reference to the location of the object that is created on the managed heap.

3. There is no heap allocation or garbage collection overhead for value-type variables. As reference types are stored on the managed heap, they have the over head of object allocation and garbage collection.

4. Value Types cannot inherit from another class or struct. Value types can only inherit from interfaces. Reference types can inherit from another class or interface.

--------------------

C# Interview Questions on data type casting

What do you mean by casting a data type?

Converting a variable of one data type to another data type is called casting. This is also called as data type conversion.



What are the 2 kinds of data type conversions in C#?

Implicit conversions: No special syntax is required because the conversion is type safe and no data will be lost. Examples include conversions from smaller to larger integral types, and conversions from derived classes to base classes.



Explicit conversions: Explicit conversions require a cast operator. The source and destination variables are compatible, but there is a risk of data loss because the type of the destination variable is a smaller size than (or is a base class of) the source variable.



What is the difference between an implicit conversion and an explicit conversion?

1. Explicit conversions require a cast operator where as an implicit converstion is done automatically.

2. Explicit conversion can lead to data loss where as with implicit conversions there is no data loss.



What type of data type conversion happens when the compiler encounters the following code?

ChildClass CC = new ChildClass();

ParentClass PC = new ParentClass();



Implicit Conversion. For reference types, an implicit conversion always exists from a class to any one of its direct or indirect base classes or interfaces. No special syntax is necessary because a derived class always contains all the members of a base class.





Will the following code compile?

double d = 9999.11;

int i = d;



No, the above code will not compile. Double is a larger data type than integer. An implicit conversion is not done automatically bcos there is a data loss. Hence we have to use explicit conversion as shown below.



double d = 9999.11;

int i = (int)d; //Cast double to int.



If you want to convert a base type to a derived type, what type of conversion do you use?

Explicit conversion as shown below.

//Create a new derived type.

Car C1 = new Car();

// Implicit conversion to base type is safe.

Vehicle V = C1;



// Explicit conversion is required to cast back to derived type. The code below will compile but throw an exception at run time if the right-side object is not a Car object.

Car C2 = (Car) V;



What operators can be used to cast from one reference type to another without the risk of throwing an exception?

The is and as operators can be used to cast from one reference type to another without the risk of throwing an exception.



If casting fails what type of exception is thrown?

InvalidCastException

--------------------

C# Interview questions on Boxing and Unboxing

What is Boxing and Unboxing?

Boxing - Converting a value type to reference type is called boxing. An example is shown below.

int i = 101;

object obj = (object)i; // Boxing



Unboxing - Converting a reference type to a value typpe is called unboxing. An example is shown below.

obj = 101;

i = (int)obj; // Unboxing





Is boxing an implicit conversion?

Yes, boxing happens implicitly.



Is unboxing an implicit conversion?

No, unboxing is an explicit conversion.



What happens during the process of boxing?

Boxing is used to store value types in the garbage-collected heap. Boxing is an implicit conversion of a value type to the type object or to any interface type implemented by this value type. Boxing a value type allocates an object instance on the heap and copies the value into the new object. Due to this boxing and unboxing can have performance impact.

8 comments:

  1. "I very much enjoyed this article.Nice article thanks for given this information. i hope it useful to many pepole.php jobs in hyderabad.
    "

    ReplyDelete
  2. Awesome! Education is the extreme motivation that open the new doors of data and material. So we always need to study around the things and the new part of educations with that we are not mindful.
    python Training institute in Pune
    python Training institute in Chennai
    python Training institute in Bangalore

    ReplyDelete
  3. Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article. thank you for sharing such a great blog with us.
    rpa training in bangalore
    best rpa training in bangalore
    rpa training in pune | rpa course in bangalore
    rpa training in chennai

    ReplyDelete

  4. Its Very Use Full Information ,For More Information Search Core Java Online Training , Thanking You

    ReplyDelete