Object Oriented Programming Interview Questions - Part - 2

1. Describe Principles of object-oriented programming:
Basic concepts of oops
The basic concepts of oops are
1. Classes
2. Objects
3. Data abstraction
4. Encapsulation
5. Inheritance
6. Polymorphism
7. Dynamic binding
8. Data hiding
1. Class:
Class is template for an object. A class serves as a plan, or blue print. We can create any number of objects related to the class. Data items in a class are called data members. The function with in a class is called member functions.
2. Objects:
An instance of a class is called object. We can create any number of objects related to the class. Each object is associated with a data type class. Object can be defined as variables and type class. The following fig. shows the two ways to represent objects.
3. Data abstraction:
Data abstraction refers to putting together essential features without including background details.
4. Data hiding:
The insulation of data from direct access of program is called data hiding. In C++, the class construct allow to declare data and methods, as a public, private and protected group. The implementation details can be hidden. This is done by the data hiding principle.
5. Encapsulation:
Object: student
Data:
Name
DateofBirth
Marks
Functions
Total
Display
Total
Display
Student
The wrapping up of data and functions into a single unit is known as encapsulation. Data encapsulation is the most striking feature of a class. The data is not accessible to the outside world, and only those functions, which are wrapped in the class, can access it.
6. Inheritance:
Inheritance is the process by which objects of one class acquire the properties of objects of another class.
The main advantages of the inheritance are
_ Reusability of the code
_ To increase the reliability of the code
_ To add some enhancement to the base class
7. Polymorphism:
Polymorphism means one name, multiple forms. It allows us to have more than one function with the same name in a program. Operator overloading and function-overloading comes under polymorphism. This helps to reduce complexity and allowing the same interface to be used to specify a general class of action. The process of making an operator to exhibit different behaviors in different instances is known as operator overloading. The following fig. illustrates that a single function name can be used to handle different number and different types of arguments.
8. Dynamic binding:
Dynamic binding means that the code associated with a given procedure call is not known until the time of the call at run-time. This is also known as late binding. It is associated with polymorphism and inheritance.

2. What are the Benefits of OOPs?
a. Through inheritance we can eliminate redundant code and extend the use of existing
    classes.
b. The principle of the data hiding helps the programmer to build secure programs.
c. It is possible to have multiple objects to co-exit without interference
d. Classes
e. It is easy to partition the work in a project based on the objects.
f. Object-oriented systems can be easily upgraded from small to large systems.
g. Software complexity can be easily managed.
h. Code reuse is possible in conventional languages as well, but object oriented languages greatly enhance the possibility of reuse.
i. Message passing techniques for communication between objects is simple.

(b) What are the various parameters passing mechanisms supported by c++? Explain  with example.
 The various parameter passing mechanism supported by c++ are
                      Call by value
                      Call by reference
 Call by value:
                        The value is submitted to the called function. The calling function  passes the parameter by value.
Example:
                 int add(int,int);
            void main()
                {
                  int a,b,c;
                  cin>>a>>b;
                  c=add(a,b);
                  cout<<c;
                }
                int add(int a,int b)
                {
                 return(a+b);
                }
Call by reference:
                    The address is passed to the called function.
Example:
               int  add(int *,int *);
               void main()
   {
     int a,b,c;
               cin>>a>>b;
               c=add(&a,&b);
          cout<<c;
          }
         int add(int *a,int *b)
         {
          return(*a+*b);
         }

3. Explain Object Oriented Languages?
Depending on the object features supported, the languages are classified into two categories, they are
1. Object-based programming languages.
2. Object-oriented programming languages.
Object-based programming languages support encapsulation object identity without supporting the important features of inheritance, polymorphism and message communications.
Eg: ADA
                                           Object-based language=Encapsulation + Object identity

Object-oriented programming languages incorporate all the features of object-based programming languages along with inheritance and polymorphism.
                                        Object-oriented programming language=object-based language + Polymorphism + inheritance.

4. What are the Applications of OOPs?
_ Real time systems
_ Simulation and modeling
_ Object oriented database
_ Hypertext, hyper media and expert text
_ Artificial intelligence and Expert systems
_ Neural networks and parallel processing
_ Decision support and office automation systems
_ CAD/CIM systems
CAD-Computer Aided Design
CIM-Compute Input Microfilm
Applications of C++:
_ C++ allows us to create hierarchy-related objects.
_ we can build special object oriented libraries, which can be used later by many
     programmers.
_ C++ programs are easily maintainable and expandable. When a new feature needs
  to be implemented, it is very easy to add to the existing structure of an object.
_ C++ is able to map the real world problem properly.

