Keyword

Result: 105 questions

Explain Overloading in C# ?

Answer:

When methods are created with the same name, but with different signature its called overloading. For example, WriteLine method in console class is an example of overloading. In the first instance, it takes one variable. In the second instance, “WriteLine” method takes two variable.

Console.WriteLine(x);
Console.WriteLine("The message is {0}", Message);

Different types of overloading in C# are

  • Constructor overloading
  • Function overloading
  • Operator overloading
View

What is Constructor Overloading in C# .net ?

Answer:

In Constructor overloading, n number of constructors can be created for the same class. But the signatures of each constructor should vary. For example

public class Employee 
{
 public Employee() 
 { }
 public Employee(String Name) 
 { }
}
View

What is Function Overloading in C# .net ?

Answer:

In Function overloading, n number of functions can be created for the same class. But the signatures of each function should vary. For example

public class Employee 
{
 public void Employee() 
 { }
 public void Employee(String Name) 
 { }
}
View

What is Operator Overloading in C# .net ?

Answer:

We had seen function overloading in the previous example. For operator Overloading, we will have a look at the example given below. We had defined a class rectangle with two operator overloading methods.

class Rectangle
{
 private int Height;
 private int Width;

 public Rectangle(int w,int h)
 {
   Width=w;
   Height=h;
 } 
 public static bool operator >(Rectangle a,Rectangle b)
 {
   return a.Height > b.Height ;
 }
 public static bool operator <(Rectangle a,Rectangle b)
 {
   return a.Height < b.Height ;
 } 
}

Let us call the operator overloaded functions from the method given below. When first if condition is triggered, the first overloaded function in the rectangle class will be triggered. When second if condition is triggered, the second overloaded function in the rectangle class will be triggered. 

public static void Main()
{
Rectangle obj1 =new Rectangle();
Rectangle obj2 =new Rectangle();

 if(obj1 > obj2)
 {
  Console.WriteLine("Rectangle1 is greater than Rectangle2");
 } 

 if(obj1 < obj2)
 {
  Console.WriteLine("Rectangle1 is less than Rectangle2");
 }
}
View

Explain Inheritance in C# ?

Answer:

In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects. In inheritance, there will be two classes - base class and derived classes. A class can inherit attributes and methods from existing class called base class or parent class. The class which inherits from a base class is called derived classes or child class. For more clarity on this topic, let us have a look at 2 classes shown below. Here Class Car is Base Class and Class Ford is derived class.

class Car
{
 public Car()
 {
  Console.WriteLine("Base Class Car");
 }

 public void DriveType()
 {
  Console.WriteLine("Right Hand Drive");
 }
}

class Ford : Car
{
 public Ford()
 {
  Console.WriteLine("Derived Class Ford");
 }

 public void Price()
 {
  Console.WriteLine("Ford Price : 100K $");
 }
}

When we execute following lines of code ,

Ford CarFord = new Ford();
CarFord.DriveType();
CarFord.Price();

Output Generated is as given below.

Base Class Car
Derived Class Ford
Right Hand Drive
Ford Price : 100K $

What this means is that, all the methods and attributes of Base Class car are available in Derived Class Ford. When an object of class Ford is created, constructors of the Base and Derived class get invoked. Even though there is no method called DriveType() in Class Ford, we are able to invoke the method because of inheriting Base Class methods to derived class.

View

Can Multiple Inheritance implemented in C# ?

Answer:

In C#, derived classes can inherit from one base class only. If you want to inherit from multiple base classes, use interface.

View

What is Polymorphism in C# ?

Answer:

The ability of a programming language to process objects in different ways depending on their data type or class is known as Polymorphism. There are two types of polymorphism

  • Compile time polymorphism. Best example is Overloading
  • Runtime polymorphism. Best example is Overriding
View

Explain the use of Virtual Keyword in C# ?

Answer:

