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

What is the use of default keyword?

It is used in 2 contexts.

1)With the switch statement.
example:

Console.WriteLine("enter number");
int a=Int32.Parse(Console.ReadLine());
switch(a)
{
case 100:
Console.WriteLine("correct");
break;
default:
Console.WriteLine("100 is right value");
break;
}


2) Provide default values for value types (which is 0) and null value as the default value

for the reference types:

example: int a=default(Int32);
default for a will be 0.
Can we call a function from derived class if you have one base class Virtual Function ?



Yes we can,

Using this code snippet we can understand that the virtual function(MyFunction) which is defined in the base class (hai) is called from the derived class (hello) which will return a value 1.

class hai

{
public virtual int MyFunction()
{
return 1;
}
}
class hello:hai
{
public int j()
{
return MyFunction();
}
}

What is an INDEXER in c# ?



Generally in C# Indexers are generally known as Smart Arrays.Defining a c# indexer is almost equal to defining properties.An indexer is a member which enables an object to be indexed in the same way as an array.


class IndexerDemo
{
private string []info = new string[5];
public string this [int index]
{
get
{
return info[index];
}
set
{
info[index] = value;
}
}
}


As shown above defining an indexer is much likely defining properties.
How can we sort elements in an array in descending order ?



We have 2 different methods like

Array.Sort()
---> Which sorts the given array

Array.Reverse()---> Which reverses the array

We need to use the Array.Reverse() method after Array.Sort() method is called.Because we get a descending order only if we reverse a sorted array.

This can be done using this small example.


int[] MyArray = new int[3];
MyArray[0] = 98;
MyArray[1] = 6;
MyArray[2] = 23;
Array.Sort(MyArray);
Array.Reverse(MyArray);


Here we get an output of (98 , 23 , 6). First it sorts the given array and later it reverses which generates our required descending order.
Is there any Supplement of exit() method or quitting a C# application ?



Yes we can, by using

System.Environment.Exit(int exitProgram)


or we can even use
Application.Exit()
if it is a windows application
How to restrict a class not to be inherited.?



This can be done by declaring the class as sealed class.

Example :

sealed class Abc
{
-ststements
}
class Xyz : Abc //invalid(we can't inherited a sealed class)

Can we use the Throws clause to raise an exception ?



No. The Throws clause is not meant for raising an exception

The throw statement signals the occurrence of an exception during the execution of a program.

If the program finds a throw statement the method would terminate and returns the error to the calling method.
What are the different Name Spaces that are used to create a localized application ?



There are 2 different name spaces required to create a localized application they are

. System.globalization

. System.Resources

We need to use both name spaces(Necessary).
What are the different Debugging tools come with .Net SDK ?



There are 2 different tools they are :

DbgcLr : This is also known as Graphic Debugger. Visual studio .Net uses this.


CorDBG
: This Command line debugger To use this we must compile the original c# file using the /debug switch.
What does the assert() method do in debug compilation ?



In this debug compilation ,

. The assert() method takes in a Boolean condition as a parameter.

. It shows an error if that condition is false

. The program runs without any intervention if the condition obtained is true.
Can we force Garbage Collector to run ?

Yes we can,

It is Possible to force Garbage Collector this can be done by calling a method called Collect().

But this makes a performance barrier since this is not a good programming practice.

Generally we have no control over garbage collector when it runs.

The main work of this GC is it checks if there are any objects which are not used by the application it calls a Destructor which frees the memory allocated to that unused objects.
Why is it not a good programming practice to use empty Destructors ?



If we create a destructor in our class there will be any entry created for the finalize queue.

When ever this destructor is called the Garbage Collector is invoked to process that queue.

So when ever that destructor is empty it causes a useless loss of performance.
Explain two different scenarios where the Static Constructors can be used ?



These are the 2 different scenarios where the Static Constructor can be used :

When ever the Class uses the log file and a constructor is used to write some contents into this file.

These Static Constructors are also used when we create Wrapper Classes for the un-managed code and when the constructor can call the load library method.
Write the difference between Stack and Heap?

Stack:
• Memory will be allocated at the compile time.
• Here the memory is allocated by the compiler.
• Memory will be allocated only in sequential locations.
• The memory will also be deleted by the compiler.
• There is lot of chance of memory wastage.

Heap:
• Memory will be allocated at the run time.
• Here the memory is allocated by the user.
• Memory will be allocated in sequential locations and non- sequential locations.
• The memory must be deleted explicitly by the user.
• There is no chance of memory wastage if the memory is handled perfectly.
What is the difference between "Convert" class and "Parse()" method?

>>> The Convert class is used to convert any value from any data type to another data type. This class consist of different methods for making conversions.
Example:
1) char ch = Convert.ToChar("x"); -> string to character
2) float f = Convert.ToSingle(45); -> int to float
3) int x = Convert.ToInt(4.5f); -> float to int

>>> The Parse method is used to convert only one string value to any other data type. Every data type is consisting of parse() method.
Example:
1) int x = int.Parse("600"); -> string to int
2) char ch = char.Parse("dnf"); -> string to character
Write the difference between Group box control and Panel control?

Group Box Control:
• A Group box control can be placed with a title.
• The group box control cannot display the scroll bar.
• Only limited number of controls can be placed within the group box.
• Group box is a light weight component.

Panel Control:
• A panel control can’t be placed with a title.
• The panel control can display the scroll bar.
• Any number of controls can be placed within the panel.
• Panel is a heavy weight component.
Will the Following code compile ? double d = 5689.56; int i = d;

NOTE: This is objective type question, Please click question title for correct answer.
What will be the output of the following code ? using System; public class BaseClass { public BaseClass() { Console.WriteLine("You are in Base Class of DotNetFunda"); } } public class ChildClass : BaseClass { public ChildClass() { Console.WriteLine("You are in Child Class of DotNetFunda"); } static void Main() { ChildClass CC = new ChildClass(); } }



You are in Base Class of DotNetFunda
You are in Child Class of DotNetFunda

This is because the Base classes are automatically instantiated before derived classes. If we could the see the output, The BaseClass constructor executed before the ChildClass constructor.
Which of the following is a value type in C# ?

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