Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

What is class member or member of class

Answer»

The properties ,events, variables and METHODS define in class are called members of class.There are LOTS of types members in the class.
(1)Private Member of class:-This member of a class have strict ACCESS control Only the member function of same class can access this members.
(2)Protected Member of Class: This of member class is accessible to member of its own calls and to any of the class member in a derived class.
(3)PUBLIC Member Of Class: This member of class can be accessible from any where in class and out of class.
(4)Public and Private Static Member Of a Class: The type of member of a class can be access without creating a object of class. While to access other member of class you have to create a object of class. The public static member can be accessed using access specifier while private static member can be accessed only the member functions.

2.

Can we create a instance of abstract class object

Answer»

As all of us know abstract method have not any implementation they have only DECLARTION as in below example we have two CONSTRUCTOR and one declartion of method sum nad below is another CLASS where we TRY to create its instantiate and we will get error

Example:-
public abstract class absexample
{
public absexamplel()
{
}
public absexample(int i)
{
}
public abstract int sum(int i, int j);
}

public class classtwo
{
absexample a; // we can create object of abstract class
a = new absexample() //Here we get error we not instantiate object of abstract class.
}

All of us know abstract method are only for declartion not for implemention so calling such function is MEANINGLESS. So it is not allowed to instantiate abstract class

3.

Types of Inheritance OOPS

Answer»

Below are the 5 diferent types of inheritance USED in oops.
(1) Single Inheritance
(2) Multiple Inheritance
(3) Hierarchical Inheritance
(4) Multilevel Inheritance
(5) Hybrid Inheritance or Virtual Inheritance

Now Example of Inheritance are GIVEN below
(1)Single Inheritance :- In this ONE DERIVED class inherits from only one base class



single inheritance

(2)Multiple Inheritance:-In this we have single derived class may inherit from two or more than two base classes



Multiple inheritance

(3)Hierarchical Inheritance:- In this we have multiple derived classes inherits from a single base class



Hierarchical inheritance

(4)Multilevel Inheritance:-In this the derived class inherits from a class, which in turn inherits from some other class



Multilevel inheritance

(5)Hybrid Inheritance or Hybrid Inheritance:-Hybrid Inheritance is combination of Hierarchical and Mutilevel Inheritance.



Hybrid inheritance

4.

what is constructor and its types with example

Answer»

Constructors are special methods that are used to initialize an object (INSTANCE) of a class. We donot have to define a return type for it because Constructors doesnot return anything. One of the most IMPORTANT thing of Constructors is that it has the same name as class. One important thing is that it must follows the access scope ( for exam private,protected, public, Internal and external).we can also overload Constructor. A class can have any number of constructors. Below is the one of simple example of Constructors
public class getproductpriceclass
{
public getproductpriceclass(int x)
{
Console.WriteLine (x);
}
}

There are many type Constructors as given Below:
(1)Default Constructor
(2)Parameterized constructor
(3)Private Constructor
(4)Static Constructor
(5)Copy Constructor

(1)Default Constructor:-A default constructor is a constructor in both C++ and C# that has no parameters or where it has parameters they are all defaulted.The compiler will generate its own default constructor for a class provided that no other constructors have been defined by the programmer. The compiler generated default constructor initializes the member variables of the class using their respective default constructors.
Example:-
class defaultcons // Has default parameterless constructor
{
public int Value { get; set; }
}

class Program
{
static void Main()
{
// Call the default constructor
defaultcons defval= new defaultcons();
defval.Value = 1;
Console.WriteLine(defval != null);
}
}

(2)Parameterized constructor:-A constructor with at least one parameter is called as parameterized constructor. Advantage of parameterized constructor is you can initialize each instance of the class to DIFFERENT values.
namespace Programname
{
class Test1
{
int A, B;
//Paremetrized Constructor
public Test1(int X, int Y)
{
A = X;
B = Y;
}
//Method to print
public void Print()
{
Console.WriteLine("A = {0} tB = {1}", A, B);
}
}
class MainClass
{
static void Main()
{
Test1 T1 = new Test1(80, 40); //Parameterized Constructor is called
T1.Print();
Console.Read();
}
}
}
output:-
A = 80 B = 40

