Object Oriented Programming Questions and Answers
For 2 Years Experienced Programmers
Q1. What is Object Oriented Programming?
Ans: Object Oriented Programming is a paradigm where every solution we intend to make is treated
as an object.
-----------------------------------------------------------------------------------------------------------------------------
Q2. Why use Object Oriented Programming , when we already have procedural programming
methods ?
Ans: Though we have procedural programming methods it had short comings in addressing problems
posed by the real world. Unknowingly almost everything we conceptualize behaves as an
identity with properties. If we design such concepts in computers using procedural techniques
what we would end doing is create a code that works but has no abstraction. Abstraction is
required because it helps hide unnecessary details and helps us understand things better than
being carried away with details.
Also for programmers OOP helps keep consistency in programming, and it directly can be
related to concept, again which helps architect and developers get closer.
OOP helps in modularizing an idea, its directly seen in design phase itself, which makes us think
more than code.
OOP helps us encapsulate all relevant details into one block.
OOP speeds up development by facilitating the communication of concept to developers.
Above all it greatly reduces repetition of modules together which helps develop a more
generalized code which can be re used across platforms.
-----------------------------------------------------------------------------------------------------------------------------
Q3. What is signature of a function?
Ans. The signature of a function includes only and only the name of the function and its parameters.
It also includes the way we parameters, type of parameters and number of parameters.
-----------------------------------------------------------------------------------------------------------------------------
Q4. What are the benefits of OOP in programming? Or What are the features of OOP?.
Ans. The following benefits are achieved using OOP techniques:-
1. Abstraction.
2. Encapsulation.
3. Inheritance.
4. Polymorphism.
-----------------------------------------------------------------------------------------------------------------------------
Q5. What is Abstraction ?
Ans. Abstraction is applied in order to hide unnecessary details from the user. Its the generalization
or providing a general term to a set of tasks and data on which the tasks operate.
Example:- A car is an abstract term- because when we say car it gives us a general idea and
not a specific detail. Also car is an object of objects(engine, wheels etc).
This helps a buyer to not involve into unwanted details.
-----------------------------------------------------------------------------------------------------------------------------
Q5. What is Abstraction ?
Ans. Abstraction is applied in order to hide unnecessary details from the user. Its the generalization
or providing a general term to a set of tasks and data on which the tasks operate.
Example:- A car is an abstract term- because when we say car it gives us a general idea and
not a specific detail. Also car is an object of objects(engine, wheels etc).
This helps a buyer to not involve into unwanted details.
-----------------------------------------------------------------------------------------------------------------------------
Q6. What is Encapsulation?
Ans: Encapsulation is enclosing related terms together as to form a capsule thereby promoting
abstraction. Abstraction is achieved through Encapsulation. And we do it by using access
modifiers.
-----------------------------------------------------------------------------------------------------------------------------
Q7. What is Inheritance? What are the types of inheritance?.
Ans: Inheritance is to create class taking the characteristics of an existing base class. This helps in
reusing existing code. There following types of inheritance:-
Single -- (supported in all OO languages).
Multiple -- (supported in C++, not in Java, not in C#).
Multilevel -- (Supported in all OO languages).
-----------------------------------------------------------------------------------------------------------------------------
Q8. What is polymorphism? How is it done?
Ans: Polymorphism is the ability to use different implementations under a single name. We achieve it
by :-Method or Function overloading, Method or Function Overriding and Method hiding.
-----------------------------------------------------------------------------------------------------------------------------
Q9. How and when did you use OO techniques in your projects?
Ans: If you know OOP then you should be able to answer this in detail. Keep in mind this question
can get you your job , because here you can logically arrange your steps in the answer and show
your expertise and skill in implementing programming techniques. Also be sure to consume time
by showing more skill than to brag something which could create boredom.
-----------------------------------------------------------------------------------------------------------------------------
Q10. What is the difference between overloading and overriding?
Ans: Overloading is to use a single name for a method and provide different implementations.
Overriding is to provide new methods in place of old methods of base classes in derived classes.
-----------------------------------------------------------------------------------------------------------------------------
Q11. How do you implement Overloading without causing ambiguity?
Ans: By changing the signature of the function.
-----------------------------------------------------------------------------------------------------------------------------
Q12. What is the signature of a function , identify its parts with an example?
Ans: Signature comprises only of the method name and its parameters(Type, Number and
order of parameters).
Example: public void add(int a, int b) -- here the underlined part is signature.
-----------------------------------------------------------------------------------------------------------------------------
Q13. Why is the return type of a function not included in the method signature?
Ans: Its a wonderful question pertaining to the design of object oriented methodology.
To answer this we need to think deeper. Imagine having a list of function or method
definitions:-
public void add(int a, int b)/*This is right*/
{
/*what the heck why should I use add as the name of
function-- You can use any name*/
}
public void add(int b, int a) /*This is wrong this should
not be in this list*/
{
/*Simply changing parameter names will not let
compiler decide that its a different func.
you need to change the types or number of
arguments.*/
/*what the heck why should I use add as the name
of function-- You can use any name*/
}
Q6. What is Encapsulation?
Ans: Encapsulation is enclosing related terms together as to form a capsule thereby promoting
abstraction. Abstraction is achieved through Encapsulation. And we do it by using access
modifiers.
-----------------------------------------------------------------------------------------------------------------------------
Q7. What is Inheritance? What are the types of inheritance?.
Ans: Inheritance is to create class taking the characteristics of an existing base class. This helps in
reusing existing code. There following types of inheritance:-
Single -- (supported in all OO languages).
Multiple -- (supported in C++, not in Java, not in C#).
Multilevel -- (Supported in all OO languages).
-----------------------------------------------------------------------------------------------------------------------------
Q8. What is polymorphism? How is it done?
Ans: Polymorphism is the ability to use different implementations under a single name. We achieve it
by :-Method or Function overloading, Method or Function Overriding and Method hiding.
-----------------------------------------------------------------------------------------------------------------------------
Q9. How and when did you use OO techniques in your projects?
Ans: If you know OOP then you should be able to answer this in detail. Keep in mind this question
can get you your job , because here you can logically arrange your steps in the answer and show
your expertise and skill in implementing programming techniques. Also be sure to consume time
by showing more skill than to brag something which could create boredom.
-----------------------------------------------------------------------------------------------------------------------------
Q10. What is the difference between overloading and overriding?
Ans: Overloading is to use a single name for a method and provide different implementations.
Overriding is to provide new methods in place of old methods of base classes in derived classes.
-----------------------------------------------------------------------------------------------------------------------------
Q11. How do you implement Overloading without causing ambiguity?
Ans: By changing the signature of the function.
-----------------------------------------------------------------------------------------------------------------------------
Q12. What is the signature of a function , identify its parts with an example?
Ans: Signature comprises only of the method name and its parameters(Type, Number and
order of parameters).
Example: public void add(int a, int b) -- here the underlined part is signature.
-----------------------------------------------------------------------------------------------------------------------------
Q13. Why is the return type of a function not included in the method signature?
Ans: Its a wonderful question pertaining to the design of object oriented methodology.
To answer this we need to think deeper. Imagine having a list of function or method
definitions:-
public void add(int a, int b)/*This is right*/
{
/*what the heck why should I use add as the name of
function-- You can use any name*/
}
public void add(int b, int a) /*This is wrong this should
not be in this list*/
{
/*Simply changing parameter names will not let
compiler decide that its a different func.
you need to change the types or number of
arguments.*/
/*what the heck why should I use add as the name
of function-- You can use any name*/
}
public void add(int a, int b, int c) /*This is right*/ {
/*what the heck why should I add -- its
unimportant don't read this, its just a comment*/
}
/*what the heck why should I add -- its
unimportant don't read this, its just a comment*/
}
public void add(double a, int b, int c) /*This is right*/ {
/*what the heck why should I add -- its unimportant don't
read this, its just a comment*/
}
/*what the heck why should I add -- its unimportant don't
read this, its just a comment*/
}
When a method call is made for the add() method, the compiler first checks the matching
version of it, but how does it do it? It checks the name and parameter list, number of
parameters and their order to match the method. In any case the compiler does not know
beforehand what return type is expected from the call until runtime, and if we explicitly assign
function to a variable say like this
beforehand what return type is expected from the call until runtime, and if we explicitly assign
function to a variable say like this
x=add(a,b); // where x is an integer.
Only then the compiler has a chance to expect a return type.
Also an important scenario is that if we want two methods with same return types but work
differently depending on parameters the point of including return type into signature would have
no significance.
So since such an implementation will only work when expected return type is known to compiler
before hand. Hence its not include in some languages, but such features are supported by some
languages like - Perl and Haskell.