C# Realtime Interview Questions and Answers:Part 4

11/07/2009 No Comment
Collection of C#.NET Interview Questions.

What is the use of the main() function in C#?

Every executable C# application must contain a class defining a Main() method that signifies the entry point of the application. Note that the Main() method is of the access type public by nature. Moreover, it is also static. See example below:
using System;
class Question
{
public static int Main(string[] args)
{
Console.Writeline("Another Question");
Console.Readline();
return 0;
}
}
A public member is accessible from other types. The main() method is set as static so that it may be invoked at class level itself, without the need of creating an instance of it. The single parameter is an array of strings. that may contain any number of incoming command line arguments.

Can we set the specifier of the main() method in C# as private?
Yes. When the access specifier is set as private for the Main() method, then other assemblies may not invoke this class' Main() method as the starting point of the application. The startup scope gets limited to the class in context itself. See code below:
private static void Main()
{
//This code isn't invoked automatically by other assemblies
}

Can we set different types of parameters & return-types in main() method"?
Yes. The Main() method may easily be played around with by developers by passing different parameters and setting different return types. See the code below, that demonstrates different ways the Main() method may be implemented:

(1)
public static void Main(string[] args)
{
//NO return type, the argument is an array of strings
}
(2)
public static int Main(string[] args)
{
//Return type is int, argument is an array of strings
}
(3)
public static int Main()
{
//Return type is int, NO arguments
}
(4)
public static void Main()
{
//Return type is void, NO arguments
}

What is the use of GetCommandLineArgs() method?
The GetCommandLineArgs() method is used to access the command line arguments. The return value of this method is an array of strings. It is a method of the System.Environment class. See the code example below:
public static int Main(string[] args)
{
string[] strArgs = System.Environment.GetCommandLineArgs();
Console.WriteLine("Arguments {0}", strArgs[0]);
}

What is the use of System.Environment class?
The class System.Environment is used to retrieve information about the operating system. Some of the static members of this class are as follows:
1) Environment.OSVersion - Gets the version of the operating system
2) Environment.GetLogicalDrives() - method that returns the drives
3) Environment.Version - returns the .NET version running the application
4) Environment.MachineName - Gets name of the current machine
5) Environment.Newline - Gets the newline symbol for the environment
6) Environment.ProcessorCount - returns number of processors on current machine
7) Environment.SystemDirectory - returns complete path to the System Directory
8) Environment.UserName - returns name of the entity that invoked the application

Why is the new keyword used for instantiating an object in .NET?
The new keyword instructs the .NET compiler to instantiate a new object, with appropriate number of bytes (depending on the type) for the object and gather required memory from the managed heap.

What are the default values for bool, int, double, string, char, reference-type variables?
When the objects of the following types are declared, then they have a default value during declaration. The following table shows the default value for each type:
Type Default Value
bool false
int 0
double 0
string null
char '\0'
Reference Type null

How to declare a constant variable in C#? What is the use of the const keyword?
If a variable needs to have a fixed value, that may not be changed across the application's life, then it may be declared with the const keyword. The value assigned to a constant variable (using the const keyword) must be known at the time of compilation

In C#, can we create an object of reference type using const keyword?
No. A constant member may not be created of an object that is of reference type, because its value is decided dynamically at runtime.

What is the difference between const and readonly in C#?
When using the const keyword to set a constant variable, the value needs to be set at compile time. The const keyword may not be used with an object of reference type.
In case a reference type needs to be assigned a non-changeable value, it may be set as readonly

What are the different parameter modifiers available in C#?

What do you mean by a parameter modifier in C#?
Parameter modifiers in C# are entities that controls the behaviour of the arguments passed in a method. Following are the different parameter modifiers in C#:
1) None - if there is NO parameter modifier with an argument, it is passed by value, where the method recieves a copy of the original data.
2) out - argument is passed by reference. The argument marked with "out" modifier needs to be assigned a value within this function, otherwise a compiler error is returned.
public void multiply(int a, int b, out int prod)
{
prod = a * b;
}
Here, note that prod is assigned a value. If not done so, then a compile time error is returned.
3) params- This modifier gives the permission to set a variable number of identical datatype arguments.
Note that a method may have only one "params" modifier. The params modifier needs to be in the last argument.
static int totalruns(params int[] runs)
{
int score = 0;
for(int x=0; x
&nsbp;score+=runs[x];
return score;
}
Further, from the calling function, we may pass the scores of each batsman as below...
score = totalruns(12,36,0,5,83,25,26);

4) ref - The argument is given a value by the caller, where data is passed by reference. This value may optionally be reset in the called method. Note that even if NO value is set in the called method for the ref attribute, no compiler error is raised.

Explain the main difference between out and ref in C#?
  • The out parameters will return  a compiler error if they are not assigned a value in the method. However, this is not the case with ref parameters in C#
  • The out parameters need not necessarily be initialized before passing to the method, whereas ref parameters always need to have an initial value before they are passed to a method.
  • Ref parameters are for that kind of data that might be modified,  whereas out parameters are for data that's an additional output for the function that are already using the return value for something.
Related Posts


No comments :

 

Aired | The content is copyrighted and may not be reproduced on other websites. | Copyright © 2009-2016 | All Rights Reserved 2016

Contact Us | About Us | Privacy Policy and Disclaimer