Wednesday, June 29, 2011

C# Interview Questions & Answer 1

What is an array?
An array is a data structure that contains several variables of the same type.

What are the 3 different types of arrays?
1.
Single-Dimensional
2. Multidimensional
3. Jagged

What is Jagged Array?
A jagged array is an array of arrays.

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,Since array type implements IEnumerable, you can use foreach iteration on all arrays in C#.


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.
List the 5 different access modifiers in C#?
1.
public
2. protected
3. internal
4. protected internal
5. private
If you donot specify an access modifier for a method, what is the default access modifier?private

Classes and structs support inheritance. Is this statement true or false?
False, Only classes support inheritance. structs donot support inheritance.

If a class derives from another class, will the derived class automatically contain all the public, protected, and internal members of the base class?
Yes, the derived class will automatically contain all the public, protected, and internal members of the base class except its constructors and destructors.

Can you create an instance for an abstract class?
No, you cannot create an instance for an abstract class.
How do you prevent a class from being inherited by another class?Use the sealed keyword to prevent a class from being inherited by another class.

Classes and structs can be declared as static, Is this statement true or false?
False, only classes can be declared as static and not structs.
Can you create an instance of a static class?No, you cannot create an instance of a static class.

Can a static class contain non static members?
No, a static class can contain only static members.

What is the difference between string keyword and System.String class?
string keyword is an alias for Syste.String class. Therefore, System.String and string keyword are the same, and you can use whichever naming convention you prefer. The String class provides many methods for safely creating, manipulating, and comparing strings.

Are string objects mutable or immutable?
String objects are immutable.
What do you mean by String objects are immutable?String objects are immutable means, they cannot be changed after they have been created. All of the String methods and C# operators that appear to modify a string actually return the results in a new string object. In the following example, when the contents of s1 and s2 are concatenated to form a single string, the two original strings are unmodified. The += operator creates a new string that contains the combined contents. That new object is assigned to the variable s1, and the original object that was assigned to s1 is released for garbage collection because no other variable holds a reference to it.

string s1 = "First String ";
string s2 = "Second String";

// Concatenate s1 and s2. This actually creates a new
// string object and stores it in s1, releasing the
// reference to the original object.
s1 += s2;

System.Console.WriteLine(s1);
// Output: First String Second String

What will be the output of the following code?
string str1 = "Hello ";
string str2 = s1;
str1 = str1 + "C#";
System.Console.WriteLine(s2);
The output of the above code is "Hello" and not "Hello C#". This is bcos, if you create a reference to a string, and then "modify" the original string, the reference will continue to point to the original object instead of the new object that was created when the string was modified.

What is a verbatim string literal and why do we use it?
The "@" symbol is the verbatim string literal. Use verbatim strings for convenience and better readability when the string text contains backslash characters, for example in file paths. Because verbatim strings preserve new line characters as part of the string text, they can be used to initialize multiline strings. Use double quotation marks to embed a quotation mark inside a verbatim string. The following example shows some common uses for verbatim strings:
string ImagePath = @"C:\Images\Buttons\SaveButton.jpg";//Output: C:\Images\Buttons\SaveButton.jpg

string MultiLineText = @"This is multiline
Text written to be in
three lines.";
/* Output:
This is multiline
Text written to be in
three lines.
*/

string DoubleQuotesString = @"My Name is ""myname.""";
//Output: My Name is "myname."

No comments:

Post a Comment