Object Oriented Programming Interview Questions - Part - 3

1. What is a main function?
All the C++ programs start with the function main ().Function main returns the integer value that indicates whether the program executed successfully or not.
Syntax: main ()
2. What is the purpose for the return statement?
The return statement is used to return the value from a function. The statements return 0; returns the value 0.The return statement supplies a value from the called function to the calling function.
3. Define friend function?
A function that has access to the private members of a class but is not itself a member of the class. An entire class can be a friend of another class.
4. How the class is specified?
Generally class specification has two parts
 class declaration
It describes the type and scope of its member
class function definition
It describes how the class functions are implemented
The general form is
Class class_name
variable declarations;
function declaration;
variable declaration;
function declaration;
5. How to create an object?
Once the class has been declared, we can create variables of that type by using the class name.
Eg: classname x; //memory for x is created
6. How to access a class member?
object-name. function-name (actual arguments)
7. How the member functions are defined?
Member functions can be defined in two ways
outside the class definition
Member function can be defined by using scope resolution operator ::
General format is
Return type class_name::function-name(argument declaration)
Inside the class definition
This method of defining member function is to replace the function declaration by the actual function definition inside the class. It is treated as inline function
Eg:class item
int a,b ;
void getdata(int x,int y)
8. What is static data member?
Static variable are normally used to maintain values common to the entire class.
1. It is initialized to zero when the first object is created. No other initialization is
2. only one copy of that member is created for the entire class and is shared by all
      the objects
3. It is only visible within the class, but its life time is the entire class
4. Type and scope of each static member variable must be defined outside the class
5. It is stored separately rather than objects
Eg: static int count//count is initialized to zero when an object is created.
int classname::count;//definition of static data member
9. What is static member function?
A member function that is declared as static has the following properties
1. A static function can have access to only other static member declared in the
      same class
