C# Interview Questions and Answers (958) - Page 21

Which of these string definitions will prevent escaping on backslashes in C#

NOTE: This is objective type question, Please click question title for correct answer.
If a method is marked as protected internal who can access it?

Classes within the same assembly, and classes derived from the declaring class.

Example:

public class BaseEx

{
protected internal int xyz=10;
}
public class Derived : BaseEx
{
public string RetrunData()
{
return xyz.ToString();
}
}
public class MainClass{
Derived obj = new Derived();
Console.WriteLine("data:{0}",obj.RetrunData());
}

How does assembly versioning in .NET prevent DLL Hell?

.NET allows assemblies to specify the name AND the version of any assemblies they need to run. Version is defined by major.minor.build.revision (Ex: 1.0.0.0)
What is the use of a static variable?

Static variable is used for class level variables.
Example: you have a website and you want to count online users.
Take a variable static in login page and increament it when successfully login. it will be updated for all users not specific.
What is private constructor? What is the use of it?

Private constrors are the special kind of constructor, which are allowed to not set create instace of a class from outside of the class. They are basically created when a class only have static members.

when you want to craete a single instance of class than use private constructor, basically they are used for Singlton pattern also.
Does interface have access modifier?

Interface has public modifier by default but you are not allowed to specify any access modifier in interface.

Example:

interface IExample{

public void funExample(); //Error: The modifier 'public' is not valid for this item.
string fun2(string strParam1, string strParam2);//no error
}

what is the use of virtual and override keywords?

When you override any function or any function is overridable then apply virtual keyword with that function in base class and in derived class add override keyword with that function.
Example:
Class parent{

public virtual void fun_overridable(){//some code here}
}

class child:parent{
public override void fun_overridable(){//some code here}
}

Can we make a class private in namespace?

No we can not create a class private in namespace. because namespace elements can not be private, protected or protected internal.

If you will create it, compiler will trow compile time error.
Example:

namespace TestNamespace{

private class testClass{
}
}

comiple time error: "Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal"
Can we inherit Multiple Interfaces into an Interface ?

NOTE: This is objective type question, Please click question title for correct answer.
can we create instance of an abstract class?

No, we can not create instance of an abstract class.

Example:

abstract class abstractExample{

public abstract void func1();
}

class derivedClass : abstractExample
{
public override void func1(){
//your code here.....
}
}


if you will try to create instance, it will give error.

abstractExample objAbstract=new abstractExample();//error in this line.

What is difference between readonly and const variable?

A const field can only be initialized only at declaration time of that field but readonly field can be initialized at the time of declaration or in constructor also. a const field will have only one constant value but readonly field can have different value depending on teh constructor.

Readonly is runtime ocnstant and cons field is compile time constant.

Example:

public class example{

public const int i = 20;

i=30;//compile time error.

public readonly int j=20;//initialized at declaration.

public readonly int l;

public example(){
l=30;//readonly field initialized in constructor also.
}

}

How can prevent a class to be inherited?

Using sealed keyword with class. you can use sealed class using object but can not inherit any class from a sealed class.

Example:

sealed class exampleSealed{

//code of a class.
}

class derived : exampleSealed{ //error can not inherit from exampleSealed
}

Can we create constructor for static class?

No, we can not create constructor for static class, as it its member are called by class name itself.

Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Class1
/// </summary>
public static class Class1
{
private Class1() //Error: Static classes cannot have instance constructors
{
//
// TODO: Add constructor logic here
//
}
}

even static class will have only static members.
What is constructor chaining?

Constructor Chaining is used to call a constructor in another constructor. Means you can initialize some fields using another constructor in one constructor.

Example:

Public class Example{
string strName;
string strCity;

public Example(string Name, string City)
{
this.strName = FirstName;
this.strCity = City;
}
//another constructor
public Example(string Name):this(Name,"Delhi")
{
this.strName= FName;
}
}
Struct can be inherited or not?

No, Struct can not be inherited because it is sealed implicitly.

Example:

public struct struct1

{
}

public class class1 : struct1 //Error : 'class1': cannot derive from sealed type 'struct1'
{
}

How You Can create a string reference?

NOTE: This is objective type question, Please click question title for correct answer.
What method is used to stop a running thread?

NOTE: This is objective type question, Please click question title for correct answer.
What type of object is required when starting a thread that requires a single parameter?

NOTE: This is objective type question, Please click question title for correct answer.
Which of the following attributes should you add to a class to enable it to be serialized?

NOTE: This is objective type question, Please click question title for correct answer.
Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories