Sie sind auf Seite 1von 13

Q1) Explain variable size array with a suitable example?

A) Arrays are used to store values of the same data type in the same variable so
they can be accessed easily.
In C#, a variable-size array , also called as variable-sized array or runtime-sized
array, is an array data structure which helps for automatic storage of data, whose
length is determined at run time instead of compile time.
The syntax for declaring a variable sized array is:
int[ ] myArray=/size of array/;
Eg: int size = 50;

string[] words = new string[size];

Q2) Is multiple main method allowed in C#? Justify the statement with a
program?
A) When a program starts, it looks for an entry point. This is the role of
the Main() method. The way this works is that, at the beginning, the compiler
looks for a method called Main. If it doesnt find it, it produces an error.
Main Method is the entry point for program execution.
C# has a strong feature in which we can define more than one class with the Main
method. Since Main is the entry point for the program execution, there are now
more than one entry points.
Eg: using System;

using System.Collections.Generic;
using System.Text;
namespace Multiple_MainClasses

class A
{
static void Main(string[] args)
{
Console.WriteLine("I am from Class A");
Console.ReadLine();
}
}
class B
{
static void Main(string[] args)
{
Console.WriteLine("I am from Class B");
Console.ReadLine();
}

To execute the program:


Syntax: csc filename.cs /main:classname

Where, filename is the name of the file where the code is stored and classname is
the name of the class containing the Main which we would like to be the entry
point.
We may write as
csc filename.cs /main:A

[ for Class A Main Execution ] or,

csc filename.cs /main:B

[ for Class B Main Execution ]

Q3) What is difference between for loop and foreach loop?


A) For Loop:
In case of for the variable of the loop is always be int only.
The For Loop executes the statement or block of statements repeatedly
until specified expression evaluates to false.
There is need to specify the loop bounds(Minimum, Maximum).
example:
usingsytem;
class class1
{
static void Main()
{
int j=0;
for(inti=0; i<=10;i++)
{
j=j+1;
}
Console.ReadLine();
}
}
For-eachloop :

In case of For-each the variable of the loop while be same as the type of
values under the array.
The For-each statement repeats a group of embedded statements for
each element in an array or an object collection.
You do not need to specify the loop bounds minimum or maximum.
example:
usingsytem;
class class1
{
static void Main()
{
int j=0;
int[] arr=new int[]{0,3,5,2,55,34,643,42,23};
foreach(inti in arr)
{
j=j+1;
}
Console.ReadLine();
}
}
The major difference between the for and for-each loop in c#, we understand by its
working:
for loop:

1) for loop's variable always be integer only.


2) The For Loop executes the statement or block of statements repeatedly until
specified expression evaluates to false.
3) in for loop we have to specify the loop's boundary ( maximum or minimum).
Here we can say this is the limitation of the for loop.
for-each loop:
1) In the case of the for-each loop the variable of the loop while be same as the
type of values under the array.
2) The For-each statement repeats a group of embedded statements for each
element in an array or an object collection.
3) In for-each loop, You do not need to specify the loop bounds minimum or
maximum. Here we can say that this is the advantage of the for each loop.

Q4) Explain reference parameters and out parameters with a program?

A) Ref Parameter:
If you want to pass a variable as ref parameter you need to initialize it before you
pass it as ref parameter to method. Ref keyword will pass parameter as a reference
this means when the value of parameter is changed in called method it get reflected
in calling method also.
Declaration of Ref Parameter:
Generally we will use ref parameters like as shown below:

int i=3; // variable need to be initialized


Refsample(ref i);

If you observe above code first we declared variable and initialized with value 3
before it pass a ref parameter to Refsample method.
Example
class Program
{
static void Main()
{
int i; // variable need to be initialized
i = 3;
Refsample(ref i);
Console.WriteLine(i);
}
public static void Refsample(ref int val1)
{
val1 += 10;
}

Out Parameter
If you want to pass a variable as out parameter you dont need to initialize it before
you pass it as out parameter to method. Out keyword also will pass parameter as a
reference but here out parameter must be initialized in called method before it
return value to calling method.
Declaration of Out Parameter
Generally we will use out parameters like as shown below:
int i,j; // No need to initialize variable
Outsample(out i, out j);

Example

class Program
{
static void Main()
{
int i,j; // No need to initialize variable
Outsample(out i, out j);
Console.WriteLine(i);
Console.WriteLine(j);
}
public static int Outsample(out int val1, out int val2)
{
val1 = 5;
val2 = 10;
return 0;
}
}

Q5) Explain with an eg. the concept of method overloading.

A) The process of creating more than one method in a class with same name or
creating a method in derived class with same name as a method in base class is
called as method overloading.
In VB.net when you are overloading a method of the base class in derived class,
then you must use the keyword Overloads.

But in C# no need to use any keyword while overloading a method either in same
class or in derived class.
While overloading methods, a rule to follow is the overloaded methods must differ
either in number of arguments they take or the data type of at least one argument.
Example for Method Overloading
using System;
namespace ProgramCall
{
class Class1
{
public int Sum(int A, int B)
{
return A + B;
}
public float Sum(int A, float B)
{
return A + B;
}
}
class Class2 : Class1
{
public int Sum(int A, int B, int C)
{
return A + B + C;
}
}
class MainClass
{
static void Main()

{
Class2 obj = new Class2();
Console.WriteLine(obj.Sum(10, 20));
Console.WriteLine(obj.Sum(10, 15.70f));
Console.WriteLine(obj.Sum(10, 20, 30));
Console.Read();
}
}
}

Note :
Method overloading provides more than one form for a method. Hence it is an
example for polymorphism.

In case of method overloading, compiler identifies which overloaded method to


execute based on number of arguments and their data types during compilation it
self. Hence method overloading is an example for compile time polymorphism.

Q6) List and explain the various primitive data-types used in C#?
A) These data types are so called primitive (built-in types), because they are
embedded in C# language at the lowest level.
i) Boolean:

Boolean variables are stored as 16-bit (2-byte) numbers, but they can only
be True or False. Use the keywords True and False to assign one of the two states
to Boolean variables.

When numeric data types are converted to Boolean values, 0 becomes False and
all other values become True. When Boolean values are converted to numeric
types, False becomes 0 and True becomes -1.

Eg:
int a = 1;
int b = 2;
// Which one is greater?
bool greaterAB = (a > b);
// Is 'a' equal to 1?
bool equalA1 = (a == 1);
// Print the results on the console
if (greaterAB)
{
Console.WriteLine("A > B");
}
else
{
Console.WriteLine("A <= B");
}
Console.WriteLine("greaterAB = " + greaterAB);
Console.WriteLine("equalA1 = " + equalA1);

ii) Character:

Char variables are stored as unsigned 16-bit (2-byte) numbers ranging in value
from 0 through 65535. Each number represents a single Unicode character.

It is declared in C# with the keyword char.


Eg:
char ch = 'a';
// Print the results on the console
Console.WriteLine(
"The code of '" + ch + "' is: " + (int)ch);
ch = 'b';
Console.WriteLine(
"The code of '" + ch + "' is: " + (int)ch);
ch = 'A';
Console.WriteLine(
"The code of '" + ch + "' is: " + (int)ch);
Console output:
// The code of 'a' is: 97
// The code of 'b' is: 98
// The code of 'A' is: 65
iii) Integer:

Integer variables are stored as signed 32-bit (4-byte) integers ranging in value
from -2,147,483,648 through 2,147,483,647.

The Integer data type provides optimal performance on a 32-bit processor, as the
smaller integral types are slower to load and store from and to memory.

Integer types represent integer numbers and are sbyte, byte, short, int, long.

iv) Float:
Real types in C# are the real numbers we know from mathematics. They are
represented by float and double.
a) Real Type Float
The first type we will consider is the 32-bit real floating-point type float. It is
also known as a single precision real number.

The range of values, which can be hold in a float type (rounded with
accuracy of 7 significant decimal digits), is from
1.5 10-45 to 3.4 1038.
b) Real Type Double
The
second
real floating-point
type in
the
C#
language
is
the double type. It is also called double precision real numberand is a
64-bit type.
This type has precision of 15 to 16 decimal digits. The range of values, which
can be recorded in double (rounded with precision of 15-16 significant
decimaldigits), is from 5.0 10-324 to 1.7 10308.

v) Object type:
Object type is a special type, which is the parent of all other types in .NET
Framework. Declared with the keyword object, it can take values from any other
type. It is a reference type i.e. index (address) of a memory area which stores the
actual value.
Eg:
// Declare some variables
object container1 = 5;
object container2 = "Five";

// Print the results on the console


Console.WriteLine("The value of container1 is: " + container1);
Console.WriteLine("The value of container2 is: " + container2);

// Console output:
// The value of container1 is: 5

// The value of container2 is: Five.

Das könnte Ihnen auch gefallen