2. A static member function can be called using the classname as follows
classname ::function_name;
10. How the objects are used as function argument?
This can be done in two ways
1 A copy of the entire object is passed to the argument
2 Only address of the objects is transferred to the function
11. What is called pass by reference?
In this method address of an object is passed, the called function works directly on the actual arguments.
12. Define const member
If a member function does not alter any data in the class, then we may declare it as const member function as
Void mul(int ,int)const;
13. Define pointers to member
It is possible to take the address of a member of a class and assign it to a pointer. The address of a member can be obtained by applying the operator &to a “fully qualified” class member name. A class member pointer can be declared using the operator::*with the class name.
Eg: class A
int m;
void show( );
pointer to member m is defined as
int A::*ip=&A::m;
A::*->pointer to member of A class
&A::m->address of the m member of A class
14. When the deferencing operator ->* is used?
It is used to access a member when we use pointer to both the object and the member.
15. When the deferencing operator.* is used?
It is used to access a member when the object itself is used as pointers.
16. Define local classes.
Classes can be defined and used inside a function or a block. Such classes are called local classes. It can use global variables and static variables declared inside the function but cannot use automatic local variables.
void test(int a)
class student
student s1(a);
17. Define constructor
A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as class name. The constructor is invoked whenever an object of its associated class is created. It is called constructor because it constructs the values of data members of the class
Class integer
integer( );//constructor
18. Define default constructor
The constructor with no arguments is called default constructor
Class integer
int m,n;
Integer( );
integer::integer( )//default constructor
the statement
integer a;
invokes the default constructor
19. Define parameterized constructor
Constructor with arguments is called parameterized constructor
Class integer
{ int m,n;
integer(int x,int y)
{ m=x;n=y;
To invoke parameterized constructor we must pass the initial values as arguments to the constructor function when an object is declared. This is done in two ways
1. By calling the constructor explicitly
integer int1=integer(10,10);
2. By calling the constructor implicitly
Integer int1(10,10);
20. Define default argument constructor
The constructor with default arguments are called default argument constructor
Complex(float real,float imag=0);
The default value of the argument imag is 0
The statement
complex a(6.0)
assign real=6.0 and imag=0
the statement
complex a(2.3,9.0)
assign real=2.3 and imag=9.0
21. What is the ambiguity between default constructor and default argument constructor?
The default argument constructor can be called with either one argument or no arguments. When called with no arguments, it becomes a default constructor. When both these forms are used in a class, it cause ambiguity for a statement such as A a;
The ambiguity is whether to call A::A () or A::A(int i=0)
22. Define copy constructor
A copy constructor is used to declare and initialize an object from another object. It takes a reference to an object of the same class as an argument
Eg: integer i2(i1);
Would define the object i2 at the same time initialize it to the values of i1.
Another form of this statement is
Eg: integer i2=i1;
The process of initializing through a copy constructor is known as copy initialization.
23. Define dynamic constructor
Allocation of memory to objects at time of their construction is known as dynamic constructor. The memory is allocated with the help of the NEW operator
Class string
char *name;
int length;
string( )
name=new char[length +1];
void main( )
string name1(“Louis”),name3(Lagrange);
24. Define const object
We can create constant object by using const keyword before object declaration.
Eg: Const matrix x(m,n);
25. Define destructor
It is used to destroy the objects that have been created by constructor. Destructor name is same as class name preceded by tilde symbol (~)
A destructor never takes any arguments nor it does it return any value. The compiler upon exit from the program will invoke it. Whenever new operator is used to allocate memory in the constructor, we should use delete to free that memory.
26. Define multiple constructors (constructor overloading).
The class that has different types of constructor is called multiple constructors
class integer
int m,n;
integer( ) //default constructor
integer(int a,int b) //parameterized constructor
m=a; n=b;
integer(&i) //copy constructor
void main()
integer i1; //invokes default constructor
integer i2(45,67);//invokes parameterized constructor
integer i3(i2); //invokes copy constructor
27. Write some special characteristics of constructor
1 They should be declared in the public section
2 They are invoked automatically when the objects are created
3 They do not have return types, not even void and therefore, and they cannot
4 return values
5 They cannot be inherited, though a derived class can call the base class
6 They can have default arguments
7 Constructors cannot be virtual function
28. How the objects are initialized dynamically?
To call parameterized constructor we should the pass values to the object
ie,for the constructor integer(int a,int b)
it is invoked by integer a(10,18)
this value can be get during run time. i.e., for above constructor
int p,q;
integer a(p,q);
29. What are Friend functions? Write the syntax.
A function that has access to the private member of the class but is not itself a member of the class is called friend functions.
The general form is
friend data_type function_name( );
Friend function is preceded by the keyword ‘friend’.
30. Write some properties of friend functions.
1 Friend function is not in the scope of the class to which it has been declared as friend. Hence it cannot be called using the object of that class.
2 Usually it has object as arguments.
3 It can be declared either in the public or private part of a class.
4 It cannot access member names directly. It has to use an object name and dot membership operator with each member name. eg: ( A . x )
31. How is a member function of a class defined?
Member function of a class is defined in two places.
1. inside the class definition
2. outside the class definition
32. What are the characteristics of a member function?
1 Several different classes can use the same function name.
2 The ‘membership label ‘will resolve their scope.
3 Member functions can access the private data of the class.
4 A member function can call another member function directly, without using the dot operator.
33. Define Nesting of member function?
A member function can be called by using its name inside another member function of the same class is known as nesting of member functions.
34. Give the syntax for calling a member function?
Object-name. Function-name (actual-arguments);
35. List the difference between constructor and destructor?
Constructor can have parameters Destructors have no parameters
There can be more than one only one destructor is used in Constructor class
Constructor is invoked when Destructor is invoked up on exit from object is declared program.
36. What is the advantage of using dynamic initialization?
The advantage of using dynamic initialization is that, various initialization formats can be provided using overloaded constructors.
37. Distinguish between assignment and initialization.


Assignment can be done at
any part of the program. Initialization is done where the
variable is declared.
Assignment refers assigning
some values to the variable
using assignment operator. Initialization refers, a variable declared with some initial value.
int a=10;
                   int b[3]={1,2,3};

38. Why C++ is not labeled as a pure object oriented programming language?
C++ is an object oriented programming language but it is not a pure object oriented programming language because, even though they provide the object oriented features like polymorphism, data hiding, etc, they do not provide facilities for concurrency, persistence ie., a process of storing objects are not fully supported by c++. So C++ is not a pure object oriented programming.
39. How will you create a class in C++?
Classes are created using the keyword class. A class is a way to bind the data and its associated functions together. A class declaration is similar syntactically to a structure.
The general form for creating class is given below.
class class_name
data members;
 member functions;
data members;
member functions;
Here the access_ specifier can be private, public, protected.
40. What are the various access specifiers?
There are three access specifiers available in C++.  They are
1. Public
2. Private
3. Protected
By default the data members and member functions declared with in a class are private to that class and will be accessed only with in that class.
This access specifier allows data members and member functions to be accessible to other parts of the program,(i.e., outside the class also.)
This access specifier is needed only when inheritance is involved. This has the working function similar to the private access specifier.
41. How will you create objects in C++?
A class declaration defines a new type that links code and data. This new type is then used to declare objects of that class. Once a class has been created we can create variables of that type by using the class name (like any other built-in type variable).
The general form is
Class_name variable;
Here the variable is nothing but the object.
sample  eno,age;
42. Compare and contrast structures and classes.
Structures Classes
Structures are created with the
Keyword struct. classes are created with the keyword
All the members inside a structure
 is treated as public members by
default All the  members and functions
in a class is private by default

The members of a structure cannot be
The members of a class can be
Hidden using access specifier.

Ex: struct student
          char name[40];
           int age;
        }s; Ex: class student
          char name[40];
           int age;
              void getdata();
              void display();

43. Compare and contrast unions and classes.

Unions Classes
Unions may contain both member functions and data members. Classes contain the data members and member functions.
Union members are public by default Class members are private by default.
No static variables can be members of
Static members can be the members of a class.
Ex: union student
          char name[40];
           int age;
        }s; Ex: class student
          char name[40];
           int age;
              void getdata();
              void display();

