UNIT -2 (Continuation)
Important Questions
10. what is polymorphism?
* Polymorphism mean "many forms" , and it occurs when we
have many classes that are related to eachother by inheritance.
* Entities behaving in different ways depending upon the input.
The receive is known as Polymorphism.
* Whenever the input is change to output of the behaviour entity
also changes.
11. Write the difference between function overloading and function
over riding?
FUNCTION OVERLOADING:
* Function that has a same name but different signatures or
parameters in same class is called function overloading.
* Method overloading is also called early binding or compile time
polymorphism or static binding.
*The compiler automatically calls the required function checking
number of parameters and their type, which are passed into that
function.
* If the number of parameters and type does not match by any
method signatures, then it will give the compile time error.
* We can achieve function overloading by changing the number
of parameters used or by using different types of parameters or by
changing the order of parameters.
FUNCTION OVERRIDING
Method Overriding means having two methods with same name
and same signatures [parameters], one should be in the base class
and other method should be in a derived class [child class].
* Method overriding is also called run time polymorphism or
dynamic polymorphism or late binding.
* We can override a method in the base class by creating similar
function in the derived class. This can be achieved by using
inheritance and using virtual & override.
* Same signature means the methods must have the same name,
same number of arguments and same type of arguments.
* Method overriding is possible only in the derived classes, but not
within the same class.
* When the derived class needs a method with the same signature
as in the base class, but wants to execute different code than the
one provided by the base class then method overriding will be
used.
12. What is operator overloading in c# ? explain with example
* Defining of multiple behaviors to an operator is known as
operator overloading "+" is a overloaded operator that works as
additional operator when used between numeric and operator
when used between strings.
* In the same way we can also define any new behaviour for
your existing operator using the concept of operator overloading.
To Overload an operator e need to first define a method known
as operator method as follows.
[<modifiers>] static <type> operator <opt>(<param def's>)
{
stmts;
-> An Operator Method must be static only
-> An Operator Method must be value returning Method
-> The input and return type of an operator method will be of the
same type
EXAMPLE:
using System;
class Complex
private int x;
private int y;
public Complex()
public Complex(int i, int j)
x = i;
y = j;
public void ShowXY()
{
Console.WriteLine("{0} {1}", x, y);
public static Complex operator -(Complex c)
Complex temp = new Complex();
temp.x = -c.x;
temp.y = -c.y;
return temp;
class MyClient
public static void Main()
Complex c1 = new Complex(10, 20);
c1.ShowXY();
Complex c2 = new Complex();
c2.ShowXY();
c2 = -c1;
c2.ShowXY();
}
13. What is an abstract class? explain with an example
An abstract class can contain both abstract and non-abstract
methods in it. But any of its child class if it wants to consume the
non-abstract methods, first it should provide the implementation
for all abstract methods.
abstract class AbsParent
public void Add(int x,int y)
Console.WriteLine(x+y);
public void Sub(int x,int y)
Console.WriteLine(x-y);
public abstract void Mul(int x,int y);
public abstract void Div(int x,int y);
*Add a class "AbsChild.cs" and write the following
class AbsChild:AbsParent
public override void Mul(int x,int y)
Console.WriteLine(x*y);
public override void Div(int x,int y)
Console.WriteLine(x/y);
static void Main()
AbsChild obj=new AbsChild ();
obj.Add (100, 45); obj.Sub (43, 34);
obj.Mul (454, 45); obj.Div (43, 23);
Console.ReadLine ();
}
14. What is delegate? explain delegates in C#
* A 'delegate' is a pointer to a method, which can be used for
invoking a method.
* These are similar to the concept of Function pointer; we use in
our traditional 'C' and 'C++' languages that are used for invoking
functions.
* A Method that is defined in a class can be invoked in two
different ways.
1) Using object of a class we can invoke a method.
2) Using a Delegate also we can invoke a Method.
Using Delegates
If we want to use a 'delegate' for invoking a method follow the below
process.
Delegate Declaration
As a delegate is type, it should be declared or defined first as
following.
[<modifiers>] delegate <void|type> <Name> {[<param def's>]}
A delegate declaration also contains IO parameters in it just like a
method declaration, where the IO Parameters of delegate should
exactly be same as the IO Parameters of Method it has to call.
Delegates are of two types
1) SingleCast (or) UniCast Delegates
2) MultiCast Delegates
If a delegate is used for invoking of a single method it is referred as
a UniCast Delegate, where as if a delegate is used for invoking of
multiple methods, we called them as MultiCast Delegates.
15. What is exception handling? explain with
example(try,catch,finally,throw)
Exception Handling in C# is a process to handle runtime errors.
We perform exception handling so that normal flow of the
application can be maintained even after runtime errors.
In C#, we use 4 keywords to perform exception handling:
1.try
2.catch
3.finally, and
4.throw
EXAMPLE:
class Demo
{
static void Main()
{
int x, y, z;
try
{
Console.WriteLine("Enter x value:");
x = int.Parse(Console.ReadLine());
Console.WriteLine("Enter y value:");
y = int.Parse(Console.ReadLine());
if (y == 1)
return;
z = x / y;
Console.WriteLine(z);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("Finally Block:");
}
Console.WriteLine("End of the program");
Console.ReadLine();
}
}
16. What is an event? explain event procedure
Events are user actions such as key press, clicks, mouse
movements, etc., or some occurrence such as system generated
notifications. Applications need to respond to events when they
occur. For example, interrupts. Events are used for inter-process
communication.
Syntax for the declaration of Event:
public event EventHandler MyEvent;
17. What is method? explain methods in C#
A method is a code block that contains a series of statements.
A program causes the statements to be executed by calling the
method and specifying any required method arguments. In C#,
every executed instruction is performed in the context of a
method. The Main method is the entry point for every C#
application and it's called by the common language runtime
(CLR) when the program is started.
Methods are declared in a class, record, or struct by
specifying:
* An optional access level, such as public or private. The
default is private.
* Optional modifiers such as abstract or sealed.
* The return value, or void if the method has none.
* Any method parameters. Method parameters are enclosed in
parentheses and are separated by commas. Empty parenthesis
indicate that the method requires no parameters
example:
using System;
namespace Method {
class Program {
// method declaration
static int addNumbers() {
int sum = 5 + 14;
return sum;
static void Main(string[] args) {
// call method
int sum = addNumbers();
Console.WriteLine(sum);
Console.ReadLine();
}
}
18. what is collection? explain with an example
Collections are similar to Arrays, it provides a more flexible way of
working with a group of objects.
In arrays, you would have noticed that you need to define the
number of elements in an array beforehand. This had to be done
when the array was declared.
But in a collection, you don’t need to define the size of the collection
beforehand. You can add elements or even remove elements from
the collection at any point of time. This chapter will focus on how
we can work with the different collections available in C#.
using System;
using System.Collections.Generic;
public class ListExample
{
public static void Main(string[] args)
{
// Create a list of strings
var names = new List<string>();
names.Add("Sonoo Jaiswal");
names.Add("Ankit");
names.Add("Peter");
names.Add("Irfan");
// Iterate list element using foreach loop
foreach (var name in names)
{
Console.WriteLine(name);
}
}
}
Output:
Sonoo Jaiswal
Ankit
Peter
Irfan