November 21, 2019

Srikaanth

AppDynamics C# Interview Questions Answers

What Is A Virtual Property. Give An Example?

A property that is marked with virtual keyword is considered virtual property. Virtual properties enable derived classes to override the property behavior by using the override keyword. In the example below FullName is virtual property in the Customer class. BankCustomer class inherits from Customer class and overrides the FullName virtual property. In the output you can see the over riden implementation. A property overriding a virtual property can also be sealed, specifying that for derived classes it is no longer virtual.

using System;
class Customer
{
private string _firstName = string.Empty;
private string _lastName = string.Empty;
public string FirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
}
}
public string LastName
{
get
{
return _lastName;
}
set
{
_lastName = value;
}
}
// FullName is virtual
public virtual string FullName
{
get
{ return _lastName + ", " + _firstName;
}
}
}
class BankCustomer : Customer
{
// Overiding the FullName virtual property derived from customer class
public override string FullName
{
get
{
return "Mr. " + FirstName + " " + LastName;
}
}
}
class MainClass
{
public static void Main()
{
BankCustomer BankCustomerObject = new BankCustomer();
BankCustomerObject.FirstName = "David";
BankCustomerObject.LastName = "Boon";
Console.WriteLine("Customer Full Name is : " + BankCustomerObject.FullName);
}
}

What Is An Abstract Property. Give An Example?

A property that is marked with abstract keyword is considered abstract property. An abstract property should not have any implementation in the class. The derived classes must write their own implementation. In the example below FullName property is abstract in the Customer class. BankCustomer class overrides the inherited abstract FullName property with its own implementation.

using System;
abstract class Customer
{
private string _firstName = string.Empty;
private string _lastName = string.Empty;
public string FirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
}
}
public string LastName
{
get
{
return _lastName;
}
set
{
_lastName = value;
}
}
// FullName is abstract
public abstract string FullName
{
get;
}
}
class BankCustomer : Customer
{
// Overiding the FullName abstract property derived from customer class
public override string FullName
{
get
{
return "Mr. " + FirstName + " " + LastName;
}
}
}
class MainClass
{
public static void Main()
{
BankCustomer BankCustomerObject = new BankCustomer();
BankCustomerObject.FirstName = "David";
BankCustomerObject.LastName = "Boon";
Console.WriteLine("Customer Full Name is : " + BankCustomerObject.FullName);
}
}

Can You Use Virtual, Override Or Abstract Keywords On An Accessor Of A Static Property?

No, it is a compile time error to use a virtual, abstract or override keywords on an accessor of a static property.

What Are Constants In C#?

Constants in C# are immutable values which are known at compile time and do not change for the life of the program. Constants are declared using the const keyword. Constants must be initialized as they are declared. You cannot assign a value to a constant after it isdeclared. An example is shown below.

using System;
class Circle
{
public const double PI = 3.14;
public Circle()
{
//Error : You can only assign a value to a constant field at the time of declaration
//PI = 3.15;
}
}
class MainClass
{
public static void Main()
{
Console.WriteLine(Circle.PI);
}
}

Can You Declare A Class Or A Struct As Constant?

No, User-defined types including classes, structs, and arrays, cannot be const. Only the C# built-in types excluding System.Object may be declared as const. Use the readonly modifier to create a class, struct, or array that is initialized one time at runtime (for example in a constructor) and thereafter cannot be changed.

Does C# Support Const Methods, Properties, Or Events?

No, C# does not support const methods, properties, or events.
AppDynamics Most Frequently Asked Latest C# Interview Questions Answers
AppDynamics Most Frequently Asked Latest C# Interview Questions Answers

Can You Change The Value Of A Constant Filed After Its Declaration?

No, you cannot change the value of a constant filed after its declaration. In the example below, the constant field PI is always 3.14, and it cannot be changed even by the class itself. In fact, when the compiler encounters a constant identifier in C# source code (for example, PI), it substitutes the literal value directly into the intermediate language (IL) code that it produces. Because there is no variable address associated with a constant at run time, const fields cannot be passed by reference.

using System;
class Circle
{
public const double PI = 3.14;
}

How Do You Access A Constant Field Declared In A Class?

Constants are accessed as if they were static fields because the value of the constant is the same for all instances of the type. You do not use the static keyword to declare them. Expressions that are not in the class that defines the constant must use the class name, a period, and the name of the constant to access the constant. In the example below constant field PI can be accessed in the Main method using the class name and not the instance of the class. Trying to access a constant field using a class instance will generate a compile time error.

using System;
class Circle
{
public const double PI = 3.14;
}
class MainClass
{
public static void Main()
{
Console.WriteLine(Circle.PI);
Circle C = new Circle();
// Error : PI cannot be accessed using an instance
// Console.WriteLine(C.PI);
}
}

What Are The 2 Broad Classifications Of Fields In C#?

1. Instance fields
2. Static fields

What Are Instance Fields In C#?

Instance fields are specific to an instance of a type. If you have a class T, with an instance field F, you can create two objects of type T, and modify the value of F in each object without affecting the value in the other object.

Can Destructors Have Access Modifiers?

No, destructors cannot have access modifiers.

What Does Protected Internal Access Modifier Mean?

The protected internal access means protected OR internal, not protected AND internal. In simple terms, a protected internal member is accessible from any class in the same assembly, including derived classes. To limit accessibility to only derived classes in the same assembly, declare the class itself internal, and declare its members as protected.

What Is The Default Access Modifier For A Class,struct And An Interface Declared Directly With A Namespace?

internal.

Can You Specify An Access Modifier For An Enumeration?

Enumeration members are always public, and no access modifiers can be specified.

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.

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 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 ""Vankat.""";
//Output: My Name is "Vankat."

Subscribe to get more Posts :