When we want to give permission to a derived class to override a method in base class, Virtual keyword is used. For example. lets us look at the classes Car and Ford as shown below.

class Car
{
 public Car()
 {
  Console.WriteLine("Base Class Car");
 }
 public virtual void DriveType()
 {
  Console.WriteLine("Right Hand Drive");
 }
}

class Ford : Car
{
 public Ford()
 {
  Console.WriteLine("Derived Class Ford");
 }
 public void Price()
 {
  Console.WriteLine("Ford Price : 100K $");
 }
 public override void DriveType()
 {
  Console.WriteLine("Right Hand ");
 }
}

When following lines of code get executed 

Car CarFord = new Car();
CarFord.DriveType();
CarFord = new Ford();
CarFord.DriveType();

Output is as given below.

Base Class Car
Right Hand Drive
Base Class Car
Derived Class Ford
Right Hand
View

What is overriding in c# ?

Answer:

To override a base class method which is defined as virtual, Override keyword is used. In the above example, method DriveType is overridden in the derived class.

View

What is Method Hiding in C# ?

Answer:

If the derived class doesn't want to use methods in the base class, derived class can implement it's own version of the same method with same signature. For example, in the classes given below, DriveType() is implemented in the derived class with same signature. This is called Method Hiding.

class Car
{
 public void DriveType()
 {
  Console.WriteLine("Right Hand Drive");
 }
}

class Ford : Car
{
 public void DriveType()
 {
  Console.WriteLine("Right Hand ");
 }
}
View

What is Abstract Class in C#?

Answer:

If we don't want a class to be instantiated, define the class as abstract. An abstract class can have abstract and non abstract classes. If a method is defined as abstract, it must be implemented in derived class. For example, in the classes given below, method DriveType is defined as abstract. 

abstract class Car
{
 public Car()
 {
  Console.WriteLine("Base Class Car");
 }
 public abstract void DriveType();
}

class Ford : Car
{
 public void DriveType()
 {
  Console.WriteLine("Right Hand ");
 }
}

Method DriveType get implemented in derived class.

View

What is Sealed Classes in c# ?

Answer:

If a class is defined as Sealed, it cannot be inherited in derived class. Example of a sealed class is given below.

public sealed class Car
{
 public Car()
 {
  Console.WriteLine("Base Class Car");
 }

 public void DriveType()
 {
  Console.WriteLine("Right Hand ");
 }
} 
View

What is an Interface in C# ?

Answer:

An interface is similar to a class with method signatures. There wont be any implementation of the methods in an Interface. Classes which implement interface should have an implementation of methods defined in the abstract class.

View

What is a Constructor in C# ?

Answer:

Constructor is a special method that get invoked/called automatically, whenever an object of a given class gets instantiated. In our class car, constructor is defined as shown below

public Car()
{
 Console.WriteLine("Base Class Car");
}

When ever an instance of class car is created from the same class or its derived class(Except Few Scenarios), Constructor get called and sequence of code written in the constructor get executed.

interface Breaks
{
 void BreakType();
}

interface Wheels
{
 void WheelType();
}

class Ford : Breaks, Wheels
{
 public Ford()
 {
  Console.WriteLine("Derived Class Ford");
 }
 public void Price()
 {
  Console.WriteLine("Ford Price : 100K $");
 }
 public void BreakType()
 {
  Console.WriteLine("Power Break");
 }
 public void WheelType()
 {
  Console.WriteLine("Bridgestone");
 }
}
View

What is a Destructor in C# ? 

Answer:

Destructor is a special method that get invoked/called automatically whenever an object of a given class gets destroyed. Main idea behind using destructor is to free the memory used by the object.

View

C# class can inherit multiple ________

Quiz

Which of the followings are value types in C#?

Quiz

Which of the following is a reference type in C#?

Quiz

An array in C# starts with _____ index.

Quiz

Which of the following is true for dynamic type in C#?

Quiz

© 2017 QuizBucket.org