(3)Private Constructor:-A private constructor is a special instance constructor. It is commonly used in classes that contain static members only. If a class has one or more private constructors and no public constructors. Private constructor is used to restrict the class from being instantiated when it contains EVERY member as static.
public class Myexample
{
private Myexample() {}
public static int counter;
public static int IncrementCounter()
{
return ++counter;
}
}
class MainClass
{
static void Main()
{
// Myexample myObject = new Myexample(); // Error
Myexample.counter = 100;
Myexample.IncrementCounter();
Console.WriteLine(Myexample.counter);
}
}


(4)Static Constructor:-A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced.A static constructor cannot be called directly and doesnot have access modifiers or have parameters. A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced. The USER has no control on when the static constructor is executed in the program. Static constructors are also useful when creating wrapper classes for unmanaged code.
namespace ProgramExample
{
class Testclass
{
public Testclass()
{
Console.WriteLine("First instance");
}
static Testclass()
{
Console.WriteLine("Static");
}
}
class StaticConstructor
{
static void Main()
{
//Both Static Constructor and instance constructor are invoked for first instance.
Testclass T1 = new Testclass();
//Only First instance constructor is invoked.
Testclass T2 = new Testclass();
Console.Read();
}
}
}
output:-Static
First instance
First instance

(5)Copy constructor:-A parameterized constructor that contains a parameter of same class type is called as copy constructor. Main purpose of copy constructor is to initialize new instance to the values of an existing instance.
namespace Programexample
{
class Testclass
{
int A, B;
public Testclass(int X, int Y)
{
A = X;
B = Y;
}
//Copy Constructor
public Testclass(Testclass T)
{
A = T.A;
B = T.B;
}
public void Print()
{
Console.WriteLine("A = {0} tB = {1}", A, B);
}
}
class CopyConstructor
{
static void Main()
{
Testclass T2 = new Testclass(80, 90);
//getting copy constructor
Testclass T3 = new Testclass(T2);
T2.Print();
T3.Print();
Console.Read();
}
}
}
output:-
A = 80 B = 90
A = 80 B = 90

5.

Function or Method Overload Ways with Example

Answer»

If any class which have multiple methods with same name but have DIFFERENT parameters then they are said overloaded. In Function OVERLOADING we use the same name for different functions to do same or different operations on same class.
We used this where we have to perform any operation with some different number of arguments.

There are mainly two WAYS function overload or method overload:-

1.By changing number of Arguments.
2.By having different types of argument.

Now example of above two ways are given below

(1)By changing number of Arguments.

int sum (int x, int y)
{
///some operations
}

int sum(int x, int y, int z)
{
///some operations
}


2.By having different types of argument.

int sum(int x,int y)
{
///some operations
}
double sum(double x,double y)
{
///some operations
}


Now take a Full example how to DEFINE and call overloading methods

using System;
namespace oopsfuntionoverload
{
class funOverloadexam
{
public string namevalue;
//Now START overloaded functions
public void setFunctionName(string var1)
{
name = var1;
}

public void setFunctionName(string var2, string var1)
{
name = var2 + "" + var1;
}

public void setFunctionName(string var3, string var2, string var1)
{
name = var3 + "" + var2 + "" + var1;
}

//Entry point
static void Main(string[] args)
{
funOverloadexam obj = new funOverloadexam();
obj.setFunctionName("barack");
obj.setFunctionName("barack "," obama ");
obj.setFunctionName("barack ","hussian","obama");
}
}
}

6.

difference between classes and struct

Answer»

(1)Struct always are value type on the other side class is a reference type.
(2)When we start class creation memory to class will be allocated on the heap but on the other side struct GETS INITIATED it gets memory on the stack.
(3)Classes may have explicit parameter less constructors. But structs doesnot have this.
(4)One of GOOD FEATURE of Classes is classes support inheritance. But there is no inheritance for structs. A struct cannot inherit from another struct or class, and it cannot be the BASE of a class. Like classes, structures can implement interfaces.
(5)We can assign null variable to class. But we cannot assign null to a struct variable, since structs are value type.
(6)We can declare a destructor in class but can not in struct.