44. What are merits and demerits of friend functions?
Friend functions are the functions that are declared inside the class with the preceded keyword friend. The definition of the function is given somewhere else. Using this friend function the private functions inside the class can be made accessible to outside the class also.(ie. It has full access rights to private members of the class).
            The private members of the class are accessible to the outside class by using friend.
              The friend function does not have storage class specifies.
45. What are friend classes? Explain its merits and demerits.
It is possible for one class to be a friend of another class. When this is the case, the friend class and all its member functions have access to the private members defined with in the other class.
class values
int a,b;
 values(int i,int j)
{ a=i; b=j;}
friend class min;    // friend class declared.
class min {
 int min(values x);
47. What is the use of static data member? Why can’t we use a global variable instead of a static data member?
Static data members are normally used to maintain the values common to the entire class. It is initialized to zero when the first object of that class is created. Only one copy of that member is created for the entire class and shared by all the objects of that class. It is visible within the class and its lifetime is entire program.
48. When constructors and destructors are executed?
An object’s constructor is called when the object comes into existence, and an object’s destructor is called when the object is destroyed.
A local object’s constructor is executed when the object’s declaration statement is encountered. The destructor functions for the local objects are executed in the reverse order of the constructor functions.
Global object’s have their constructor functions execute before main () function begins execution. Global constructors are executed in order of their declarations. Global destructors execute in reverse order after main () function has terminated.
49. How will you create initialized and uninitialized array of objects?
The array of objects can be either initialized or uninitialized. They are shown below.
The uninitialized array of objects takes the form,
Class_name object[size];
Sample g[10];
The initialized array of objects takes the form,
Class_name object[size] = {val1,val2,…};
Sample en[3]={1,2,3};
Sample na[3]={‘ram’,’babu’,’sita’};
50. What is a this pointer?
C++ uses a unique keyword called this to represent an object that invokes a member function. This unique pointer is automatically passed to a member function when it is called. The pointer this acts as implicit argument to all the member functions.
class abc
{     int a;
The private variable a can be used directly inside a member function, like
we can also use this as
this ->a=15;
51. What are references? How will you create reference parameters?
Reference provides an alias i.e. a different name for a previously defined variable using & symbol.
   int a;
   int &r=a;
Reference parameters are created in two ways.
     1. passing values.
     2. passing address.
52. What do you mean by 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.
int a;
int &ref=a;
53. Explain dynamic memory allocation in C++.
C++ uses two unary operators that perform the task of allocating and freeing the memory in a better and easier way.
An object can be created using new operator and destroyed using delete operator. This takes the general form
Pointer_variable = new datatype;
Example; p=new int;
P=new int[10];

Delete Pointer_variable;
Example:        delete p;
Delete []p;
Using these operators we can dynamically allocate memory and free the memory.

Explain with example constructor overloading?
Constructor can be overloaded by passing argument to the constructor. Using overloaded constructors we can provide various initialization formats. Constructor overloading provide the flexibility of using different format of data at runtime depending on the situation functions that perform a variety of different tasks.
Constructors can be overloaded by using two types of constructors
                    i) constructor ( ) .They are constructors without arguments.
                   ii) constructor ( arg1,arg2...argn).They are constructors with arguments.
             In the first, constructor itself supplies the data values and no values are passed by calling program. In the second case function call passes appropriate values.
class integer
int m,n;
    { m=n=0;
integer(int a,int b)
{  m=a;n=b;
}   ;
Under what circumstances copy constructor is invoked?
Copy constructor is invoked when an object is passed as argument to the constructor. When one object is used to initialize another object, copy constructor is invoked. The value of one object is copied to another object where the bitwise copy is by passed. Reference variable is passed as argument to the constructor.
Explain the way to initializes one object by another where copy constructor will be invoked?
Reference variable has been used in the argument to copy constructor rather than passing values by values. When one object explicitly initializes another, such as in declaration when a copy of an object is made to be passed to a function when a temporary object is generated (most commonly , as a return value)
  myclass x=y //y explicitly initializing x
func(y);//y passed as a parameter
y=func();//y receiving a temporary ,return object
Write down the syntax for creating copy constructor?
         classname (const classname &object);
                   // body of consturctor