2. Explain about inline function?
In C++ you can create short functions that are not actually called, instead their code is expanded in line at each point of invocation. An inline function is a function that is expanded in line when it is invoked. That is compiler replaces the function call with the corresponding function code. The inline functions are the functions that are expanded in line wherever they are invoked. Since when the inline functions are invoked, they make a copy of the function definition where it is invoked. This is the second copy (duplicated copy) of the function that occupies more memory. So having more number of inline functions will take much memory.
The inline functions take the form,
inline function_header
{
function body;
}
Example:
#include<iostream.h>
inline float mul( float x, float y)
{
return (x*y);
}
inline double div(double p, double q)
{
return(p/q);
}
void main( )
{
float a=12.56;
float b=15.10;
cout<< mul(a,b)<<endl;
cout<<div(a,b)<<endl;
}
The situations where inline expansion may not work are
• for functions returning values, if loop, a switch, or a goto exists
• for functions not returning values , if a return statement exists
• if function contain static variables
• if inline functions are recursive

3. Explain Function Overloading?
Function overloading means we can use the same function name to create
functions that perform a variety of different tasks.
Eg: An overloaded add ( ) function handles different data types as shown below.
// Declarations
iv. int add( int a, int b); //add function with 2 arguments of same type
v. int add( int a, int b, int c); //add function with 3 arguments of same type
vi. double add( int p, double q); //add function with 2 arguments of
different type
//Function calls
i. add (3 , 4);
ii. add (3, 4, 5);
iii. add (3 , 10.0);

5. Explain the following with Example.
(a) Reference parameters.
The arguments can pass to the functions in one of the two ways. Using call by value or using call by reference.
When using call by value a copy of the argument is passed to the function. Call by reference passes the address of the argument to the function.
C++ provides two ways to achieve the call by reference parameter passing.
1. We can explicitly pass a pointer to the argument.
2. We can use a reference parameter.
To create the reference parameter, precede the parameter’s name with an &.
Example;
#include<iostream.h>
void swap(int &i, int &j);
void main( )
{
  int a,b,c,d;
a=1;b=2;c=3;d=4;
cout<<”a and b”<<a” “ <<b<<endl;
swap(a,b);
cout<<”a and b”<<a” “ <<b<<endl;
cout<<”c an d”<<c” “ <<d<<endl;
swap(c,d);
cout<<”c and b”<<c” “ <<d<<endl;
}
void swap(int &i, int &j)
{
int t;
t=i;i=j;j=t;
}
(b) Passing references to objects.
When an object is passed to an argument to a function a copy of the object is made. When the copy is made objects normal constructor function is not called. When the function terminates the copy’s destructor is called. If for some reasons the users do not want the destructor function to be called simply pass the object by reference.
Example;
class sample
{
int x,y;;
             public:
  sample(int a,int b)
{
x=a;
y=b;
}
void display( )
{
cout<<x<<endl;
cout<<y;
}
void swap( sample &ob)
{
int t;
t=ob.x;
ob.x=ob.y;
ob.y=t;
}
};
void main( )
{
sample s(100,200);
s.swap(s);
s.display();
}
(c) Independent references.
The most common uses for the references are to pass an argument using call by reference and to act as a return value from function.
However we can declare a reference that is simply a variable. This type of reference is called independent reference. Independent reference is nothing but creating another name for an object variable. All independent references must be initialized when they are created.
Example:
int a;
int &ref=a;
Example;
void main( )
{
int a=10;
int &ref=a; //independent reference.
cout<<a<<”  “<<ref<<endl;
ref=15;
cout<<a<<”  “<<ref<<endl;
int b=20;
ref=b;  // this puts b’s value into a
cout<<a<<”  “<<ref<<endl;
ref--; // decrements a.
cout<<a<<”  “<<ref<<endl;
}
output.
10 10
15  15
20  20
19  19
(d) Call by reference functions.
In call by reference parameter passing the address of the arguments are passed to the function rather than the values that are directly passed.
Example;
int add (int *, int *);
void main( )
{
int x,y,z;
cin>>x>>y;
z=add(&x,&y);
cout<<z;
}
int add(int *a, int *b)
{
return (*a+*b);
}

Default arguments
C++ allows a function to assign a parameter a default value when no argument corresponding to that parameter is specified in a call to that function the default value is specified in a manner syntactically similar to that of a variable initialization.
Example:
  void  myfunc(double d=0.0)
   {
      //
    }
now myfunc()can be called one of two ways .
  myfunc(198.234);//pass an explicit value
      myfunc();             //let function use default
Default arguments are useful in situations where some arguments always have the same value
 example:
   int add(int a=10,int b=20)
      {
           return(a+b);
        }
 void main()
        {
               int x,y;
              cin>>x>>y;
              cout<<add(x,y);//uses  inputed value of x & y
              cout<<add();//uses default value
          }
The This Pointer:
The this pointer is a variable which is used to access the address of the class itself. Sometimes, the this pointer may have return data items to the called. For example,
   the functions call max () will set the pointer this to the address of the object A. The starting address is the same as the address of the first variable in the          class structure. Here is a short example that shows the mechanism.
#include<iostream.h>
class where
{
char ray;
public :
void display()
{
cout<<”Current object’s address is “<<this; }
};
void main()
{
where w1,w2;
w1. display() ;
w2 .display() ;
}
The main() program in this example creates two objects of type where . It then asks each object to print its address, using the display() member function .This function prints out the value of this pointer .