Thursday, January 26, 2012

Mostly Asked Oops Interview Questions

Common OOPs Questions Frequently Asked By Interviewers

"When we face the technical Interview in an IT company(small OR MNC) we found that the interviewer mostly start interview with some basic  oops questions and we feel very bad when unable to reply. So dear friends I want to discuss you some common questions"

What is class and objects?
Sol:
Class is collection of functions and variables. and object is used to use this template.The keyword class in C# indicates that we are going to define a new class (type of object)

What is Virtual method?
Sol: Virtual Method has implementation & provide the derived class with the option to override it.

What is the use of constructor ?
Sol:
Constructor is used to create and instantiate a object. 

Can we declare a constructor to private?
Sol:
Yes

Can Struct be inherited?
Sol: No, Struct can't be inherited as this is implicitly sealed.

Can we specify the accessibility modifier inside the interface?
Sol:
It should be public there is no use of private methods in interface.
 
How to prevent a public class method to be overridden in derived class?
Sol: Just make method to sealed.

What is Sealed modifiers?
Sol:
Sealed types cannot be inherited & are concrete.
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.

How to prevent a class being instantiated?
Sol:
Make class abstract or static

How to override a private virtual method?
Sol:
You can not override a private method.

Can you derive a static method of base class in child class?
Sol:
No

What is Static field?
Sol:
To indicate that a field should only be stored once no matter how many instance of the class we create.

How to stop a class being inherited by another class?
Sol:
Just make the class sealed. by using sealed keyword.

Can you override a normal public method of base class and make it static?
Sol:
No , the method signature should be same to be overridden.
Question 11. what is early and late binding?
Answer. when we call a non virtual method it decide at compile time and this is called early binding. while if method calling decide at runtime is called late binding.

What is Static Method?
Sol:
It is possible to declare a method as Static provided that they don't attempt to access any instance data or other instance methods.

What is Inheritance?
Sol:
It provides a convenient way to reuse existing fully tested code in different context thereby saving lot of coding. 

What is Virtual keyword?
Sol:
This keyword indicates that a member can be overridden in a child class. It can be applied to methods, properties, indexes and events.

What is New modifiers?
Sol:
The new modifiers hides a member of the base class. C# supports only hide by signature.

Can namespace contain the private class?
Sol:
No, having a single private class does not mean anything , it is applicable only in case of nested class.

What is Abstract method?
Sol: Abstract method doesn't provide the implementation & forces the derived class to override the method.

When to use Interface over abstract class?
Sol:
Abstract Classes: Classes which cannot be instantiated. This means one cannot make a object of this class or in other way cannot create object by saying ClassAbs abs = new ClassAbs(); where ClassAbs is abstract class. 
Abstract classes contains have one or more abstarct methods, ie method body only no implementation.
Interfaces: These are same as abstract classes only difference is we can only define method definition and no implementation.
When to use wot 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.

0 comments:

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Powered by Code Imagine