OOps interview Questions
1) What is meant by Object Oriented Programming?
2) What is a Class?
Class is a template for a set of objects that share a common structure and a common behaviour. The keyword class in C# indicates that we are going to define a new class (type of object)
3) What is an Object?
4) What is an Instance?
An instance has state, behaviour and identity. The structure and behaviour of similar classes are defined in their common class. An instance is also called as an object.
5) What are the core OOP’s concepts?
6) What is meant by abstraction?
7) What is meant by Encapsulation?
8) What is meant by Inheritance?
9) What is meant by Polymorphism?
10) What is an Abstract Class?
There are scenarios in which it is useful to define classes that is not intended to instantiate; because such classes normally are used as base-classes in inheritance hierarchies, we call such classes abstract classes.
Abstract classes cannot be used to instantiate objects; because abstract classes are incomplete, it may contain only definition of the properties or methods and derived classes that inherit this implements it's properties or methods.
Static, Value Types & interface doesn't support abstract modifiers. Static members cannot be abstract. Classes with abstract member must also be abstract.
11) What is an Interface?
Interface is an outside view of a class or object which emphaizes its abstraction while hiding its structure and secrets of its behaviour.
An interface mandates a set of behavior, but not the implementation. Interface must be inherited. We can't create an instance of an interface.
An interface is an array of related function that must be implemented in derived type. Members of an interface are implicitly public & abstract. An interface can inherit from another interface.
12) What is a base class?
13) What is a subclass?
14) What is a superclass?
superclass is a class from which another class inherits.
15) What is a constructor?
16) What is a destructor?
17) What is meant by Binding?
18) What is meant by static binding?
19) What is meant by Dynamic binding?
20) Define Modularity?
21) What is meant by Persistence?
22) How to prevent a class from being inherited?
//C# Example
sealed class MyClass
{
public int x;
public int y;
}
No class can inherit from MyClass defined above. Instances of ClassA may be created and its members may then be accessed.
23) What is Polymorphism?
Compile time: function or operator overloading
Runtime: Inheritence & virtual functions
24) What is Abstract method?
25) What is Virtual method?
25) Can Struct be inherited?
26) What is Static field?
27) What is Static Method?
28) What is Virtual keyword?
29) What is New modifiers?
30) What is Sealed modifiers?
Sealed modifiers can also be applied to instance methods, properties, events & indexes. It can't be applied to static members.
Sealed members are allowed in sealed and non-sealed classes.
31) When to use Interface over abstract class?
ClassAbs abs = new ClassAbs(); where ClassAbs is abstract class.
Abstract classes contains one or more abstarct methods, ie method body only no implementation.
Interfaces: These are same as abstract classes only difference is that we can only define method definition and no implementation.
When to use what depends on various reasons. One being design choice.
One reason for using abstarct classes is we can code common functionality and force our developer to use it. I can have a complete class but I can still mark the class as abstract.
Developing by interface helps in object based communication.
32) What is pure virtual function?
A pure virtual function is a function that must be overridden in a derived class and need not be defined. A virtual function is declared to be "pure" using the curious "=0"
syntax:
class Base {
public:
void f1(); // not virtual
virtual void f2(); // virtual, not pure
virtual void f3() = 0; // pure virtual
};
33) Can we specify the access modifier for explicitly implemented interface method?
34) What is Protected access modifier in C#?
A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declare this member. In other words access is limited to within the class definition and any class that inherits from the class
A protected member of a base class is accessible in a derived class only if the access takes place through the derived class type.
35) What is Public access modifier in C#?
36) What is Private access modifier in C#?
Private members are accessible only within the body of the class or the struct in which they are declared. This is the default access modifier for the class declaration.
37) What is Internal access modifier in C#?
38 ) What is Protected Internal access modifier in C#?
Protected Internal means the method is accessible by anything that can access the protected method UNION with anything that can access the internal method.
b) A class has default modifiers as Internal .
It can declare members (methods etc) with following access modifiers:
public
internal
private
protected internal
c) An interface has default modifier as public
d) A struct has default modifier as Internal .
It can declare its members (methods etc) with following access modifiers:
public
internal
private
e) A methods, fields, and properties has default access modifier as "Private" if no modifier is specified.
40) What is method overloading?
Method overloading occurs when a class contains two methods with the same name, but different signatures.Method overloading allows us to write different version of the same method in a class or derived class. Compiler automatically select the most appropriate method based on the parameter supplied. {
public int Add(int a, int b)
{
return a + b;
}
public int Add(int a, int b, int c)
{
return a+b+c;
}
}
To call the above method, you can use following code.
AddClass ad= new AddClass();
int number = ad.Add(2, 3) // result =5
int number1 = ad.Add(2, 3, 4) // result = 9
Rules for Overloading
There must be changes in either return type,or number of parameters or type of parameters.
You can't have a overload method with same number parameters but different return type. In order to create overload method, the return type must be the same and parameter type must be different or different in numbers.
41) What is Overriding?
42) What is Method Overriding? How to override a function in C#?
You cannot override a non-virtual or static method. The overridden base method must be virtual, abstract, or override.
43) Can we call a base class method without creating instance?
Yes. But .. * Its possible by inheriting from that class also.
* Its possible from derived classes using base keyword.
44) In which cases you use override and new base?
45) Difference between new and override keyword?
using System;
class Program
{
public class BaseClass
{
public virtual void func1()
{
Console.WriteLine("Base Class function 1.");
}
public virtual void func2()
{
Console.WriteLine("Base Class function 2.");
}
public void func3()
{
Console.WriteLine("Base Class function 3.");
}
}
public class DeriveClass : BaseClass
{
public new void func1()
{
Console.WriteLine("Derieve Class fuction 1 used new keyword");
}
public override void func2()
{
Console.WriteLine("Derieve Class fuction 2 used override keyword");
}
public void func3()
{
Console.WriteLine("Derieve Class fuction 3 used override keyword");
}
}
static void Main(string[] args)
{
BaseClass b = new BaseClass();
b.func1();
DeriveClass d = new DeriveClass();
d.func1();
//Calls Base class function 1 as new keyword is used.
BaseClass bd = new DeriveClass();
bd.func1();
//Calls Derived class function 2 as override keyword is used.
BaseClass bd2 = new DeriveClass();
bd2.func2();
Console.Read();
}
}
Now the difference is
new: hides the base class function.
Override: overrides the base class function.
BaseClass objB = new DeriveClass();
If we create object like above notation and make a call to any function which exists in base class and derive class both, then it will always make a call to function of base class. If we have overidden the method in derive class then it wlll call the derive class function.
For example…
objB.func1(); //Calls the base class function. (In case of new keyword)
objB.func2(); //Calls the derive class function. (Override)
objB.func3(); //Calls the base class function.(Same prototype in both the class.)
Note:
// This will throw a compile time error. (Casting is required.)
DeriveClass objB = new BaseClass();
//This will throw run time error. (Unable to cast)
DeriveClass objB = (DeriveClass) new BaseClass();
0 comments:
Post a Comment