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

In C#, which of the following keyword is a 16-bit unsigned Integer?

NOTE: This is objective type question, Please click question title for correct answer.
What will happen if we compile below C# code? Whether it will compile or not.If compile then what will be the output? 1). if (string.IsNullOrEmpty(false)) { //code } 2). if (string.IsNullOrEmpty(1)) { //code }

It will not give us any output because it will throw compile-time error as:-
The best overloaded method match for 'string.IsNullOrEmpty(string)' has some invalid arguments
Argument 1: cannot convert from 'bool' to 'string'
Argument 2: cannot convert from 'int' to 'string'

Which method is used to sort the elements of an array in descending order.?

By using Sort() method we can sort the elements and the use Reverse() method for descending order..
What are custom Exceptions?

In some scenarios the user need to handle some kind of errors. In that cases the user will handle by user defined exceptions. These exceptions are called as custom exceptions.
What is the default data type for enum members?

NOTE: This is objective type question, Please click question title for correct answer.
What is the difference between System.Array.CopyTo() and System.Array.Clone()?

Both the method are used for copy the data. But the difference is System.Array.CopyTo() method performs deep copy and System.Array.Clone() method performs shallow copy.
Here deep copy means it will create new instance of each element object where as shallow copy contains reference of same object.
What is an abstract modifier in C# programming language?

abstract modifier is used for either class or a method.

abstract class is a class which has no complete implementations. All the implementations are done by the derived classes who inherits the properties of that class.

abstract methods are only limited to abstract class and we cannot declare them outside. It is an empty method which has no body, no curly braces ({}) etc.
abstract class MyClass              // This is an abstract class.

{
public abstract int Function(); // This is an abstract method.
.........;
........;
}

What is a sealed modifier in C# programming language?

sealed modifiers are used to seal a class or a particular method to prevent inheriting them by the derived classes.

For class,
sealed class One // This is a sealed class

{
.......;
......;
}

This cannot be inherited by the deived classes,
class Two : One // This will give a compile time error

{
........;
......;
}

What is an override modifier in C# programming language?

override modifier is used for abstract or virtual classes to extend them or to modify them. It is basically used in the derived classes to give a new implementation to the base class methods.
We cannot override a static method.
abstract class MyClass              // This is an abstract class.

{
public abstract int Function(); // This is an abstract method.
.........;
........;
}
class YourClass : MyClass
{
public override int Function() // Overriding Base class method.
{
.......;
.....;
}
}

What is an unsafe modifier in C# programming language?

unsafe modifier is used to denote unsafe context which is generally required for some pointer operations. Example,

public unsafe void Function()  // unsafe Method

{
int x, y; // unsafe context
..........; // we can use pointers here.
}

What is a volatile modifier in C# programming language?

In order to do any modifications by executing threads, volatile modifier is used.
volatile is applicable only for a class or struct. We can apply it to the field types such as integral, reference, pointer, enum and generic.

class Color

{
public volatile int clr; // volatile field
Color(int blue)
{
clr = blue;
}
}

What is a readonly modifier in C# programming language?

In order to prevent any further modifications to a field after its declaration, readonly keyword is used.

public class MyClass

{
readonly int color; // Read only and cannot be modified
}

What do you mean by public keyword in C# programming language?

public is an access modifier which has no restrictions. In other words, a type with public keyword can be accessed by any one from any assembly which references it.

syntax,
public class MyClass  // public class

{
public int a; // public field
}

What do you mean by protected keyword in C# programming language?

protected is an access modifier which provides access level to a particular class and any other classes derived from it. In other words, a type with protected keyword can be accessed only by its class members or any other derived class members.

syntax,
protected class MyClass

{
.........;
........;
}

What do you mean by private keyword in C# programming language?

private is an access modifier which provides access only to a particular class. In other words, a type with private keyword can be accessed only by the members of that class or struct .
It is the default access modifier.

syntax,
private class MyClass

{
...........;
.........;
}

What do you mean by internal keyword in C# programming language?

internal is an access modifier which provides access between the classes of current project assembly. In other words, a type with internal keyword can be accessed by any members of the same assembly reference and restricted to other assembly members.

syntax,
internal class MyClass

{
............;
...........;
}

What do mean by protected internal modifier in C# programming language?

protected and internal are the keywords which may be used at a time to provide both access limits to a type or class. In other words, a type with protected internal can be accessed by any members of the same assembly and also by the derived classes.

syntax,
protected internal MyClass

{
.............;
...........;
}

What do mean by static modifier in C# programming language?

static is a keyword which can be used in many cases such as for classes, methods, operators, constructors etc. static denotes the singular type and is used to declare a member or type.
static methods are very faster when compared with instance methods as they doesn't comprises any instances.
By using static, flexibility becomes less but performance will be more.

static class MyClass          // This is a static class

{
static int x; // This is a static variable
public static void Test() // This is a static method
{
.........;
........;
}
}

What do you mean by namespace keyword in C# programming language?

In order to declare a set of objects, namespace is used. namespace is a domain for the type names.
namespace is used to organize the elements which creates unique types by avoiding conflicts.
namespace Home.Hall.Television 

{
class Channel1 { }
class Channel2 { }
}

namespaces doesn't depends on assembly type and also they do not have any impact on the member accessibility such as public, private etc.
What do you mean by name scoping in C# programming language?

Name Scoping is the property of the namespace which allows the inner namespace to make use of the types declared in the outer namespace.
Example,
namespace Home

{
// Outer namespace
namespace Hall
{
class Television { }
class Furniture { }
// Inner namespace
namespace DiningRoom
{
class Tables : Hall.Furniture { }
class Chairs : Hall.Furniture { }
}
}
}

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